iOS 根据地址跳转地图:
Swift:
func setServiceListPushMapActionModel() {
let tmpAddressStr = "目的地地址"
print("===== 跳转地址======>",tmpAddressStr)
let alert : UIAlertController = UIAlertController.init(title: "选择地图", message: nil, preferredStyle: .actionSheet)
//苹果地图
if UIApplication.shared.canOpenURL(NSURL(string: "http://maps.apple.com/")! as URL) {
let iosMapAction : UIAlertAction = UIAlertAction.init(title: "苹果地图", style: .default) { (iosMapAlert) in
let urlStr = String(format: "http://maps.apple.com/?saddr=我的位置&daddr=%@&address =%@",tmpAddressStr,tmpAddressStr)
let url = NSURL(string: urlStr.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!)
UIApplication.shared.open(url! as URL)
}
alert.addAction(iosMapAction)
}
//百度地图
if UIApplication.shared.canOpenURL(NSURL(string: "baidumap://")! as URL) {
let baiduMapAction : UIAlertAction = UIAlertAction.init(title: "百度地图", style: .default) { (baiduMapAlert) in
let urlStr = String(format: "baidumap://map/direction?origin={{我的位置}}&destination=%@&coord_type=bd09ll&mode=driving&src=ios.demo",tmpAddressStr)
let url = NSURL(string: urlStr.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!)
UIApplication.shared.open(url! as URL)
}
alert.addAction(baiduMapAction)
}
//高德地图
if UIApplication.shared.canOpenURL(NSURL(string: "iosamap://")! as URL) {
let gaodeMapAction : UIAlertAction = UIAlertAction.init(title: "高德地图", style: .default) { (gaodeMapAlert) in
let urlStr = String(format: "iosamap://path?sourceApplication=applicationName&sid=BGVIS1&sname=%@&did=BGVIS2&dname=%@&dev=0&t=0","我的位置",tmpAddressStr)
let url = NSURL(string: urlStr.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!)
UIApplication.shared.open(url! as URL)
}
alert.addAction(gaodeMapAction)
}
let cancelAction : UIAlertAction = UIAlertAction.init(title: "取消", style: .cancel, handler: nil)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)
}
OC:
- (void) setServiceListPushMapActionModel
{
NSString * tmpAddressStr = @"目的地址";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选择地图" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com/"]])
{
UIAlertAction *action = [UIAlertAction actionWithTitle:@"苹果地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//根据地址定位
NSString *urlString = [[NSString stringWithFormat:@"http://maps.apple.com/?saddr=我的位置&daddr=%@&address =%@",tmpAddressStr,tmpAddressStr] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}];
[alert addAction:action];
}
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]])
{
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//根据地址导航
NSString * urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=%@&coord_type=bd09ll&mode=driving&src=ios.demo",tmpAddressStr] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}];
[alert addAction:action1];
}
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]])
{
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//根据地址定位
NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=applicationName&sid=BGVIS1&sname=%@&did=BGVIS2&dname=%@&dev=0&t=0",@"我的位置",tmpAddressStr] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}];
[alert addAction:action2];
}
UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:action3];
[self presentViewController:alert animated:YES completion:nil];
}