2017-08-11 12 views
0

マップビューで5ポイント(配列内にある)を表示する必要があります。私は私のlog.LoopのArray値が完全に実行していたと注釈が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 
} 

しかし、それは私の中に注釈が表示されませんマップビュー。事前にありがとうございます。

答えて

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

質問はスピーディーなコードなので、最初に見てからObjective-Cコードなしで答えてください。 –

+0

確かに..それを正しく編集する –

関連する問題