let destLat:Float = ((dictionry["Latitude"] as NSString?)?.floatValue)!
let destLong:Float = ((dictionry["Longitude"] as NSString?)?.floatValue)!
let srcLat:Float = ((latitude as NSString?)?.floatValue)! //Float(sourcLatitude!)
let strLong:Float = ((longitude as NSString?)?.floatValue)!//Float(sourceLongitude!)
let urlString = "comgooglemaps://?saddr=" + "\(srcLat),\(strLong)" + "&daddr=" + "\(destLat),\(destLong)" + "&directionsmode=driving"
if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {
/*UIApplication.shared.openURL(NSURL(string: "comgooglemaps://?saddr=\(Float(12.972442)),\(Float( 77.580643))&daddr=\(Float(27.0238)),\(Float(74.2179))&directionsmode=driving")! as URL)*/
UIApplication.shared.openURL(NSURL(string: urlString)! as URL)
} else {
print("Can't use comgooglemaps://");
HelperManager.errorAlertUpdate(self, title: ShowAlertTitle, message: "Your device does not contain Maps.")
}
最佳答案
你可以这样写:
objective-c :
NSString *directionsURL = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f",39.190269,-84.359380,39.1899,-84.3598];
if ([[UIApplication sharedApplication] respondsToSelector:@selector(openURL:options:completionHandler:)]) {
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: directionsURL] options:@{} completionHandler:^(BOOL success) {}];
} else {
// Fallback on earlier versions
}
} else {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: directionsURL]];
}
图片如下所示:
迅速:
var directionsURL = "http://maps.google.com/maps?saddr=\(39.190269),\(-84.359380)&daddr=\(39.1899),\(-84.3598)"
if UIApplication.shared.responds(to: Selector("openURL:options:completionHandler:")) {
if #available(iOS 10.0, *) {
if let anURL = URL(string: directionsURL) {
UIApplication.shared.open(anURL, options: [:], completionHandler: {(_ success: Bool) -> Void in
})
}
} else {
// Fallback on earlier versions
}
} else {
if let anURL = URL(string: directionsURL) {
UIApplication.shared.openURL(anURL)
}
}
关于ios - 谷歌地图使用 URL 方案,源位置在 map 中正确显示,目标位置未正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49811380/