After IOS6.0 system, compatible with iOS5.0 and iOS6.0 map navigation, need two steps
#define System_version_less_than (v) ([[[Uidevice Currentdevice] systemversion] compare:v options:nsnumericsearch] = = nsorderedascending)//used to get the phone system, judging the system is how much
Cllocationcoordinate2d Startcoor = self.mapView.userLocation.location.coordinate; Cllocationcoordinate2d Endcoor = Cllocationcoordinate2dmake (startcoor.latitude+0.01, startCoor.longitude+0.01); if (System_version_less_than (@ "6.0")) {//IOS6 below, call Google map nsstring *urlstring = [[NSString alloc] Init withformat:@ "Http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirfl=d", Startcoor.latitude, Startcoor.longitude,endcoor.latitude,endcoor.longitude]; @ "Http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f", Startcoor.latitude,startcoor.longitude, Endcoor.latitude,endcoor.longitude urlstring = [URLString stringbyaddingpercentescapesusingencoding:nsutf8stringen Coding]; Nsurl *aurl = [Nsurl urlwithstring:urlstring]; [[UIApplication sharedapplication] openurl:aurl]; } else {//Direct call to iOS own Apple map mkmapitem *currentlocation = [Mkmapitem mapitemforcurrentlocation]; Mkmapitem *tolocation = [[MkmapiteM alloc] Initwithplacemark:[[mkplacemark alloc] Initwithcoordinate:endcoor Addressdictionary:nil]]; Tolocation.name = @ "to name"; [Mkmapitem openmapswithitems:@[currentlocation, Tolocation] Launchoptions:@{mklaunchoptionsdirection Smodekey:mklaunchoptionsdirectionsmodedriving,mklaunchoptionsshowstraffickey: [NSNumber NumberWithBool:YES]}]; }
If you do not want to use Apple's own map, you can also use a third-party map, such as Baidu, Google maps, gold and other
Before use, determine if the app is installed on the device
Baidu Map:
if ([[uiapplicationsharedapplication]canopenurl:[nsurlurlwithstring:@ "baidumap://map/"]])
Reference
German map:
if ([[uiapplicationsharedapplication]canopenurl:[nsurlurlwithstring:@ "iosamap://"]])
Reference
Google Maps:
if ([[uiapplicationsharedapplication]canopenurl:[nsurlurlwithstring:@ "comgooglemaps://"]])
Reference
Sample code
-(void) Availablemapsapps {[Self.availablemaps removeallobjects]; Cllocationcoordinate2d Startcoor = self.mapView.userLocation.location.coordinate; Cllocationcoordinate2d Endcoor = Cllocationcoordinate2dmake (startcoor.latitude+0.01, startCoor.longitude+0.01); NSString *toname = @ "to name"; if ([[[UIApplication sharedapplication] canopenurl:[nsurl urlwithstring:@ "baidumap://map/"]]) {NSString *urlString = [NSString stringwithformat:@] Baidumap://map/direction?origin=latlng:%f,%f|name: My location &destination=latlng:%f,% F|name:%@&mode=transit ", Startcoor.latitude, Startcoor.longitude, Endcoor.latitude, EndC Oor.longitude, ToName]; Nsdictionary *dic = @{@ "name": @ "Baidu map", @ "url": urlstring}; [Self.availablemaps Addobject:dic]; } if ([[UIApplication sharedapplication] canopenurl:[nsurl urlwithstring:@ "iosamap://"]) {nsstring *urlString = [NSString stringwithformat:@ "iosamap://navi?sourceapplication=%@&backscheme=applicationscheme&poiname=fangheng&poiid=bgvis& Lat=%f&lon=%f&dev=0&style=3 ", @" Fairchild Era ", Endcoor.latitude, Endcoor.longitude]; Nsdictionary *dic = @{@ "name": @ "German map", @ "url": urlstring}; [Self.availablemaps Addobject:dic]; } if ([[UIApplication sharedapplication] canopenurl:[nsurl urlwithstring:@ "comgooglemaps://"]) {NSString *urlS Tring = [NSString stringwithformat:@ "Comgooglemaps://?saddr=&daddr=%f,%f¢er=%f,%f&directionsmode=transit" , Endcoor.latitude, Endcoor.longitude, Startcoor.latitude, Startcoor.longitude]; Nsdictionary *dic = @{@ "name": @ "Google Maps" @ "url": urlstring}; [Self.availablemaps Addobject:dic]; }}
Show a Actionsheet
[Self Availablemapsapps]; Uiactionsheet *action = [[Uiactionsheet alloc] init]; [Action addbuttonwithtitle:@ using the system to bring your own map navigation]; For (Nsdictionary *dic in self.availablemaps) { [action addbuttonwithtitle:[nsstring stringwithformat:@ ' use%@ navigation ', dic[@ "name"]]; } [Action addbuttonwithtitle:@ "Cancel"]; Action.cancelbuttonindex = Self.availableMaps.count + 1; Action.delegate = self; [Action ShowInView:self.view];
Implement delegate
-(void) Actionsheet: (Uiactionsheet *) Actionsheet Clickedbuttonatindex: (Nsinteger) Buttonindex {if (ButtonIndex = = 0) { Cllocationcoordinate2d Startcoor = self.mapView.userLocation.location.coordinate; Cllocationcoordinate2d Endcoor = Cllocationcoordinate2dmake (startcoor.latitude+0.01, startCoor.longitude+0.01); if (System_version_less_than (@ "6.0")) {//IOS6 below, call Google map nsstring *urlstring = [[Nsstri] Ng Alloc] initwithformat:@ "Http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirfl=d", Startcoor.latitude,startcoor.longitude,endcoor.latitude,endcoor.longitude]; @ "Http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f", Startcoor.latitude,startcoor.longitude, Endcoor.latitude,endcoor.longitude urlstring = [URLString stringbyaddingpercentescapesusingencoding:nsutf8stri Ngencoding]; Nsurl *aurl = [Nsurl urlwithstring:urlstring]; [[UIApplication sharedapplication] openurl:aurl]; } else{//directly calls the iOS own Apple map mkmapitem *currentlocation = [Mkmapitem mapitemforcurrentloc Ation]; Mkplacemark *placemark = [[Mkplacemark alloc] Initwithcoordinate:endcoor Addressdictionary:nil]; Mkmapitem *tolocation = [[Mkmapitem alloc] initwithplacemark:placemark]; Tolocation.name = @ "to name"; [Mkmapitem openmapswithitems:@[currentlocation, Tolocation] Launchoptions:@{mklaunchoptionsdirec Tionsmodekey:mklaunchoptionsdirectionsmodedriving,mklaunchoptionsshowstraffickey: [NSNumber NumberWithBool:YES]} ]; }}else if (Buttonindex < self.availablemaps.count+1) {nsdictionary *mapdic = Self.availablemaps[buttonindex -1]; NSString *urlstring = mapdic[@ "url"]; URLString = [URLString stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; Nsurl *url = [Nsurl urlwithstring:urlstring]; Debug_log (@ "\n%@\n%@\n%@", mapdic[@ "Name"], mapdic[@ "url"], urlstring); [[UIApplication sharedapplication] openurl:url]; }}
IOS Call Map Navigation