根据地点转坐标 再用坐标定位,应为坐标格式需要转火星文,火星文使用的百度坐标,所以只有百度地图准确度较高,其他地图有偏差,
建议根据地点昵称去跳转地图,经测试根据地点跳转地图所有地图都是比较精准的 😄
- (void)viewDidLoad {
[super viewDidLoad];
NSString*address =@"北京市朝阳区高和蓝峰大厦A座";
CLGeocoder*myGeocoder = [[CLGeocoderalloc]init];
[myGeocodergeocodeAddressString:addresscompletionHandler:^(NSArray *_Nullableplacemarks,NSError*_Nullableerror) {
if([placemarkscount] >0&& error ==nil) {
NSLog(@"Found %lu placemark(s).",(unsignedlong)[placemarkscount]);
CLPlacemark*firstPlacemark = [placemarksobjectAtIndex:0];
self.desCoordinate= [JZLocationConvertergcj02ToBd09:firstPlacemark.location.coordinate];
NSLog(@"longitude:%f",self.desCoordinate.longitude);
NSLog(@"latitude:%f",self.desCoordinate.latitude);
}elseif([placemarkscount] ==0&& error ==nil){
NSLog(@"Found no placemarks.");
}elseif(error !=nil){
NSLog(@"An error occurred = %@",error);
}
}];
NSString*urlScheme =@"MapJump://";
NSString*appName =@"MapJump";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选择地图" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//这个判断其实是不需要的
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com/"]]) {
UIAlertAction*action = [UIAlertActionactionWithTitle:@"苹果地图"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*action) {
MKMapItem*currentLocation = [MKMapItemmapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:self.desCoordinate addressDictionary:nil]];
toLocation.name=@"终点";//可传入目标地点名称
[MKMapItem openMapsWithItems:@[currentLocation, toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
}];
[alertaddAction:action];
}
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
UIAlertAction*action = [UIAlertActionactionWithTitle:@"百度地图"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*action) {
//我的位置代表起点位置为当前位置,也可以输入其他位置作为起点位置,如天安门
NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=%f,%f&mode=driving&src=JumpMapDemo", self.desCoordinate.latitude, self.desCoordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",urlString);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}];
[alertaddAction:action];
}
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
//coordinate = CLLocationCoordinate2DMake(40.057023, 116.307852);
UIAlertAction*action = [UIAlertActionactionWithTitle:@"高德地图"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*action) {
NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=applicationName&sid=BGVIS1&sname=%@&did=BGVIS2&dlat=%f&dlon=%f&dev=0&m=0&t=0",@"我的位置",self.desCoordinate.latitude, self.desCoordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//@"我的位置"可替换为@"终点名称"
NSLog(@"%@",urlString);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}];
[alertaddAction:action];
}
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
UIAlertAction*action = [UIAlertActionactionWithTitle:@"谷歌地图"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*action) {
NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",appName,urlScheme,self.desCoordinate.latitude, self.desCoordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",urlString);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}];
[alertaddAction:action];
}
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
UIAlertAction*action = [UIAlertActionactionWithTitle:@"腾讯地图"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*action) {
NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?type=drive&from=我的位置&to=%@&tocoord=%f,%f&policy=1&referer=%@", @"终点名称", self.desCoordinate.latitude, self.desCoordinate.longitude, appName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",urlString);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}];
[alertaddAction:action];
}
UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertaddAction:action];
[self presentViewController:alert animated:YES completion:^{
}];
}
根据地点昵称去跳转地图:
- (void) setServiceListPushMapActionModel
{
NSString* tmpAddressStr =@"北京市朝阳区高和蓝峰大厦A座";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选择地图" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com/"]])
{
UIAlertAction*action = [UIAlertActionactionWithTitle:@"苹果地图"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*action) {
//根据地址定位
NSString *urlString = [[NSString stringWithFormat:@"http://maps.apple.com/?saddr=我的位置&daddr=%@&address =%@",tmpAddressStr,tmpAddressStr] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}];
[alertaddAction:action];
}
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]])
{
UIAlertAction*action1 = [UIAlertActionactionWithTitle:@"百度地图"style:UIAlertActionStyleDefaulthandler:^(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]];
}];
[alertaddAction:action1];
}
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]])
{
UIAlertAction*action2 = [UIAlertActionactionWithTitle:@"高德地图"style:UIAlertActionStyleDefaulthandler:^(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]];
}];
[alertaddAction:action2];
}
UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertaddAction:action3];
[self presentViewController:alert animated:YES completion:nil];
}