Positioning on Android

Source: Internet
Author: User

Recently, I am working on positioning related things to sort out and share my knowledge.

1. Positioning includes three types: Network, base station, and GPS

2. Either way, the core of positioning is to obtain the longitude and latitude. You know there are many ways to convert the longitude and latitude to a specific geographical location.

Three implementations are described as follows:

◆ GPS

Relatively simple

Public String MAC; textview MSG; locationmanager; locationlistener; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); MSG = (textview) findviewbyid (R. id. text); locationmanager = (locationmanager) getsystemservice (location_service); // defines the listening function locationlistener for location changes = new locationlistener () {public void onlocationchanged (location) {MSG. settext ("onlocationchanged" + location. getlatitude () + "," + location. getlongpolling (); log. E ("onlocationchanged", location. getlatitude () + "," + location. getlongpolling ();} public void onstatuschanged (string provider, int status, bundle extras) {} public void onproviderenabled (string provider) {} public void onproviderdisabled (string provider) {}}; locationmanager. requestlocationupdates (locationmanager. gps_provider, 0, 0, locationlistener );}

The first thing you need to note about GPS is to enable GPS and manifest in your mobile phone positioning settings. the relevant permissions in XML need to be written in (I will search for them, but I will not repeat them here). I finally remember to test them outdoors, but I cannot test GPS in the room.

◆ Base Station

You can achieve this by yourself, but the complexity is much more than you think. To obtain the base station information, you can use the API provided by Android, which is as simple as the following code:

Private void getcellinfo () throws exception {/** call the API to obtain the base station information */telephonymanager mtelnet = (telephonymanager) getsystemservice (context. telephony_service); gsmcelllocation location = (gsmcelllocation) mtelnet. getcelllocation (); If (location = NULL) throw new exception ("failed to get the base station information"); string operator = mtelnet. getnetworkoperator (); log. E ("base station information", operator); string MCC = Operator. substring (0, 3); string MNC = Operator. substring (3, 5); int cid = location. getcid (); int LAC = location. getlac (); log. E ("specific base station information", "MCC =" + MCC + ", MNC =" + MNC + ", LAC =" + LAC + ", cid = "+ CID );}

 

However, it is troublesome to convert the base station information to the longitude and latitude. Of course, Google is the most authoritative one, and some other free websites have been found on the Internet, which is obviously unreliable, I believe that you have found all the APIS related to this API interface called MAPS maps. here we need to obtain the longitude and latitude APIs through the base station information, but it is based on business, the maximum number of tests per day is 100. fees should be charged after the release. The fees are not checked. Don't think this is easy. I believe that you will find more problems when you spend half a day testing. In addition, it is necessary to have the ability to read English. For most people, the threshold is too high. It is recommended to skip this step. Because the base station is not very easy to use, for example, when the mobile phone does not have a SIM card or tablet, it cannot use the base station.

◆ Network

We recommend that you use Network Positioning. Because the network is too common, mobile devices can access the Internet now. Of course, I am talking about wifi. 3G is not tested without conditions. Do not use the android API here, as shown in figure

    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,                0, locationListener);

Because most mobile phones in China have been castrated. If you are lucky, you may be able to obtain the data. If you are lucky, you may find that the mobile phone system has been castrated for a long time, I think you may be crazy. Therefore, we cannot rely on Android APIs for network positioning. Here we recommend Baidu positioning. I tested it, and the speed was quite good. The address is http://developer.baidu.com/map/sdk-android.htm. Then

Take a look at the mylocation class in simple, which is simple and clear to quickly solve the problem. However, I have never tested it abroad. I don't know if Baidu can be used abroad. Of course, the android SDK in Google maps should also have corresponding functions, but it is a bit complicated. For example, if you only want to locate in China, you can trust Baidu. If it is international, google will always be your first choice

End!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.