Swift:添加方向按钮以固定注释

Bri*_*vie 2 maps mkmapview mkannotation swift

我有一个使用 MKMapView 显示公司位置的应用程序。当用户访问联系页面时,屏幕顶部会出现一张地图,其中有一根大头针落下,它会显示我们的位置。单击地图图钉时,它会在注释中显示公司名称。

我使用 back4app 将公司地址从存储在 Parse 后端的数据中拉入 MKMApView。

我的这一切都工作得很好,但是,当我单击地图图钉并显示公司名称时,我试图在其中添加一个汽车图标按钮,以便用户可以获得前往我们公司的路线。

这是我必须设置注释的代码:

func addPinOnMap(_ address: String) {
    var hotelClass = PFObject(className: HOTEL_CLASS_NAME)
    hotelClass = hotelArray[0]

    if mapView.annotations.count != 0 {
        annotation = mapView.annotations[0] 
        mapView.removeAnnotation(annotation)
    }

    // Make a search on the Map
    localSearchRequest = MKLocalSearchRequest()
    localSearchRequest.naturalLanguageQuery = address
    localSearch = MKLocalSearch(request: localSearchRequest)

    localSearch.start { (localSearchResponse, error) -> Void in

        // Add PointAnnonation text and a Pin to the Map
        self.pointAnnotation = MKPointAnnotation()
        self.pointAnnotation.title = "\(hotelClass[HOTEL_NAME]!)"
        self.pointAnnotation.coordinate = CLLocationCoordinate2D( latitude: localSearchResponse!.boundingRegion.center.latitude, longitude:localSearchResponse!.boundingRegion.center.longitude)

        self.pinView = MKPinAnnotationView(annotation: self.pointAnnotation, reuseIdentifier: nil)
        self.mapView.centerCoordinate = self.pointAnnotation.coordinate
        self.mapView.addAnnotation(self.pinView.annotation!)

        // Zoom the Map to the location
        self.region = MKCoordinateRegionMakeWithDistance(self.pointAnnotation.coordinate, 1000, 1000);
        self.mapView.setRegion(self.region, animated: true)
        self.mapView.regionThatFits(self.region)
        self.mapView.reloadInputViews()
    }

}
Run Code Online (Sandbox Code Playgroud)

我只是不知道如何在注释中获取按钮图标并在单击按钮时显示方向。使用当前地图视图或打开用户设备上的地图应用程序,两种方式都可以。

谢谢。

Sph*_*ech 5

我建议您使用一个示例方法,在您的图钉上放置一个按钮,并通过苹果地图应用程序向用户提供图钉的方向。

首先,在您的 pin 声明之后,您应该声明一个按钮,让您有机会与 pin 中的用户交互(我们稍后将展示如何初始化 pin 中的按钮)。

您可以像这样声明您的按钮:

let smallSquare = CGSize(width: 30, height: 30)
let button = UIButton(frame: CGRect(origin: CGPointZero, size: smallSquare))
Run Code Online (Sandbox Code Playgroud)

请注意smallSquare为按钮指定未来图钉按钮的正确尺寸。


您可以使用以下方法在图钉按钮中添加图像:button.setBackgroundImage(UIImage(named: "car"), forState: .Normal)

苹果开发者给我们一些文档:苹果开发者网站


然后您可以使用以下方法进行按钮操作:button.addTarget(self, action: #selector(ViewController.getDirections), forControlEvents: .TouchUpInside)。因此,像这样,如果正常选择按钮 ( .TouchUpInside),程序就会调用该函数getDirection ()


现在,您可以使用以下方法初始化 pin 中的按钮:pinView?.leftCalloutAccessoryView = button

注意此方法将按钮设置在引脚的左侧。您可以将按钮放在右侧,如下所示:pinView?.rightCalloutAccessoryView = button

苹果开发人员提供了有关该主题的一些信息。

但是,我不知道如何初始化引脚整个表面上的按钮。


getDirections ()最后,您应该具有向用户提供引脚“方向”的功能。

我建议您使用苹果地图应用程序来实现此目的。

我的函数getDirections ()如下所示:

func getDirections(){
        guard let selectedPin = selectedPin else { return }
        let mapItem = MKMapItem(placemark: selectedPin)
        let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
        mapItem.openInMapsWithLaunchOptions(launchOptions)

        print ("GET DIRECTION")
    }
Run Code Online (Sandbox Code Playgroud)

我在这个网站上找到了这个功能。我认为它可以比我更好地解释这一点。

最后我的程序如下所示:

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

    guard !(annotation is MKUserLocation) else { return nil }
    let reuseId = "pin"
    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
    }
    pinView?.pinTintColor = UIColor.orangeColor()
    pinView?.canShowCallout = true
    //The initialization of the pin. Here your program looks great.

    // And after the code as seen above.
    let smallSquare = CGSize(width: 30, height: 30)
    let button = UIButton(frame: CGRect(origin: CGPointZero, size: smallSquare))
    button.setBackgroundImage(UIImage(named: "car"), forState: .Normal)
    button.addTarget(self, action: #selector(ViewController.getDirections), forControlEvents: .TouchUpInside)
    pinView?.leftCalloutAccessoryView = button

     return pinView
}




func getDirections(){
    guard let selectedPin = selectedPin else { return }
    let mapItem = MKMapItem(placemark: selectedPin)
    let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
    mapItem.openInMapsWithLaunchOptions(launchOptions)

    print ("GET DIRECTION")
}
Run Code Online (Sandbox Code Playgroud)

通常,您的程序应如下所示:

 func addPinOnMap(_ address: String) {    

        var hotelClass = PFObject(className: HOTEL_CLASS_NAME)
        hotelClass = hotelArray[0]

        if mapView.annotations.count != 0 {
            annotation = mapView.annotations[0]
            mapView.removeAnnotation(annotation)
        }

        // Make a search on the Map
        localSearchRequest = MKLocalSearchRequest()
        localSearchRequest.naturalLanguageQuery = address
        localSearch = MKLocalSearch(request: localSearchRequest)

        localSearch.start { (localSearchResponse, error) -> Void in

            // Add PointAnnonation text and a Pin to the Map
            self.pointAnnotation = MKPointAnnotation()
            self.pointAnnotation.title = "\(hotelClass[HOTEL_NAME]!)"
            self.pointAnnotation.coordinate = CLLocationCoordinate2D( latitude: localSearchResponse!.boundingRegion.center.latitude, longitude:localSearchResponse!.boundingRegion.center.longitude)

            self.pinView = MKPinAnnotationView(annotation: self.pointAnnotation, reuseIdentifier: nil)
            self.mapView.centerCoordinate = self.pointAnnotation.coordinate
            self.mapView.addAnnotation(self.pinView.annotation!)

            let smallSquare = CGSize(width: 30, height: 30)
            let button = UIButton(frame: CGRect(origin: CGPointZero, size: smallSquare))
            button.setBackgroundImage(UIImage(named: "car"), forState: .Normal)
            button.addTarget(self, action: #selector(ViewController.getDirections), forControlEvents: .TouchUpInside)
            self.pinView?.leftCalloutAccessoryView = button

            // Zoom the Map to the location
            self.region = MKCoordinateRegionMakeWithDistance(self.pointAnnotation.coordinate, 1000, 1000);
            self.mapView.setRegion(self.region, animated: true)
            self.mapView.regionThatFits(self.region)
            self.mapView.reloadInputViews()
        }

func getDirections(){
guard let selectedPin = selectedPin else { return }
let mapItem = MKMapItem(placemark: selectedPin)
let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
mapItem.openInMapsWithLaunchOptions(launchOptions)

print ("GET DIRECTION")
}
Run Code Online (Sandbox Code Playgroud)

抱歉,我无法尝试您的代码,因为我没有像 pinView 声明那样的完整代码。

请注意,您可能需要添加这行代码来接受比引脚可以显示的标注pinView?.canShowCallout = true

如果您想了解更多详细信息或告诉我评论,您可以查阅此网站。

索恩科技

苹果开发者