Ada*_*dam 6 annotations ios swift
试图在地图上做一些注释.但无法绕过它.我正在使用的代码是
// Names
names = ["Ben", "Big", "Hawk", "Enot", "Wiltons", "Scott's", "The Laughing"]
// Latitudes, Longitudes
coordinates = [
[51.519066, -0.135200],
[51.513446, -0.125787],
[51.465314, -0.214795],
[51.507747, -0.139134],
[51.509878, -0.150952],
[51.501041, -0.104098],
[51.485411, -0.162042],
[51.513117, -0.142319]
]
Run Code Online (Sandbox Code Playgroud)
Cas*_*lie 14
你真的没有尝试过多少.但看看这个.函数addAnnotation采用CLLocation类型的数组,这是您应该使用的.因此,使数组看起来像这样初始化CLLocation对象.我假设你已经有一个工作渲染的地图,mapView对象等.
let coords = [ CLLocation(latitude: xxxx, longitude: xxxx),
CLLocation(latitude: xxx, longitude: xxx),
CLLocation(latitude: xxx, longitude:xxx)
];
Run Code Online (Sandbox Code Playgroud)
这是一个可以接受该数组的函数,并循环遍历每个元素并将其作为注释添加到mapView(尚未呈现)
func addAnnotations(coords: [CLLocation]){
for coord in coords{
let CLLCoordType = CLLocationCoordinate2D(latitude: coord.coordinate.latitude,
longitude: coord.coordinate.longitude);
let anno = MKPointAnnotation();
anno.coordinate = CLLCoordType;
mapView.addAnnotation(anno);
}
}
Run Code Online (Sandbox Code Playgroud)
最后,MKMapViewDelegate当添加新注释时,您需要使用委托使用其自动调用的方法之一,因此我们可以对其进行排队和呈现.如果你一次添加注释,这是不必要的,但是最好像这样添加它们,以便稍后可以操作它们.
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation{
return nil;
}else{
let pinIdent = "Pin";
var pinView: MKPinAnnotationView;
if let dequeuedView = mapView.dequeueReusableAnnotationViewWithIdentifier(pinIdent) as? MKPinAnnotationView {
dequeuedView.annotation = annotation;
pinView = dequeuedView;
}else{
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: pinIdent);
}
return pinView;
}
}
Run Code Online (Sandbox Code Playgroud)
不要只是添加此代码并期望它能够正常工作,确保将其正确地合并到项目的正确区域.如果您有其他问题,请发表评论.
更新:
记得将MKMapViewDelegate协议继承到你使用的控制器中.
确保您实际调用addAnnotations,ViewDidLoad传入和传递coords数组.哪个可以定义ViewDidLoad.
确保mapView方法不在ViewDidLoad中,而是控制器的成员.
| 归档时间: |
|
| 查看次数: |
13320 次 |
| 最近记录: |