繁体   English   中英

如何在地图视图中添加多个注释

[英]How to add multiple annotations in map view

我必须在地图视图中显示5点(在数组中)。 我已经在日志中显示了Array值,Loop运行得很好,并且没有添加注释,这是我的代码:

            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中显示注释。

我认为该地区未正确选择。 因此,要集中所有注释,请使用以下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)

    }
}

您可以通过操纵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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM