【问题标题】:Swift: Add Directions button to pin annotationSwift:添加方向按钮以固定注释
【发布时间】:2017-02-05 10:22:04
【问题描述】:

我有一个使用 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()
    }

}

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

谢谢。

【问题讨论】:

    标签: swift maps mkmapview mkannotation


    【解决方案1】:

    我向您推荐一个示例方法,在您的 pin 上添加一个按钮,并通过苹果地图应用程序向用户提供 pin 的方向。

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

    你可以这样声明你的按钮:

    let smallSquare = CGSize(width: 30, height: 30)
    let button = UIButton(frame: CGRect(origin: CGPointZero, size: smallSquare))
    

    注意smallSquare 为按钮提供您未来图钉按钮的正确大小。


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

    Apple 开发者给我们一些文档:Apple developer web site.


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


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

    注意此方法将按钮设置在图钉的左侧。您可以像这样将按钮放在右侧:pinView?.rightCalloutAccessoryView = button

    Apple developer提供一些关于这个主题的信息。

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


    最后,你应该有函数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")
        }
    

    我在这个web site 中找到了这个功能。我认为它可以比我更好地解释这一点。

    最后我的程序是这样的:

    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")
    }
    

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

     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")
    }
    

    对不起,我不能尝试你的代码,因为我没有像 pinView 声明那样的完整代码。

    注意,您需要添加这行代码才能接受,而不是 pin 可以显示标注pinView?.canShowCallout = true

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

    Thorn technologies

    Apple developer

    【讨论】:

    • 您的建议非常好,这一切都适用于我的情况!感谢您抽出宝贵时间帮助我,我现在可以通过地图图钉按钮成功获取路线!
    猜你喜欢
    • 1970-01-01
    • 2011-04-25
    • 2013-06-17
    • 2017-06-11
    • 1970-01-01
    • 2010-11-28
    • 2018-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多