根據(jù)地點(diǎn)轉(zhuǎn)坐標(biāo) 再用坐標(biāo)定位,應(yīng)為坐標(biāo)格式需要轉(zhuǎn)火星文,火星文使用的百度坐標(biāo),所以只有百度地圖準(zhǔn)確度較高,其他地圖有偏差,
建議根據(jù)地點(diǎn)昵稱去跳轉(zhuǎn)地圖,經(jīng)測試根據(jù)地點(diǎn)跳轉(zhuǎn)地圖所有地圖都是比較精準(zhǔn)的 ??
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? NSString*address =@"北京市朝陽區(qū)高和藍(lán)峰大廈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];
?? ? ? //這個(gè)判斷其實(shí)是不需要的
?? ? ? 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=@"終點(diǎn)";//可傳入目標(biāo)地點(diǎn)名稱
?? ? ? ? ? ? ? [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) {
?? ? ? ? ? ? ? //我的位置代表起點(diǎn)位置為當(dāng)前位置,也可以輸入其他位置作為起點(diǎn)位置,如天安門
?? ? ? ? ? ? ? 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];//@"我的位置"可替換為@"終點(diǎn)名稱"
?? ? ? ? ? ? ? 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=%@", @"終點(diǎn)名稱", 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:^{
?? ? ? }];
}
根據(jù)地點(diǎn)昵稱去跳轉(zhuǎn)地圖:
- (void) setServiceListPushMapActionModel
{
? ? NSString* tmpAddressStr =@"北京市朝陽區(qū)高和藍(lán)峰大廈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) {
? ? ? ? ? ? //根據(jù)地址定位
? ? ? ? ? ? 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) {
?? ? ? ? //根據(jù)地址導(dǎo)航
?? ? ? ? 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) {
? ? ? ? ? //根據(jù)地址定位
?? ? ? ? ? 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];
}