如何在地图上显示iPhone的图钉

时间:2010-07-07 12:21:15

标签: iphone mkmapview

我正在制作一张地图视图,在经度和经度上我想要显示一个图钉,我是在mapview中制作这样的应用程序的新手。

如果有人知道,请告诉我。

提前致谢

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我将使用它来显示地图中的图钉。

func setupData() {
        // 1. check if system can monitor regions
        if CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) {

            // 2. region data
            let title = ""
            let allAnnotations = self.mapVW.annotations
            self.mapVW.removeAnnotations(allAnnotations)
            setZoom()
            for item in arrPin
            {
                let dict = item as? NSDictionary
                if let lat = dict?.value(forKey: "lat") as? String, let long = dict?.value(forKey: "lon") as? String
                {
                    selectedPlaceAnnotation = MKPointAnnotation()
                    selectedPlaceAnnotation.coordinate = CLLocationCoordinate2DMake(Double(lat)!,Double(long)!)
                    mapVW.addAnnotation(selectedPlaceAnnotation)
                    arrAnnotation.add(selectedPlaceAnnotation)
                }


            }
        }

    }

 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

        guard !(annotation is MKUserLocation) else {
            return nil
        }


        // Better to make this class property
        let annotationIdentifier = "AnnotationIdentifier"
        var annotationView = MKAnnotationView()
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        annotationView.frame = CGRect(x: annotationView.frame.origin.x, y: annotationView.frame.origin.y, width: 80, height: 200)

        annotationView.annotation = annotation
        annotationView.tag = index
        index += 1

        let imageViewPin = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 40))
        imageViewPin.center = CGPoint(x: annotationView.center.x, y: annotationView.center.y - 11)
        imageViewPin.image = UIImage(named: "green_pin")
        annotationView.addSubview(imageViewPin)

        return annotationView
    }