Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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 从当前位置导航到标记_Ios_Swift_Google Maps_Map Directions - Fatal编程技术网

Ios 从当前位置导航到标记

Ios 从当前位置导航到标记,ios,swift,google-maps,map-directions,Ios,Swift,Google Maps,Map Directions,您好,我有多个这样的标记: var Groningen = GMSMarker() marker.position = CLLocationCoordinate2DMake(...., .....) marker.title = "...." marker.icon = UIImage(named: "...") marker.snippet = "....." marker.map = mapView var m

您好,我有多个这样的标记:

var Groningen = GMSMarker()
        marker.position = CLLocationCoordinate2DMake(...., .....)
        marker.title = "...."
        marker.icon = UIImage(named: "...")
        marker.snippet = "....."
        marker.map = mapView

var marker1= GMSMarker()
        marker1.position = CLLocationCoordinate2DMake(...., .....)
        marker1.title = "...."
        marker1.icon = UIImage(named: "...")
        marker1.snippet = "....."
        marker1.map = mapView
func mapView(mapView: GMSMapView!, didTapInfoWindowOfMarker marker: GMSMarker!) {

        let actionSheetController: UIAlertController = UIAlertController(title: "Navigeer", message: "Kies een optie!", preferredStyle: .ActionSheet)

        //Create and add the Cancel action
        let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
            //Do some stuff
        }
        actionSheetController.addAction(cancelAction)
        //Create and add first option action
        let GoGoogleMaps: UIAlertAction = UIAlertAction(title: "Google Maps", style: .Default) { action -> Void in
            if (UIApplication.sharedApplication().canOpenURL(NSURL(string:"comgooglemaps://")!)) {
                UIApplication.sharedApplication().openURL(NSURL(string:
                    "comgooglemaps://?center=40.765819,-73.975866&zoom=14&views=traffic")!)
            } else {
                UIApplication.sharedApplication().openURL(NSURL(string:
                    "https://maps.google.com/?daddr=Amsterdam")!);

            }

            //Do some other stuff
        }
        actionSheetController.addAction(GoGoogleMaps)
        //Create and add a second option action
        let GoAppleMaps: UIAlertAction = UIAlertAction(title: "Apple Maps", style: .Default) { action -> Void in

            if (UIApplication.sharedApplication().canOpenURL(NSURL(string:"http://maps.apple.com")!)) {
                UIApplication.sharedApplication().openURL(NSURL(string:
                    "http://maps.apple.com/?daddr=Amsterdam")!)
            } else {
                NSLog("Can't use Apple Maps");
            }

            //Do some other stuff
        }
        actionSheetController.addAction(GoAppleMaps)

        //We need to provide a popover sourceView when using it on iPad
       actionSheetController.popoverPresentationController?.sourceView = sender as! UIView;

        //Present the AlertController
        self.presentViewController(actionSheetController, animated: true, completion: nil)
    }
在中,您希望在信息窗口或地图下方的标签中添加一个按钮,该按钮将设置从当前位置到取消选定标记的方向

当用户单击按钮时,其获取问题如下:

var Groningen = GMSMarker()
        marker.position = CLLocationCoordinate2DMake(...., .....)
        marker.title = "...."
        marker.icon = UIImage(named: "...")
        marker.snippet = "....."
        marker.map = mapView

var marker1= GMSMarker()
        marker1.position = CLLocationCoordinate2DMake(...., .....)
        marker1.title = "...."
        marker1.icon = UIImage(named: "...")
        marker1.snippet = "....."
        marker1.map = mapView
