A complete solution to Android GPS can't locate this stubborn problem

Source: Internet
Author: User

We go online search Android location is null can not locate the problem, there is an estimated a lot of articles about how to solve, but in the end we found that basically useless. This article will be based on the principles of Android positioning to deeply analyze the reasons and propose a real solution. Before we go into the analysis, we must first look at the official Android Locator SDK.


default Android GPS location instance
Get Locationmanager:
Mlocationmanager = (Locationmanager) getsystemservice (Context.location_service);
Select location Provider:

The Android system has a variety of provider, namely

Gps_provider:

This is the mobile phone with a GPS chip, and then use the chip can use the satellite to obtain their own location information. However, in the indoor, GPS positioning is basically useless, it is difficult to locate the.

Network_provider:

This is the use of network positioning, usually using the mobile phone base station and the address of the WiFi node to roughly locate the location,

This positioning method depends on the server, which is the ability to translate base station or WIF node information into location information. Since most Android phones do not have Google's official Location Manager library installed, the Continental network is not allowed, that is, there is no server to do this, naturally this method is basically unable to achieve positioning.

Passive_provider:

Passive positioning method, this meaning is also more obvious, is to use off-the-shelf, when other applications use positioning updated positioning information, the system will be saved, the application receives the message directly after the read. For example, if the system has installed Baidu map, the gold map (indoor can achieve accurate positioning), you just use them to locate, and then use this method in your program must be able to get more accurate positioning information.

A user can specify a provider directly

String Provider = Mlocationmanager.getprovider (Locationmanager.gps_provider);

Configuration can also be provided by the system according to the user's configuration for the user to choose a provider closest to the user's needs

Criteria Crite = new criteria ();  

get location
Location location = mlocationmanager.getlocation (provider);  

Then you will find that the returned location is always null, and you cannot locate it naturally. Then the Internet is full of advice on why the location is null, and the same network is everywhere to solve the problem of the so-called solution.

The so-called solution

Some people online say that the first location is likely to be null , because the program has never been requested, simply re-request the update location, and register the listener to receive updated location information.

Locationlistener Locationlistener = new Locationlistener () {        @Override public        void Onstatuschanged (String provider, int status, Bundle extras) {        }        @Override public        void onproviderenabled (String provider) {        }< c6/> @Override public        void onproviderdisabled (String provider) {        }        @Override public        Void Onlocationchanged (location location) {            longitude = location.getlongitude ();            Latitude  = Location.getlatitude ();            LOG.D (TAG, "location Longitude:" + longitude + "Latitude:" + latitude);}        }; Mlocationmanager.requestlocationupdates (ServiceProvider, 10000, 1, this);

Then you find that onlocationchanged will never be called, and you still can't get the location information.

Why can't we get to location?

In fact, I have mentioned above, all of the above solutions have not solved the fundamental problem, that is, when you are in the interior development, your phone is not able to obtain location information, you call the system how to notify your location information to your program. So to solve this problem fundamentally, we should solve the problem of location information acquisition. Just also mentioned, only network_provider this mode is the indoor positioning reliable way, but because of the strange network of the mainland, and most manufacturers will not use Google services, this positioning method is not available by default. So what? Good to do, find an alternative service providers can, Baidu's location information SDK can solve this problem. Its basic principle has been mentioned above, is to collect your WiFi node information and your cell phone base station information to locate.


Real solution, use Baidu Location Locator SDK
SDK Download:

Http://pan.baidu.com/s/1i3xGMih

Of course, you can download it on the official website so that you can download it to the latest SDK.

Http://lbsyun.baidu.com/sdk/download

SDK use:

1.   Apply for Baidu's service key, For specific procedures See official website:

Span style= "font-size:18px" >      http:// api.map.baidu.com/lbsapi/cloud/geosdk.htm

2. Copy the SDK file Locsdk_4.1.jar downloaded above to your project's libs

