Android gets the latitude and longitude of the location through the native API

In the development of Android, the positioning function is often needed, especially the applications that depend on the geographical location function. This paper introduces that Android obtains the longitude and latitude of the location through the native API and shares it with you

1、 Introduction to difficulties

1. Difficulties

A new function of our application is to return the user's longitude and latitude to the server through an interface after the user opens the attached person page, so that the server can locate accurately.

Because only a small function is added, the introduction of third-party SDK positioning is somewhat overqualified, so we are going to complete it with the help of the native API: locationmanager. After a search on the network, there are many about obtaining longitude and latitude coordinates through locationmanager. All the codes are almost the same. With a responsible attitude, I read many articles, and then summarized a relatively excellent tool class for obtaining longitude and latitude.

The following problems were encountered during the verification:

① Permission problem: after Android 6.0, dynamic permission is added, and the permission to obtain latitude and longitude coordinates needs to be obtained dynamically if your app's (targetversion > = 23) (of course, it has to be set in the manifest file)

② Google's network service is banned in China, so it will lead to the unavailability of network positioning in China. Only GPS positioning can be used, and GPS positioning needs users to open GPS signs to get it

One of the most difficult problems for me to solve is the second problem. Because I have tried many times, I can only get the location by turning on GPS. Finally, after asking all the great gods, they all said that it was because Google's network services were banned in China.

Fortunately, our application is facing overseas users, so there is no case that Google services are blocked.

The above are the problems I encountered in the process. If you solve your confusion, you will be very happy.

For Android 6.0 dynamic permissions, just write the corresponding callback, which is not very difficult.

Here's the code of my tool class:

Usage:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>