func mapView(mapView: GMSMapView!, didTapInfoWindowOfMarker marker: GMSMarker!) {

        let actionSheetController: UIAlertController = UIAlertController(title: "Navigeer", message: "Kies een optie!", preferredStyle: .ActionSheet)

        //Create and add the Cancel action
        let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
            //Do some stuff
        }
        actionSheetController.addAction(cancelAction)
        //Create and add first option action
        let GoGoogleMaps: UIAlertAction = UIAlertAction(title: "Google Maps", style: .Default) { action -> Void in
            if (UIApplication.sharedApplication().canOpenURL(NSURL(string:"comgooglemaps://")!)) {
                UIApplication.sharedApplication().openURL(NSURL(string:
                    "comgooglemaps://?center=40.765819,-73.975866&zoom=14&views=traffic")!)
            } else {
                UIApplication.sharedApplication().openURL(NSURL(string:
                    "https://maps.google.com/?daddr=Amsterdam")!);

            }

            //Do some other stuff
        }
        actionSheetController.addAction(GoGoogleMaps)
        //Create and add a second option action
        let GoAppleMaps: UIAlertAction = UIAlertAction(title: "Apple Maps", style: .Default) { action -> Void in

            if (UIApplication.sharedApplication().canOpenURL(NSURL(string:"http://maps.apple.com")!)) {
                UIApplication.sharedApplication().openURL(NSURL(string:
                    "http://maps.apple.com/?daddr=Amsterdam")!)
            } else {
                NSLog("Can't use Apple Maps");
            }

            //Do some other stuff
        }
        actionSheetController.addAction(GoAppleMaps)

        //We need to provide a popover sourceView when using it on iPad
       actionSheetController.popoverPresentationController?.sourceView = sender as! UIView;

        //Present the AlertController
        self.presentViewController(actionSheetController, animated: true, completion: nil)
    }
这在某种程度上是可能的。

基于,在自定义信息窗口中的按钮上添加触摸事件并不容易

(您可以改为实现GMSMapViewDelegate的-didTapInfoWindowOfMarker:delegate方法来检查infowindow是否被点击。 (缺点是整个信息窗口变成一个按钮)

但是,如果要在MapView中添加UILabel,则需要将
consumesGesturesInView
设置为false。因此UILabel可以接收触摸事件

添加带有触摸事件的UILabel的示例代码:

 override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        let label = UILabel(frame: CGRectMake(view.frame.size.width - 100, view.frame.size.height - 40, 80, 30))
        label.backgroundColor = UIColor.whiteColor()
        label.text = "direction"
        label.textAlignment = .Center
        label.layer.cornerRadius = 10
        label.clipsToBounds = true
        label.userInteractionEnabled = true

        let tap = UITapGestureRecognizer(target: self, action: "directionTapped")
        label.addGestureRecognizer(tap)

        mapView!.settings.consumesGesturesInView = false
        mapView!.addSubview(label)
        mapView!.bringSubviewToFront(label)

    }
显示地图方向选项的示例代码:

 func directionTapped() {
        let openMapsActionSheet = UIAlertController(title: "Open in Maps", message: "Choose a maps application", preferredStyle: .ActionSheet)
        openMapsActionSheet.addAction(UIAlertAction(title: "Apple Maps", style: .Default, handler: { (action: UIAlertAction!) -> Void in
            let placemark = MKPlacemark(coordinate: CLLocationCoordinate2DMake(self.mapView!.selectedMarker.position.latitude, self.mapView!.selectedMarker.position.longitude), addressDictionary: nil)
            let item = MKMapItem(placemark: placemark)
            let options = [MKLaunchOptionsDirectionsModeKey:
                MKLaunchOptionsDirectionsModeDriving,
                MKLaunchOptionsShowsTrafficKey: true]
            item.openInMapsWithLaunchOptions(options as [NSObject : AnyObject])
        }))
        openMapsActionSheet.addAction(UIAlertAction(title: "Google Maps", style: .Default, handler: { (action: UIAlertAction!) -> Void in
            if (UIApplication.sharedApplication().canOpenURL(NSURL(string:"comgooglemaps://")!)) {
                UIApplication.sharedApplication().openURL(NSURL(string:
                    "comgooglemaps://?saddr=\(self.mapView!.selectedMarker.position.latitude),\(self.mapView!.selectedMarker.position.longitude)&daddr=\(self.mapView!.selectedMarker.position.latitude),\(self.mapView!.selectedMarker.position.longitude)")!)
            } else {
                UIApplication.sharedApplication().openURL(NSURL(string:
                    "http://maps.google.com/maps?saddr=\(self.mapView!.selectedMarker.position.latitude),\(self.mapView!.selectedMarker.position.longitude)&daddr=\(self.mapView!.selectedMarker.position.latitude),\(self.mapView!.selectedMarker.position.longitude)")!)
            }
        }))
        openMapsActionSheet.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
        presentViewController(openMapsActionSheet, animated: true, completion: nil)
    }

我仍然有一个问题,我可以使用if-else语句吗?如果使用puch按钮,将显示一条消息,提示用户首先选择pin,然后按下按钮