【问题标题】:How to add multiple annotations in map view如何在地图视图中添加多个注释
【发布时间】:2017-08-11 10:27:42
【问题描述】:

我必须在我的地图视图中显示 5 个点(在数组中)。我已经在我的日志中显示了数组值。循环运行完美,没有添加注释。这是我的代码:

            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
}

但它没有在我的地图视图中显示注释。提前致谢。

【问题讨论】:

    标签: ios swift3 annotations mapkit


    【解决方案1】:

    你可以通过操作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
    }
    

    【讨论】:

      【解决方案2】:

      我认为区域选择不正确。因此,要关注所有注释,请使用以下 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)
      
          }
      }
      

      【讨论】:

      • 问题在swift代码中,所以你应该先看一下,然后在没有Objective-C代码的情况下回答。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-27
      相关资源
      最近更新 更多