2017-08-11 106 views
0

我必须在我的地图视图中显示5个点(位于数组中)。我已经表明我的log.Loop数组值运行完美和注释都没有得到added.This是我的代码:如何在地图视图中添加多个注释

  self.mapView.delegate = self 
      self.mapView.mapType = MKMapType.standard 
      self.mapView.isZoomEnabled = true 
      self.mapView.isScrollEnabled = true 
      self.mapView.showsUserLocation = true 

     print("LOCATION LIST==>>") 
     print(items["Location"].arrayValue) 

     for locationList in items["Location"].arrayValue 
     { 
      print(locationList) 

      let annotation = MKPointAnnotation() 
      annotation.coordinate = CLLocationCoordinate2D(latitude: locationList["latitude"].doubleValue, longitude: locationList["longitude"].doubleValue) 
      self.mapView.addAnnotation(annotation)  
     } 

     self.mapView.delegate = self 

日志:在

LOCATION LIST==>> 
{ 
    "Latitude" : 12.988415, 
    "Longtitude" : 80.218386 
}, { 
    "Latitude" : 12.988445, 
    "Longtitude" : 80.21839199999999 
}, { 
    "Latitude" : 12.988492, 
    "Longtitude" : 80.218422 
}, { 
    "Latitude" : 12.988558, 
    "Longtitude" : 80.218461 
}, 
{ 
    "Latitude" : 12.991181, 
    "Longtitude" : 80.21950200000001 
} 

但它没有显示我的注解地图View.Thanks提前。

回答

0

您可以通过操作MKMapView's delegate

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
    print("viewForAnnotation \(annotation.title)") 
    if annotation is MKUserLocation { 
     return nil 
    } 
    let reuseID = "pin" 
    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as? MKPinAnnotationView 
    if(pinView == nil) { 
     pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID) 
     pinView!.canShowCallout = true 
     pinView!.animatesDrop = true 
    } 
    return pinView 
} 
0

我觉得该地区没有被正确选择。因此,要集中所有的注解既可以使用下面的API - >

func showAnnotations(_ annotations: [MKAnnotation], 
     animated: Bool) 

使用

self.mapView.showAnnotations(annotations, animated: true) 

或者尝试设置基于注解这适用于所有的注释区域 - >重点标记

func getVisibleRectForAnnotation(markers : [MKPointAnnotation?],width:Double) -> MKMapRect{ 
    var zoomRect:MKMapRect = MKMapRectNull 

    for index in 0..<markers.count { 
     let annotation = markers[index] 
     if let annotation = annotation{ 
      let aPoint:MKMapPoint = MKMapPointForCoordinate(annotation.coordinate) 
      let rect:MKMapRect = MKMapRectMake(aPoint.x, aPoint.y, width, width) 
      if MKMapRectIsNull(zoomRect) { 
       zoomRect = rect 
      } else { 
       zoomRect = MKMapRectUnion(zoomRect, rect) 
      } 
     } 
    } 
    return zoomRect 
} 

func focusMarkers(markers : [MKPointAnnotation?],width:Double){ 

    let zoomRect = getVisibleRectForAnnotation(markers: markers, width: width) 

    if(!MKMapRectIsNull(zoomRect)){ 
     let mapEdgePadding = UIEdgeInsets(top: 40, left: 40, bottom: 40, right: 40) 
     mapView.setVisibleMapRect(zoomRect, edgePadding: mapEdgePadding, animated: true) 

    } 
} 
+0

问题在swift代码中,所以您应该先看看,然后在没有Objective-C代码的情况下回答。 –

+0

哦,当然..编辑它权利awat –

相关问题