Configuring iOS to Launch Third-Party Map Applications
To enable your iOS application to open various map services, follow these essential steps:
1. Whitelist Map Application Schemes
Add the following URL schemes to your Info.plist file under LSApplicationQueriesSchemes:
- Baidu Maps: baidumap
- AutoNavi Maps: iosamap
- Tencent Maps: qqmap
- Apple Maps: http://maps.apple.com/
2. Verify Map Application Availability
Before attempting to launch a map app, check if it's installed on the device:
// Check if Baidu Maps is available
NSURL * baiduMapUrl = [NSURL URLWithString:@"baidumap://"];
if ([[UIApplication sharedApplication] canOpenURL:baiduMapUrl]) {
// Baidu Maps is available
}
// Check if AutoNavi Maps is available
NSURL * autoNaviUrl = [NSURL URLWithString:@"iosamap://"];
if ([[UIApplication sharedApplication] canOpenURL:autoNaviUrl]) {
// AutoNavi Maps is available
}
// Check if Apple Maps is available
NSURL * appleMapsUrl = [NSURL URLWithString:@"http://maps.apple.com/"];
if ([[UIApplication sharedApplication] canOpenURL:appleMapsUrl]) {
// Apple Maps is available
}
3. Launch Navigation with Coordinates
Baidu Maps
NSURL * baiduMapUrl = [NSURL URLWithString:@"baidumap://"];
if ([[UIApplication sharedApplication] canOpenURL:baiduMapUrl]) {
// Use bd09ll coordinate system for Baidu Maps
NSString *navigationUrl = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{Current Location}}&destination=latlng:%@,%@|name:%@&coord_type=bd09ll&mode=driving&src=com.yourapp.id",latitude,longitude,address] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:navigationUrl] options:@{} completionHandler:nil];
}
AutoNavi Maps
NSURL * autoNaviUrl = [NSURL URLWithString:@"iosamap://"];
if ([[UIApplication sharedApplication] canOpenURL:autoNaviUrl]) {
// Convert BD-09 coordinate system to GCJ-02
CLLocationCoordinate2D convertedLocation = [CoordinateConverter bd09ToGcj02:CLLocationCoordinate2DMake([latitude doubleValue], [longitude doubleValue])];
NSString *navigationUrl = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=com.yourapp.id&dlat=%f&dlon=%f&dname=%@&style=2&dev=0",convertedLocation.latitude,convertedLocation.longitude,address] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:navigationUrl] options:@{} completionHandler:nil];
}
Tencent Maps
- (void)launchTencentMaps {
CLLocationCoordinate2D gcj02Coord = CLLocationCoordinate2DMake(latitude, longitude);
float destinationLat = gcj02Coord.latitude;
float destinationLng = gcj02Coord.longitude;
NSString *navigationUrl = [NSString stringWithFormat:@"qqmap://map/routeplan?type=bus&fromcoord=%f,%f&from=Current Location&referer=YOUR_APP_KEY",self.currentLocation.coordinate.latitude, self.currentLocation.coordinate.longitude];
navigationUrl = [NSString stringWithFormat:@"%@&tocoord=%f,%f&to=%@",navigationUrl, destinationLat, destinationLng, destinationName];
navigationUrl = [navigationUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:navigationUrl] options:@{} completionHandler:^(BOOL success) {
// Handle completion
}];
}
Apple Maps
NSURL * appleMapsUrl = [NSURL URLWithString:@"http://maps.apple.com/"];
if ([[UIApplication sharedApplication] canOpenURL:appleMapsUrl]) {
// Convert coordinates if necessary
CLLocationCoordinate2D location = [CoordinateConverter bd09ToGcj02:CLLocationCoordinate2DMake([latitude doubleValue], [longitude doubleValue])];
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
MKMapItem *destination = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:location addressDictionary:nil]];
destination.name = address;
[MKMapItem openMapsWithItems:@[currentLocation,destination] launchOptions:@{
MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsShowsTrafficKey: @YES
}];
}
Android Implementation
if (platform == ANDROID_PLATFORM) {
switch(mapService) {
case TENCENT_MAP:
url = 'qqmap://map/routeplan?type=drive&from=&fromcoord=&to='+ destinationName +'&tocoord=' + lat + ',' + lng + '&policy=0&referer=YOUR_APP_KEY';
break;
case AUTONAVI_MAP:
url = 'androidamap://route?sourceApplication=app_name&slat=&slon=&sname=&dlat='+ lat + '&dlon='+ lng+'&dname='+destinationName+'&dev=0&t=2';
break;
case BAIDU_MAP:
url = 'bdapp://map/direction?destination='+lat+','+lng+'&coord_type=gcj02&mode=driving&src=com.your.package.name';
break;
default:
break;
}
// Launch the URL
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
H5 Implementation
switch(mapService) {
case TENCENT_MAP:
url = 'https://apis.map.qq.com/uri/v1/marker?marker=coord:' + lat + ',' + lng +';addr:'+ address +';title:'+ destinationName + '&referer=YOUR_KEY';
break;
case AUTONAVI_MAP:
url = 'https://uri.amap.com/marker?position='+ lng + ',' + lat +'&name='+ address +'&callnative=1';
break;
case BAIDU_MAP:
url = 'http://api.map.baidu.com/marker?location=' + lat + ',' + lng +'&title='+ destinationName +'&content='+ address +'&output=html&src=webapp.yourapp.name&coord_type=gcj02';
break;
default:
break;
}
// Open the URL in a web view or system browser
window.open(url, '_system');