Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在MapKit中为pin注释命名?_Ios_Swift_Mapkit - Fatal编程技术网

Ios 如何在MapKit中为pin注释命名?

Ios 如何在MapKit中为pin注释命名?,ios,swift,mapkit,Ios,Swift,Mapkit,我想为上面的绿色和右侧pin注释命名 我看到一个视频教程,他可以使用annotation.title=为注释命名,但我不知道为什么我可以在我的MapKit中正确显示名称 这是我使用的代码 import UIKit import MapKit class ViewController: UIViewController, MKMapViewDelegate { @IBOutlet weak var mapKit: MKMapView! override func viewD

我想为上面的绿色和右侧pin注释命名

我看到一个视频教程,他可以使用
annotation.title=
为注释命名,但我不知道为什么我可以在我的MapKit中正确显示名称

这是我使用的代码

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var mapKit: MKMapView!


    override func viewDidLoad() {
        super.viewDidLoad()

        mapKit.delegate = self

       let bakrieTowerCoordinate = CLLocation(latitude: -6.23860724759536, longitude: 106.789429759178)
       let GBKCoordinate = CLLocation(latitude: -6.23864960081552, longitude: 106.789627819772)


        let locationGBK : CLLocationCoordinate2D = CLLocationCoordinate2DMake(-6.23864960081552, 106.789627819772)
        let locationBakrieToweer : CLLocationCoordinate2D = CLLocationCoordinate2DMake(-6.23860724759536, 106.789429759178)

        let annotation =  MKPointAnnotation()
        annotation.coordinate = locationGBK
        annotation.title = "GBK"
        annotation.subtitle = "Stadion"
        mapKit.addAnnotation(annotation)

        let annotation2 =  MKPointAnnotation()
        annotation2.coordinate = locationBakrieToweer
        annotation2.title = "Bakrie Tower"
        annotation2.subtitle = "Office"
        mapKit.addAnnotation(annotation2)



        zoomMapOn(location1: GBKCoordinate, location2: bakrieTowerCoordinate)


    }



    func zoomMapOn(location1: CLLocation, location2: CLLocation) {

        let distanceOf2CoordinateInMeters =  location1.distance(from: location2)
        let radius = distanceOf2CoordinateInMeters * 3

        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location1.coordinate, radius, radius)
        mapKit.setRegion(coordinateRegion, animated: true)

    }


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

        let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")

        guard let locationName = annotation.title else {return nil}


        if locationName == "GBK" {
            annotationView.pinTintColor = UIColor.green
        } else if locationName == "Bakrie Tower" {
            annotationView.pinTintColor = UIColor.red
        }


        return annotationView
    }






}

在返回
注释视图之前,您需要在
地图视图(uuquo:viewFor:)
中设置此属性:

annotationView.canShowCallout = true

现在,当您点击pin时,它将显示您的文本。

在返回您的
注释视图之前,您需要在
地图视图(\uquo:viewFor:)
中设置此属性:

annotationView.canShowCallout = true

现在,当您点击pin时,它将显示您的文本。

将此代码添加到视图控制器中-

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var mapKit: MKMapView!


    override func viewDidLoad() {
        super.viewDidLoad()

        mapKit.delegate = self

        let bakrieTowerCoordinate = CLLocation(latitude: -6.23860724759536, longitude: 106.789429759178)
        let GBKCoordinate = CLLocation(latitude: -6.23864960081552, longitude: 106.789627819772)


        let locationGBK : CLLocationCoordinate2D = CLLocationCoordinate2DMake(-6.23864960081552, 106.789627819772)
        let locationBakrieToweer : CLLocationCoordinate2D = CLLocationCoordinate2DMake(-6.23860724759536, 106.789429759178)

        let annotation =  MKPointAnnotation()
        annotation.coordinate = locationGBK
        annotation.title = "GBK"
        annotation.subtitle = "Stadion"
        mapKit.addAnnotation(annotation)

        let annotation2 =  MKPointAnnotation()
        annotation2.coordinate = locationBakrieToweer
        annotation2.title = "Bakrie Tower"
        annotation2.subtitle = "Office"
        mapKit.addAnnotation(annotation2)


        zoomMapOn(location1: GBKCoordinate, location2: bakrieTowerCoordinate)

    }



    func zoomMapOn(location1: CLLocation, location2: CLLocation) {

        let distanceOf2CoordinateInMeters =  location1.distance(from: location2)
        let radius = distanceOf2CoordinateInMeters * 3

        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location1.coordinate, radius, radius)
        mapKit.setRegion(coordinateRegion, animated: true)

    }


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

        let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")

        guard let locationName = annotation.title else {return nil}


        if locationName == "GBK" {
            annotationView.canShowCallout = true
        } else if locationName == "Bakrie Tower" {
            annotationView.pinTintColor = UIColor.red
        }

        annotationView.canShowCallout = true // Add this line in your code
        return annotationView
    }

}
当你点击pin时,它会显示文本,如-

加上
annotationView.canShowCallout=true
mapView(uu.mapView:)
中。谢谢。

将此代码添加到视图控制器-

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var mapKit: MKMapView!


    override func viewDidLoad() {
        super.viewDidLoad()

        mapKit.delegate = self

        let bakrieTowerCoordinate = CLLocation(latitude: -6.23860724759536, longitude: 106.789429759178)
        let GBKCoordinate = CLLocation(latitude: -6.23864960081552, longitude: 106.789627819772)


        let locationGBK : CLLocationCoordinate2D = CLLocationCoordinate2DMake(-6.23864960081552, 106.789627819772)
        let locationBakrieToweer : CLLocationCoordinate2D = CLLocationCoordinate2DMake(-6.23860724759536, 106.789429759178)

        let annotation =  MKPointAnnotation()
        annotation.coordinate = locationGBK
        annotation.title = "GBK"
        annotation.subtitle = "Stadion"
        mapKit.addAnnotation(annotation)

        let annotation2 =  MKPointAnnotation()
        annotation2.coordinate = locationBakrieToweer
        annotation2.title = "Bakrie Tower"
        annotation2.subtitle = "Office"
        mapKit.addAnnotation(annotation2)


        zoomMapOn(location1: GBKCoordinate, location2: bakrieTowerCoordinate)

    }



    func zoomMapOn(location1: CLLocation, location2: CLLocation) {

        let distanceOf2CoordinateInMeters =  location1.distance(from: location2)
        let radius = distanceOf2CoordinateInMeters * 3

        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location1.coordinate, radius, radius)
        mapKit.setRegion(coordinateRegion, animated: true)

    }


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

        let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")

        guard let locationName = annotation.title else {return nil}


        if locationName == "GBK" {
            annotationView.canShowCallout = true
        } else if locationName == "Bakrie Tower" {
            annotationView.pinTintColor = UIColor.red
        }

        annotationView.canShowCallout = true // Add this line in your code
        return annotationView
    }

}
当你点击pin时,它会显示文本,如-

加上
annotationView.canShowCallout=true
mapView(uu.mapView:)
中。多谢各位