Note: The code under this article has an unknown number of variables, so it is not possible to run directly, I also transfer others
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
[CPP]View Plaincopy
- 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] 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:nsutf8stringencoding];
- 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:@{mklaunchoptionsdirectionsmodekey: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
[CPP]View Plaincopy
- -(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, Endcoor.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 *urlstring = [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
[CPP]View Plaincopy
- [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
[CPP]View Plaincopy
- -(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 = [[NSString 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:nsutf8stringencoding];
- Nsurl *aurl = [Nsurl urlwithstring:urlstring];
- [[UIApplication sharedapplication] openurl:aurl];
- } else{//Direct call to iOS own Apple map
- Mkmapitem *currentlocation = [Mkmapitem mapitemforcurrentlocation];
- Mkplacemark *placemark = [[Mkplacemark alloc] Initwithcoordinate:endcoor Addressdictionary:nil];
- Mkmapitem *tolocation = [[Mkmapitem alloc] initwithplacemark:placemark];
- Tolocation.name = @"to name";
- [Mkmapitem openmapswithitems:@[currentlocation, Tolocation]
- Launchoptions:@{mklaunchoptionsdirectionsmodekey: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];
- }
- }
- Original: http://blog.csdn.net/u011011341/article/details/9233049#
IOS Call Map Navigation