Solve the problem of Android native positioning

Time:2022-6-19

There are many Android native positioning codes on the Internet, so they will not be posted.

Let’s briefly understand:

GPS_PROVIDER:Through the built-in GPS chip of the mobile phone, the positioning information is obtained by using satellites. Position monitoring and satellite status monitoring consume a lot of power and indoor positioning is very inaccurate.

NETWORK_PROVIDER:Network positioning queries the location information in the positioning data server by using the node ID through the base station and WiFi node. However, the domestic network does not allow it, and it is reported that Google has not provided this service. So it is not available to obtain location information in this way on the Internet, that is, network_ Provider is not available in China.

PASSIVE_PROVIDER:Passive positioning. Other applications such as Baidu and Gaode use positioning. The system will update the positioning information and save it. In this way, the latest location information can be obtained.

The third-party SDK is of course easy to use, but it should be noted that the longitude and latitude obtained are offset and only adapt to your own map. For example, the longitude and latitude obtained with the baidu map SDK are accurate on Baidu map, and there may be some deviation in the display on Gaode map. If the longitude and latitude and the map are not used in a set, the offset should be considered instead of the inaccurate longitude and latitude obtained. Third party SDKs have their own network positioning data. Their network positioning not only depends on server query, but also on a set of algorithms. Therefore, indoor positioning is more accurate.

Some projects do not use the third-party SDK, and can only use the native method to obtain the location. NETWORK_ Provider forced to give up, passive_ Provider can only be auxiliary, GPS_ Provider needs to increase accuracy.

This is how I handled it:

1. addgpsstatlistener monitors satellite status. GPS can be used only when the number of satellites with signal value > 1 is more than 4_ PROVIDER;

2. only when the speed is >0.5 and the accuracy is < 50 can it be considered as valid longitude and latitude data (the equipment in my project is mobile).

Add: reasons for failure to locate using Android’s built-in API

Prelude: it is so troublesome to use the positioning of Gaode Baidu. Why not use the API that comes with Android to locate. Save time and effort?

1. it is easy to use the self-contained positioning, as shown in the following figure

2. inherit locationlistener

3. parse the data if it is obtained

But here comes the problem.

There are two ways for location providers: network or gps

Question:

1. using GPS, no callback is received.

2. use network to always call back onproviderdisabled

Reason: that is, when you are developing indoors, your mobile phone simply cannot obtain location information. You ask the system how to notify your program of location information.

Therefore, to fundamentally solve this problem, we must solve the problem of location information acquisition.

As mentioned just now, only network_ The provider mode is a reliable way of indoor positioning. However, due to the strange network in the mainland and most manufacturers will not use Google services, this positioning mode is useless by default.

What can we do? It’s easy to do. Just find an alternative service provider. Baidu’s location information SDK can solve this problem.

Its basic principle has been mentioned above, which is to collect your WiFi node information and your mobile base station information to locate.

The above is my personal experience. I hope I can give you a reference, and I hope you can support developeppaer. If there is any mistake or not fully considered, please don’t hesitate to comment.