使用 googleMaps api 在 swift mapView 中显示方向按钮
show direction button in swift mapView using googleMaps api
我正在为 android 和 ios 开发应用程序。在 android 地图视图中,当我点击地图的标记时,视图底部会显示 2 个按钮。 (方向和 google 地图)查看上传的图片
按照 google 的说明,我在 swift 中设置了 mapView 以显示 google 的地图。但是当我点击标记时,我在 mapView 中看不到任何按钮。这是为什么?
google 在 swift 中设置地图视图的说明:
https://developers.google.com/maps/documentation/ios-sdk/start
我用谷歌搜索了这个,但一无所获。所以我手动做了。通过覆盖 'didTapMarker' 我添加了这两个按钮:
func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) -> Bool {
if marker.title != nil {
let mapViewHeight = mapView.frame.size.height
let mapViewWidth = mapView.frame.size.width
let container = UIView()
container.frame = CGRectMake(mapViewWidth - 100, mapViewHeight - 63, 65, 35)
container.backgroundColor = UIColor.whiteColor()
self.view.addSubview(container)
let googleMapsButton = CustomButton()
googleMapsButton.setTitle("", forState: .Normal)
googleMapsButton.setImage(UIImage(named: "googlemaps"), forState: .Normal)
googleMapsButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
googleMapsButton.frame = CGRectMake(mapViewWidth - 80, mapViewHeight - 70, 50, 50)
googleMapsButton.addTarget(self, action: "markerClick:", forControlEvents: .TouchUpInside)
googleMapsButton.gps = String(marker.position.latitude) + "," + String(marker.position.longitude)
googleMapsButton.title = marker.title
googleMapsButton.tag = 0
let directionsButton = CustomButton()
directionsButton.setTitle("", forState: .Normal)
directionsButton.setImage(UIImage(named: "googlemapsdirection"), forState: .Normal)
directionsButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
directionsButton.frame = CGRectMake(mapViewWidth - 110, mapViewHeight - 70, 50, 50)
directionsButton.addTarget(self, action: "markerClick:", forControlEvents: .TouchUpInside)
directionsButton.gps = String(marker.position.latitude) + "," + String(marker.position.longitude)
directionsButton.title = marker.title
directionsButton.tag = 1
self.view.addSubview(googleMapsButton)
self.view.addSubview(directionsButton)
}
return true
}
func markerClick(sender: CustomButton) {
let fullGPS = sender.gps
let fullGPSArr = fullGPS!.characters.split{[=10=] == ","}.map(String.init)
let lat1 : NSString = fullGPSArr[0]
let lng1 : NSString = fullGPSArr[1]
let latitude:CLLocationDegrees = lat1.doubleValue
let longitude:CLLocationDegrees = lng1.doubleValue
if (UIApplication.sharedApplication().openURL(NSURL(string:"comgooglemaps://")!)) {
if (sender.tag == 1) {
UIApplication.sharedApplication().openURL(NSURL(string:
"comgooglemaps://?saddr=&daddr=\(latitude),\(longitude)&directionsmode=driving")!)
} else if (sender.tag == 0) {
UIApplication.sharedApplication().openURL(NSURL(string:
"comgooglemaps://?center=\(latitude),\(longitude)&zoom=14&views=traffic")!)
}
} else {
let regionDistance:CLLocationDistance = 10000
let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
var options = NSObject()
if (sender.tag == 1) {
options = [
MKLaunchOptionsMapCenterKey: NSValue(MKCoordinate: regionSpan.center),
MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan: regionSpan.span),
MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving
]
} else if (sender.tag == 0) {
options = [
MKLaunchOptionsMapCenterKey: NSValue(MKCoordinate: regionSpan.center),
MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan: regionSpan.span)
]
}
let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = sender.title
mapItem.openInMapsWithLaunchOptions(options as? [String : AnyObject])
}
}
在 mapView 的右下角添加了两个按钮,点击它们后,将打开 googleMaps 应用程序(如果存在)。感谢您投反对票:(
自定义按钮 class:
class CustomButton: UIButton {
var gps = ""
override func awakeFromNib() {
super.awakeFromNib()
//TODO: Code for our button
}
}
我遇到了同样的问题,但是在 Objective-c 中并且按照我已经实现的代码执行。
对于那些想在点击标记图标后使用 Google 地图添加方向图标的人,下面的代码对我有用:
-(bool)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker{
UIButton *directionsButton = [[UIButton alloc] init];
[directionsButton setTitle:@"" forState:UIControlStateNormal];
[directionsButton setImage:[UIImage imageNamed:@"ic_google_direction"] forState:UIControlStateNormal];
[directionsButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[directionsButton setFrame:CGRectMake(self->mapView.frame.size.width - 110, self->mapView.frame.size.height - 130, 50, 50)];
[directionsButton addTarget:self action:@selector(markerClick) forControlEvents:UIControlEventTouchUpInside];
[directionsButton setTag:1];
[[self view] addSubview:directionsButton];
return NO;
}
-(void) markerClick{
NSMutableArray *installedNavigationApps = [[NSMutableArray alloc] initWithObjects:@"Apple Maps", nil];
if ([[UIApplication sharedApplication] canOpenURL:
[NSURL URLWithString:@"comgooglemaps://"]]) {
[installedNavigationApps addObject:@"Google Maps"];
}
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"Selecione um aplicativo" message:@"Abrir com" preferredStyle:UIAlertControllerStyleActionSheet];
for (NSString *app in installedNavigationApps) {
if([app isEqualToString:@"Apple Maps"]){
[actionSheet addAction:[UIAlertAction actionWithTitle:app style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[[UIApplication sharedApplication] openURL:
[NSURL URLWithString: [NSString stringWithFormat:@"http://maps.apple.com/?saddr=%@,%@&daddr=%@,%@", _latitude, _longitude, _denuncia.Latitude, _denuncia.Longitude]]];
}]];
}
else if([app isEqualToString:@"Google Maps"]){
[actionSheet addAction:[UIAlertAction actionWithTitle:app style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[[UIApplication sharedApplication] openURL:
[NSURL URLWithString: [NSString stringWithFormat:@"comgooglemaps://?saddr=%@,%@&daddr=%@,%@&zoom=14&directionsmode=driving", _latitude, _longitude, _denuncia.Latitude, _denuncia.Longitude]]];
}]];
}
}
[actionSheet addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
// Present action sheet.
[self presentViewController:actionSheet animated:YES completion:nil];
}
我使用 swift 版本 3 的代码。我更改了包含视图,我将 UIImageView 的子视图添加到 containView。
在 google 地图上添加两个按钮。我在顶部使用锂代码。
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
if marker.title != nil {
let mapViewHeight = mapView.frame.size.height
let mapViewWidth = mapView.frame.size.width
let container = UIView()
container.frame = CGRect.init(x: mapViewWidth - 100, y: mapViewHeight - 63, width: 65, height: 35)
container.backgroundColor = UIColor.white
self.view.addSubview(container)
let googleMapsButton = CustomButton()
googleMapsButton.setTitle("", for: .normal)
googleMapsButton.setImage(UIImage(named: "googlemaps")?.resizableImage(withCapInsets: UIEdgeInsets.init(top: 0, left: 0, bottom: 50, right: 50)),for: .normal)
googleMapsButton.setTitleColor(UIColor.blue, for: .normal)
googleMapsButton.frame = CGRect.init(x: mapViewWidth - 80, y: mapViewHeight - 70, width: 50, height: 50)
googleMapsButton.addTarget(self, action: #selector(self.markerClick(sender:)), for: .touchUpInside)
googleMapsButton.gps = String(marker.position.latitude) + "," + String(marker.position.longitude)
googleMapsButton.title = marker.title
googleMapsButton.tag = 0
let directionsButton = CustomButton()
directionsButton.setTitle("", for: .normal)
directionsButton.setImage(UIImage(named: "googlemapsdirection")?.resizableImage(withCapInsets: UIEdgeInsets.init(top: 0, left: 0, bottom: 50, right: 50)),for: .normal)
directionsButton.setTitleColor(UIColor.blue, for: .normal)
directionsButton.frame = CGRect.init(x:mapViewWidth - 110, y:mapViewHeight - 70, width:50, height: 50)
directionsButton.addTarget(self, action: #selector(self.markerClick(sender:)), for: .touchUpInside)
directionsButton.gps = String(marker.position.latitude) + "," + String(marker.position.longitude)
directionsButton.title = marker.title
directionsButton.tag = 1
self.view.addSubview(googleMapsButton)
self.view.addSubview(directionsButton)
}
return false
}
func markerClick(sender:CustomButton){
let fullGPS = sender.gps
let fullGPSArr = fullGPS!.characters.split{[=10=] == ","}.map(String.init)
let lat1 : String = fullGPSArr[0]
let lng1 : String = fullGPSArr[1]
guard let lat1_double = Double(lat1),let lng1_double = Double(lng1) else{
return
}
let latitude:CLLocationDegrees = lat1_double
let longitude:CLLocationDegrees = lng1_double
if (UIApplication.shared.openURL(URL(string:"comgooglemaps://")!)) {
if (sender.tag == 1) {
UIApplication.shared.openURL(URL(string:
"comgooglemaps://?saddr=&daddr=\(latitude),\(longitude)&directionsmode=driving")!)
} else if (sender.tag == 0) {
UIApplication.shared.openURL(URL(string:
"comgooglemaps://?center=\(latitude),\(longitude)&zoom=14&views=traffic")!)
}
} else {
let regionDistance:CLLocationDistance = 10000
let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
var options : NSDictionary! = nil
if (sender.tag == 1) {
options = [
MKLaunchOptionsMapCenterKey: NSValue.init(mkCoordinate: regionSpan.center),
MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span),
MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving
]
} else if (sender.tag == 0) {
options = [
MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
]
}
let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = sender.title
mapItem.openInMaps(launchOptions: options as? [String : Any])
}
}
自定义按钮 - swift
只需将 class 添加到 viewcontroller 底部
class CustomButton: UIButton {
var gps = ""
override func awakeFromNib() {
super.awakeFromNib()
//TODO: Code for our button
}
}
请求 "appleMap" 需要导入 MapKit
SWIFT 4.2
根据@Lithium 的回答,我更新了与 Swift 4.2:
兼容的代码
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
if marker.title != nil {
let mapViewHeight = mapView.frame.size.height
let mapViewWidth = mapView.frame.size.width
let container = UIView()
container.frame = CGRect(x: mapViewWidth - 100, y: mapViewHeight - 63, width: 65, height: 35)
container.backgroundColor = UIColor.white
self.view.addSubview(container)
let googleMapsButton = CustomButton()
googleMapsButton.setTitle("", for: .normal)
googleMapsButton.setImage(UIImage(named: "googlemaps"), for: .normal)
googleMapsButton.setTitleColor(UIColor.blue, for: .normal)
googleMapsButton.frame = CGRect(x: mapViewWidth - 80, y: mapViewHeight - 70, width: 50, height: 50)
googleMapsButton.addTarget(self, action: #selector(markerClick(sender:)), for: .touchUpInside)
googleMapsButton.gps = String(marker.position.latitude) + "," + String(marker.position.longitude)
googleMapsButton.setTitle(marker.title, for: .normal)
googleMapsButton.tag = 0
let directionsButton = CustomButton()
directionsButton.setTitle("", for: .normal)
directionsButton.setImage(UIImage(named: "googlemapsdirection"), for: .normal)
directionsButton.setTitleColor(UIColor.blue, for: .normal)
directionsButton.frame = CGRect(x: mapViewWidth - 110, y: mapViewHeight - 70, width: 50, height: 50)
directionsButton.addTarget(self, action: #selector(markerClick(sender:)), for: .touchUpInside)
directionsButton.gps = String(marker.position.latitude) + "," + String(marker.position.longitude)
directionsButton.setTitle(marker.title, for: .normal)
directionsButton.tag = 1
self.view.addSubview(googleMapsButton)
self.view.addSubview(directionsButton)
}
return true
}
@objc func markerClick(sender: CustomButton) {
let fullGPS = sender.gps
let fullGPSArr = fullGPS.split(separator: ",")
let lat1 : String = String(fullGPSArr[0])
let lng1 : String = String(fullGPSArr[1])
let latitude = Double(lat1) as! CLLocationDegrees
let longitude = Double(lng1) as! CLLocationDegrees
if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {
if (sender.tag == 1) {
let url = URL(string: "comgooglemaps://?saddr=&daddr=\(latitude),\(longitude)&directionsmode=driving")
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
} else if (sender.tag == 0) {
let url = URL(string:"comgooglemaps://?center=\(latitude),\(longitude)&zoom=14&views=traffic&q=\(latitude),\(longitude)")
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
}
} else {
let regionDistance:CLLocationDistance = 10000
let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
var options = NSObject()
if (sender.tag == 1) {
options = [
MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span),
MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving
] as NSObject
} else if (sender.tag == 0) {
options = [
MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
] as NSObject
}
let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = sender.title(for: .normal)
mapItem.openInMaps(launchOptions: options as? [String : AnyObject])
}
}
class CustomButton: UIButton {
var gps = ""
override func awakeFromNib() {
super.awakeFromNib()
//TODO: Code for our button
}
}
我正在为 android 和 ios 开发应用程序。在 android 地图视图中,当我点击地图的标记时,视图底部会显示 2 个按钮。 (方向和 google 地图)查看上传的图片
按照 google 的说明,我在 swift 中设置了 mapView 以显示 google 的地图。但是当我点击标记时,我在 mapView 中看不到任何按钮。这是为什么?
google 在 swift 中设置地图视图的说明: https://developers.google.com/maps/documentation/ios-sdk/start
我用谷歌搜索了这个,但一无所获。所以我手动做了。通过覆盖 'didTapMarker' 我添加了这两个按钮:
func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) -> Bool {
if marker.title != nil {
let mapViewHeight = mapView.frame.size.height
let mapViewWidth = mapView.frame.size.width
let container = UIView()
container.frame = CGRectMake(mapViewWidth - 100, mapViewHeight - 63, 65, 35)
container.backgroundColor = UIColor.whiteColor()
self.view.addSubview(container)
let googleMapsButton = CustomButton()
googleMapsButton.setTitle("", forState: .Normal)
googleMapsButton.setImage(UIImage(named: "googlemaps"), forState: .Normal)
googleMapsButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
googleMapsButton.frame = CGRectMake(mapViewWidth - 80, mapViewHeight - 70, 50, 50)
googleMapsButton.addTarget(self, action: "markerClick:", forControlEvents: .TouchUpInside)
googleMapsButton.gps = String(marker.position.latitude) + "," + String(marker.position.longitude)
googleMapsButton.title = marker.title
googleMapsButton.tag = 0
let directionsButton = CustomButton()
directionsButton.setTitle("", forState: .Normal)
directionsButton.setImage(UIImage(named: "googlemapsdirection"), forState: .Normal)
directionsButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
directionsButton.frame = CGRectMake(mapViewWidth - 110, mapViewHeight - 70, 50, 50)
directionsButton.addTarget(self, action: "markerClick:", forControlEvents: .TouchUpInside)
directionsButton.gps = String(marker.position.latitude) + "," + String(marker.position.longitude)
directionsButton.title = marker.title
directionsButton.tag = 1
self.view.addSubview(googleMapsButton)
self.view.addSubview(directionsButton)
}
return true
}
func markerClick(sender: CustomButton) {
let fullGPS = sender.gps
let fullGPSArr = fullGPS!.characters.split{[=10=] == ","}.map(String.init)
let lat1 : NSString = fullGPSArr[0]
let lng1 : NSString = fullGPSArr[1]
let latitude:CLLocationDegrees = lat1.doubleValue
let longitude:CLLocationDegrees = lng1.doubleValue
if (UIApplication.sharedApplication().openURL(NSURL(string:"comgooglemaps://")!)) {
if (sender.tag == 1) {
UIApplication.sharedApplication().openURL(NSURL(string:
"comgooglemaps://?saddr=&daddr=\(latitude),\(longitude)&directionsmode=driving")!)
} else if (sender.tag == 0) {
UIApplication.sharedApplication().openURL(NSURL(string:
"comgooglemaps://?center=\(latitude),\(longitude)&zoom=14&views=traffic")!)
}
} else {
let regionDistance:CLLocationDistance = 10000
let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
var options = NSObject()
if (sender.tag == 1) {
options = [
MKLaunchOptionsMapCenterKey: NSValue(MKCoordinate: regionSpan.center),
MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan: regionSpan.span),
MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving
]
} else if (sender.tag == 0) {
options = [
MKLaunchOptionsMapCenterKey: NSValue(MKCoordinate: regionSpan.center),
MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan: regionSpan.span)
]
}
let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = sender.title
mapItem.openInMapsWithLaunchOptions(options as? [String : AnyObject])
}
}
在 mapView 的右下角添加了两个按钮,点击它们后,将打开 googleMaps 应用程序(如果存在)。感谢您投反对票:(
自定义按钮 class:
class CustomButton: UIButton {
var gps = ""
override func awakeFromNib() {
super.awakeFromNib()
//TODO: Code for our button
}
}
我遇到了同样的问题,但是在 Objective-c 中并且按照我已经实现的代码执行。 对于那些想在点击标记图标后使用 Google 地图添加方向图标的人,下面的代码对我有用:
-(bool)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker{
UIButton *directionsButton = [[UIButton alloc] init];
[directionsButton setTitle:@"" forState:UIControlStateNormal];
[directionsButton setImage:[UIImage imageNamed:@"ic_google_direction"] forState:UIControlStateNormal];
[directionsButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[directionsButton setFrame:CGRectMake(self->mapView.frame.size.width - 110, self->mapView.frame.size.height - 130, 50, 50)];
[directionsButton addTarget:self action:@selector(markerClick) forControlEvents:UIControlEventTouchUpInside];
[directionsButton setTag:1];
[[self view] addSubview:directionsButton];
return NO;
}
-(void) markerClick{
NSMutableArray *installedNavigationApps = [[NSMutableArray alloc] initWithObjects:@"Apple Maps", nil];
if ([[UIApplication sharedApplication] canOpenURL:
[NSURL URLWithString:@"comgooglemaps://"]]) {
[installedNavigationApps addObject:@"Google Maps"];
}
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"Selecione um aplicativo" message:@"Abrir com" preferredStyle:UIAlertControllerStyleActionSheet];
for (NSString *app in installedNavigationApps) {
if([app isEqualToString:@"Apple Maps"]){
[actionSheet addAction:[UIAlertAction actionWithTitle:app style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[[UIApplication sharedApplication] openURL:
[NSURL URLWithString: [NSString stringWithFormat:@"http://maps.apple.com/?saddr=%@,%@&daddr=%@,%@", _latitude, _longitude, _denuncia.Latitude, _denuncia.Longitude]]];
}]];
}
else if([app isEqualToString:@"Google Maps"]){
[actionSheet addAction:[UIAlertAction actionWithTitle:app style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[[UIApplication sharedApplication] openURL:
[NSURL URLWithString: [NSString stringWithFormat:@"comgooglemaps://?saddr=%@,%@&daddr=%@,%@&zoom=14&directionsmode=driving", _latitude, _longitude, _denuncia.Latitude, _denuncia.Longitude]]];
}]];
}
}
[actionSheet addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
// Present action sheet.
[self presentViewController:actionSheet animated:YES completion:nil];
}
我使用 swift 版本 3 的代码。我更改了包含视图,我将 UIImageView 的子视图添加到 containView。
在 google 地图上添加两个按钮。我在顶部使用锂代码。
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
if marker.title != nil {
let mapViewHeight = mapView.frame.size.height
let mapViewWidth = mapView.frame.size.width
let container = UIView()
container.frame = CGRect.init(x: mapViewWidth - 100, y: mapViewHeight - 63, width: 65, height: 35)
container.backgroundColor = UIColor.white
self.view.addSubview(container)
let googleMapsButton = CustomButton()
googleMapsButton.setTitle("", for: .normal)
googleMapsButton.setImage(UIImage(named: "googlemaps")?.resizableImage(withCapInsets: UIEdgeInsets.init(top: 0, left: 0, bottom: 50, right: 50)),for: .normal)
googleMapsButton.setTitleColor(UIColor.blue, for: .normal)
googleMapsButton.frame = CGRect.init(x: mapViewWidth - 80, y: mapViewHeight - 70, width: 50, height: 50)
googleMapsButton.addTarget(self, action: #selector(self.markerClick(sender:)), for: .touchUpInside)
googleMapsButton.gps = String(marker.position.latitude) + "," + String(marker.position.longitude)
googleMapsButton.title = marker.title
googleMapsButton.tag = 0
let directionsButton = CustomButton()
directionsButton.setTitle("", for: .normal)
directionsButton.setImage(UIImage(named: "googlemapsdirection")?.resizableImage(withCapInsets: UIEdgeInsets.init(top: 0, left: 0, bottom: 50, right: 50)),for: .normal)
directionsButton.setTitleColor(UIColor.blue, for: .normal)
directionsButton.frame = CGRect.init(x:mapViewWidth - 110, y:mapViewHeight - 70, width:50, height: 50)
directionsButton.addTarget(self, action: #selector(self.markerClick(sender:)), for: .touchUpInside)
directionsButton.gps = String(marker.position.latitude) + "," + String(marker.position.longitude)
directionsButton.title = marker.title
directionsButton.tag = 1
self.view.addSubview(googleMapsButton)
self.view.addSubview(directionsButton)
}
return false
}
func markerClick(sender:CustomButton){
let fullGPS = sender.gps
let fullGPSArr = fullGPS!.characters.split{[=10=] == ","}.map(String.init)
let lat1 : String = fullGPSArr[0]
let lng1 : String = fullGPSArr[1]
guard let lat1_double = Double(lat1),let lng1_double = Double(lng1) else{
return
}
let latitude:CLLocationDegrees = lat1_double
let longitude:CLLocationDegrees = lng1_double
if (UIApplication.shared.openURL(URL(string:"comgooglemaps://")!)) {
if (sender.tag == 1) {
UIApplication.shared.openURL(URL(string:
"comgooglemaps://?saddr=&daddr=\(latitude),\(longitude)&directionsmode=driving")!)
} else if (sender.tag == 0) {
UIApplication.shared.openURL(URL(string:
"comgooglemaps://?center=\(latitude),\(longitude)&zoom=14&views=traffic")!)
}
} else {
let regionDistance:CLLocationDistance = 10000
let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
var options : NSDictionary! = nil
if (sender.tag == 1) {
options = [
MKLaunchOptionsMapCenterKey: NSValue.init(mkCoordinate: regionSpan.center),
MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span),
MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving
]
} else if (sender.tag == 0) {
options = [
MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
]
}
let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = sender.title
mapItem.openInMaps(launchOptions: options as? [String : Any])
}
}
自定义按钮 - swift 只需将 class 添加到 viewcontroller 底部
class CustomButton: UIButton {
var gps = ""
override func awakeFromNib() {
super.awakeFromNib()
//TODO: Code for our button
}
}
请求 "appleMap" 需要导入 MapKit
SWIFT 4.2
根据@Lithium 的回答,我更新了与 Swift 4.2:
兼容的代码func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
if marker.title != nil {
let mapViewHeight = mapView.frame.size.height
let mapViewWidth = mapView.frame.size.width
let container = UIView()
container.frame = CGRect(x: mapViewWidth - 100, y: mapViewHeight - 63, width: 65, height: 35)
container.backgroundColor = UIColor.white
self.view.addSubview(container)
let googleMapsButton = CustomButton()
googleMapsButton.setTitle("", for: .normal)
googleMapsButton.setImage(UIImage(named: "googlemaps"), for: .normal)
googleMapsButton.setTitleColor(UIColor.blue, for: .normal)
googleMapsButton.frame = CGRect(x: mapViewWidth - 80, y: mapViewHeight - 70, width: 50, height: 50)
googleMapsButton.addTarget(self, action: #selector(markerClick(sender:)), for: .touchUpInside)
googleMapsButton.gps = String(marker.position.latitude) + "," + String(marker.position.longitude)
googleMapsButton.setTitle(marker.title, for: .normal)
googleMapsButton.tag = 0
let directionsButton = CustomButton()
directionsButton.setTitle("", for: .normal)
directionsButton.setImage(UIImage(named: "googlemapsdirection"), for: .normal)
directionsButton.setTitleColor(UIColor.blue, for: .normal)
directionsButton.frame = CGRect(x: mapViewWidth - 110, y: mapViewHeight - 70, width: 50, height: 50)
directionsButton.addTarget(self, action: #selector(markerClick(sender:)), for: .touchUpInside)
directionsButton.gps = String(marker.position.latitude) + "," + String(marker.position.longitude)
directionsButton.setTitle(marker.title, for: .normal)
directionsButton.tag = 1
self.view.addSubview(googleMapsButton)
self.view.addSubview(directionsButton)
}
return true
}
@objc func markerClick(sender: CustomButton) {
let fullGPS = sender.gps
let fullGPSArr = fullGPS.split(separator: ",")
let lat1 : String = String(fullGPSArr[0])
let lng1 : String = String(fullGPSArr[1])
let latitude = Double(lat1) as! CLLocationDegrees
let longitude = Double(lng1) as! CLLocationDegrees
if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {
if (sender.tag == 1) {
let url = URL(string: "comgooglemaps://?saddr=&daddr=\(latitude),\(longitude)&directionsmode=driving")
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
} else if (sender.tag == 0) {
let url = URL(string:"comgooglemaps://?center=\(latitude),\(longitude)&zoom=14&views=traffic&q=\(latitude),\(longitude)")
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
}
} else {
let regionDistance:CLLocationDistance = 10000
let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
var options = NSObject()
if (sender.tag == 1) {
options = [
MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span),
MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving
] as NSObject
} else if (sender.tag == 0) {
options = [
MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
] as NSObject
}
let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = sender.title(for: .normal)
mapItem.openInMaps(launchOptions: options as? [String : AnyObject])
}
}
class CustomButton: UIButton {
var gps = ""
override func awakeFromNib() {
super.awakeFromNib()
//TODO: Code for our button
}
}