从当前位置导航到标记
Navigate from current location to marker
你好,我有多个这样的标记:
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)
}
这在某种程度上可能吗?
基于this Whosebug answer,没有简单的方法可以在自定义信息window 中的按钮上添加触摸事件。
(您可以改为实施 GMSMapViewDelegate 的 -didTapInfoWindowOfMarker: 委托方法来检查信息window 是否被点击。
(缺点是整个infowindow变成了一个button))
但是如果你想在你的 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)
}
你好,我有多个这样的标记:
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)
}
这在某种程度上可能吗?
基于this Whosebug answer,没有简单的方法可以在自定义信息window 中的按钮上添加触摸事件。
(您可以改为实施 GMSMapViewDelegate 的 -didTapInfoWindowOfMarker: 委托方法来检查信息window 是否被点击。 (缺点是整个infowindow变成了一个button))
但是如果你想在你的 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)
}