Code practice of map navigation using URI jump method

Code practice of map navigation using URI jump method

[[145108]]

Preface

I mentioned before that I am working on a location-based application. Recently, we need to make a location navigation function that allows users to navigate from their current location to a specified destination (by default, navigation is by driving).

Navigation methods on mobile phones are divided into in-app navigation and out-of-app navigation

  • In-app navigation refers to using the SDK provided by the map service (such as Amap, Baidu, etc.) to directly embed the navigation function into our own APP. However, I personally do not like this solution because it takes a certain amount of time to access and increases the memory usage of the APP.
  • The out-of-app navigation is done by URI jump (in iOS, it is URL Scheme) directly jumping to the corresponding map APP and directly using the other party's functions to navigate. The advantages of this are that it is easy to access and it does not increase the cost of your own APP. The disadvantage is that if the user does not have this map application installed, he will not be able to use the map service.

Speaking of in-app navigation, I was badly cheated by Tuba back then. Two years ago, when Amap and Baidu had not yet launched navigation SDKs, it seemed that only Tuba had an in-app navigation SDK on the market, so I had to use Tuba SDK. Today, Tuba SDK is still the most difficult map SDK to use in my mind (By the way, the design of Baidu's SDK and Tuba SDK seems to be in the same vein. I wonder if Baidu poached a large number of people from Tuba when it was making maps?). Moreover, this difficult SDK is still charged.

Today I am going to talk about the second method. Because the information on the Internet is not very comprehensive, I will summarize the research results on this method today.

Research

Let's first look at what we want to achieve. When we click on the navigation, the following selection list will pop up.

Of course, if a map APP is not installed, the corresponding option will not appear. To check whether the APP is installed, just call the following method.

  1. [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@ "appurlscheme://" ]

I won’t introduce the URL Scheme of APP here, you can study it on your own.

So the four map applications mentioned in the figure above are

  1. Apple Maps
  2. Baidu Map
  3. Amap
  4. Google Maps

These are the most commonly used maps at present (what do you mean, there is also Tencent Maps? Unfortunately, Tencent Maps does not support opening with URIs yet, so I will add it when it is available)

Let's compare several maps.

map URL Scheme document Is it possible to jump back to the APP?
Apple Maps document no
Baidu Map baidumap:// document no
Amap iosamap:// document yes
Google Maps comgooglemaps:// document yes

Apple Maps is built-in (and the best way to open Apple Maps is not to use URI), so it can be opened without URL Scheme. Secondly, it is a good experience to be able to jump back after jumping to the map APP (refer to WeChat jump). Unfortunately, neither Apple Maps nor Baidu Maps support jumping back.

Next, let's get back to the topic and talk about the jump method of each map.

Assume we have a specified destination coordinate coordinate and our own APP URL Scheme is urlScheme name is appName

  1. CLLocationCoordinate2D coordinate;
  2. NSString *urlScheme;
  3. NSString *appName;

Apple Maps

Apple Maps can be opened via openURL

  1. NSString *urlString = [[NSString stringWithFormat:@ "http://maps.apple.com/?daddr=%f,%f&saddr=slat,slng" ,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
  2.  
  3. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];

However, this method cannot use the current location as the starting point, so it does not meet our requirements. It is said that the following method can be used online, but I did not succeed.

  1. NSString *urlString = [[NSString stringWithFormat:@ "http://maps.apple.com/?daddr=%f,%f&saddr=Current+Location" ,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

But Apple provides another way to use MKMapItem

  1. MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
  2. MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]];
  3.                                               
  4. [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
  5. launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,
  6. MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];

The effect is as follows

Baidu Map

  1. NSString *urlString = [[NSString stringWithFormat:@ "baidumap://map/direction?origin={{my location}}&destination=latlng:%f,%f|name=destination&mode=driving&coord_type=gcj02" ,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  2.                                               
  3. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];

A few things to note

  1. origin=
    This cannot be modified, otherwise the starting position cannot be set to the current position
  2. destination=latlng:%f,%f|name=destination
    name=XXXX The name field cannot be omitted, otherwise the navigation will fail. The text behind it can be filled in at will.
  3. coord_type=gcj02
    The allowed values ​​of coord_type are bd09ll, gcj02, and wgs84. If your APP uses Baidu Map SDK, please fill in bd09ll. Otherwise, fill in gcj02. You basically can't use wgs84 (I won't talk much about map encryption here, please learn it yourself)

The effect is as follows

#p#

Amap

  1. NSString *urlString = [[NSString stringWithFormat:@ "iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2" ,appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  2.  
  3. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];

A few things to note

  1. sourceApplication=%@&backScheme=%@
    sourceApplication represents the name of your own APP, which will be displayed when you jump back later, so you must fill in backScheme is the URL Scheme of your APP. If you don't fill it in, you won't be able to jump back.
  2. dev=0
    Just fill in 0 here. It has the same meaning as gcj02 above. 1 represents wgs84 and is not used.

The effect is as follows

After exiting navigation, you will be prompted whether to jump back to the APP

Google Maps

  1. NSString *urlString = [[NSString stringWithFormat:@ "comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving" ,appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  2.  
  3. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];

A few things to note

  1. x-source=%@&x-success=%@
    Like Amap, these represent the name of the APP and the URL Scheme respectively.
  2. saddr=
    Leaving this blank means triggering from the current position

The effect is as follows: When there are multiple routes, Google Maps will let you choose one of them

After selecting, you will enter the navigation page

Tencent Maps

Since we mentioned Tencent Maps, let's talk about the URIs that can be called from the Internet and official documents.

  1. NSString *urlString = [[NSString stringWithFormat:@ "qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&tocoord=%f,%f&coord_type=1&policy=0" ,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  2.  
  3. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];

But unfortunately, an error occurred after the call and I cannot navigate

The effect is as follows

summary

The demo in this article can be found here

Relatively speaking, Amap is more attentive, after all, it is also a service provider for Apple. Baidu is relatively inferior. Google is still unusable without a VPN. As for Apple's own map, I won't say much about it. The function is still too simple.

Here we just use the simplest way to call the navigation function. In fact, there are still many parameters and functions of each map that have not been used. Students who need to know can find detailed descriptions in the document link at the beginning of the article.

<<:  Debugging tips you may not know

>>:  Why do most programmers' side projects die?

Recommend

How to promote video account? How to attract traffic and promote video accounts?

Since the launch of the WeChat short video functi...

Is the moon actually black?

Lunar soil refers to the soil unique to the moon....

Talk about what KOLs create in community operations

With the rapid development of mobile Internet , c...

Comprehensive understanding of Mobile Backend as a Service (MBaaS)

【51CTO Translation】What if you could build a comp...

Life in space is full of fun!

How do astronauts live in space? Do they enjoy th...

The more stable iOS 12 public beta is here, and even my 5s wants to upgrade

[[234979]] The new iOS 12 and macOS were released...

Dragon Boat Festival Marketing Activities Guide!

Holidays have always been important marketing nod...

The basic logic of Youpengpule's entry into the children's content market

Upengpule, the interactive TV cloud service provi...

Original touch transmission: TCL Momoda

Many people are curious about the name of the phon...

How to become an operations expert, user growth system

Let’s first understand the difference between use...