Welcome to pay attention to the public number: good code farmers

Call a third party map for navigation in iOS apps. This is enough. All your questions can be answered here

The third party map is called in three steps

1, configuration, relative to the map app LSApplicationQueriesSchemes white list

  • Baidumap: Baidumap
  • Amap: iosamap

2. Determine whether third-party maps can be opened

- (BOOL) canOpenURL: (NSURL *) url API_AVAILABLE (ios (3.0));Copy the code

This method can judge whether can open the corresponding map Such as to adjust the baidu map, gold or apple map, you will need to first determine whether can open (if you are in the first step configuration white list didn’t fill in the wrong, so can’t open the mobile phone is not installed related applications), if you can’t open the prompt the user to download the application first

NSURL * baidu_App = [NSURL URLWithString:@"baidumap://"]; If ([[UIApplication sharedApplication] canOpenURL:baidu_App]) {} NSURL * gaode_App = [NSURL URLWithString:@"iosamap://"]; If ([UIApplication sharedApplication] canOpenURL:gaode_App]) {} NSURL * apple_App = [NSURL URLWithString:@"http://maps.apple.com/"]; if ([[UIApplication sharedApplication] canOpenURL:apple_App]) { [alert addAction:appleAction]; }Copy the code

3. Map navigation by longitude and latitude

  • Baidu map
NSURL * baidu_App = [NSURL URLWithString:@"baidumap://"]; If ([[UIApplication sharedApplication] canOpenURL:baidu_App]) {// If ([[UIApplication sharedApplication] canOpenURL:baidu_App]) StringWithFormat: @ "baidumap: / / map/direction? Origin = {} {my position} & destination = latlng: % @, % @ | name: % @ & coord_type = bd09ll & mode = driv ing&src=ios.blackfish.XHY",Latitude,Longitude,adderss] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:bdString] options:@{} completionHandler:nil]; }Copy the code

So here we have the latitude and longitude and the destination, and here we have to pay attention to the coordinate system. By default, Baidu Map uses baidu coordinate system BD-09, while Autonavi and Apple Map are GCJ-02.

At present, there are three main coordinate system types in China: WGS84, GCJ02 and BD09.

WGS84 is a geodetic coordinate system, which is also used in the GPS global positioning system. GCJ02: is the coordinate system of geographic information system formulated by The State Bureau of Surveying and Mapping of China, and is the coordinate system encrypted by WGS84 coordinate system; BD09: Baidu coordinate system, encrypted again based on GCJ02 coordinate system. BD09ll represents latitude and longitude coordinates of Baidu and BD09mc represents Mercator metric coordinates of Baidu.

So if you’re using frame GCJ02 then you need to specify coord_type equals GCJ02 when you’re calling up the scale. Because I’m using baidu coordinates, my code above says coord_type=bd09ll. If it’s WGS84 then coord_type= WGS84. Some other parameters we can choose according to the screenshot below, more detailed content can enter Baidu Map to view

  • Scott map
NSURL * gaode_App = [NSURL URLWithString:@"iosamap://"]; If ([UIApplication sharedApplication] canOpenURL:gaode_App]) {// Convert the COORDINATE system BD-09 to the COORDINATE system CLlocation2D location of GCJ-02 = [JZLocationConverter bd09ToGcj02:CLLocationCoordinate2DMake([Latitude doubleValue], [Longitude doubleValue])]; gdString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=ios.blackfish.XHY&dlat=%f&dlon=%f&dname=%@&style=2&dev=0",latitude,l ongitude,adderss] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; // Start with the location and then manually click the navigation}Copy the code

If your coordinate system is GCJ02, you can comment out the code of converting coordinate system in my code above. Since I am using Baidu coordinate system BD09, I need to convert the coordinate system before setting up Amap. God has written the coordinate system conversion tool, you can go to download github.com/JackZhouCn/… In baidu Map and Autonavi open documents also have coordinate conversion API. The corresponding parameters can be seen in the screenshot below. For more details, you can see Amap

The dev parameter needs to be mentioned. If you pass 0, you pass GCJ02. If you pass WGS84, you pass 1.

  • Apple map
NSURL * apple_App = [NSURL URLWithString:@"http://maps.apple.com/"]; If ([[UIApplication sharedApplication] canOpenURL:apple_App]) {// Convert coordinate system BD-09 to COORDINATE system GCJ-02 cllocation2D location = [JZLocationConverter bd09ToGcj02:CLLocationCoordinate2DMake([Latitude doubleValue], [Longitude doubleValue])]; MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation]; MKMapItem *tolocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:location addressDictionary:nil]]; tolocation.name = adderss; [MKMapItem openMapsWithItems:@[currentLocation,tolocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}]; }Copy the code

Call Apple Map also need to pay attention to the problem of coordinate system conversion, if it is GCJ02 coordinate system can comment out the code I transformed above. In the above code Latitude is Latitude, Longitude is Longitude, and adderss is destination