3.  Modify the Androidmanifest. config file to add the following configuration

        <service            android:name= "com.baidu.location.f"            android:enabled= "true"            android:process= ": Remote" >        </service>        <meta-data            android:name= "Com.baidu.lbsapi.API_KEY"            android:value= " XXXXX "/><uses-permission android:name=" Android.permission.ACCESS_NETWORK_STATE "/><uses-permission Android:name= "Android.permission.ACCESS_COARSE_LOCATION"/><uses-permission android:name= " Android.permission.ACCESS_FINE_LOCATION "/>

The value of value in Meta-data above is changed to your own key

in the code, call the SDK:

public class Locationutil {private Final static Boolean DEBUG = True;private final static String TAG = "Locationutil";p Riv Ate static locationutil minstance;private bdlocation mlocation = null;private mlocation mbaselocation = new MLocation ();p Ublic static Locationutil getinstance (context context) {if (minstance = = null) {minstance = new Locationutil (context);} return minstance;} Context Mcontext; String mprovider;public Bdlocationlistener mylistener = new Mylocationlistener ();p rivate locationclient Mlocationclient;public Locationutil (Context context) {mlocationclient = new locationclient ( Context.getapplicationcontext ()); InitParams (); Mlocationclient.registerlocationlistener (MyListener);} public void StartMonitor () {if (DEBUG) log.d (TAG, "Start Monitor location"), if (!mlocationclient.isstarted ()) { Mlocationclient.start ();} if (mlocationclient! = null && mlocationclient.isstarted ()) {mlocationclient.requestlocation ();} else {log.d (" LocSDK3 "," locclient is null or not started ");}}public void Stopmonitor () {if (DEBUG) log.d (TAG, "Stop Monitor Location"); if (mlocationclient! = null && mlocation Client.isstarted ()) {mlocationclient.stop ();}} Public Bdlocation getLocation () {if (DEBUG) log.d (TAG, "get Location"); return mlocation;} Public Mlocation getbaselocation () {if (DEBUG) log.d (TAG, "get Location"); return mbaselocation;} private void InitParams () {locationclientoption option = new Locationclientoption (); Option.setopengps (true);// Option.setpriority (Locationclientoption.networkfirst); Option.setaddrtype ("all");// The returned positioning result contains address information option.setcoortype ("Bd09ll");//Returns the positioning result is Baidu latitude and longitude, the default value of Gcj02option.setscanspan (5000);//    Set the time interval for initiating a location request to 5000msoption.disablecache (true);//disable Enable cache location Option.setpoinumber (5); Returns the maximum number of POI option.setpoidistance (1000); Poi Query Distance Option.setpoiextrainfo (true); If you need details such as the phone and address of the POI mlocationclient.setlocoption (option);} public class Mylocationlistener implements Bdlocationlistener {@Overridepublic void onreceivelocation (bdlocation Location{if (location = = null) {return;} Mlocation = Location;mbaselocation.latitude = Mlocation.getlatitude (); mbaselocation.longitude = Mlocation.getlongitude (); StringBuffer sb = new StringBuffer, Sb.append ("Time:"); Sb.append (Location.gettime ()); Sb.append ("\nerror Code:") ; Sb.append (Location.getloctype ()); Sb.append ("\nlatitude:"); Sb.append (Location.getlatitude ()); Sb.append ("\ Nlontitude: "); Sb.append (Location.getlongitude ()); Sb.append (" \nradius: "); Sb.append (Location.getradius ()); Sb.append ("\ncity:"); Sb.append (Location.getcity ()); if (location.getloctype () = = Bdlocation.typegpslocation) { Sb.append ("\nspeed:"); Sb.append (Location.getspeed ()); Sb.append ("\nsatellite:"); Sb.append ( Location.getsatellitenumber ());} else if (location.getloctype () = = bdlocation.typenetworklocation) {sb.append ("\naddr:"); Sb.append ( Location.getaddrstr ());} if (DEBUG) log.d (TAG, "" + SB);} public void Onreceivepoi (Bdlocation poilocation) {}}public class Mlocation {public double latitude;public double Longitude;}} 

of course, don't forget to open the GPS in the setting.

/********************************

* This article from the blog "Love Kick Door"

* Reprint Please indicate the source : Http://blog.csdn.net/itleaks

******************************************/

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.