If you search for Android location null on the internet, you cannot locate the problem. It is estimated that there are a lot of articles about how to solve the problem, but in the end, you find it useless. This article will analyze the implementation principle of Android positioning in depth and propose a real solution. Before analysis, we must first check the positioning SDK officially provided by android.
Default Android GPS Positioning instance
Get LocationManager:
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Select Location Provider:
There are multiple providers in the Android system, which are
GPS_PROVIDER:
This is because the mobile phone has a GPS chip, and then the chip can use the satellite to obtain its own location information. However, in the room, GPS positioning is useless and difficult to locate.
NETWORK_PROVIDER:
This is the use of Network Positioning, usually by using the addresses of mobile phone base stations and Wi-Fi nodes to roughly locate the location,
This positioning method depends on the server, that is, the server that translates the information of the base station or WIF node into the location information. Currently, most Android mobile phones do not have the official google location manager library installed, and the Chinese mainland network does not allow this, that is, there is no server to do this. Naturally, this method basically cannot be implemented.
PASSIVE_PROVIDER:
Passive Location method, which means that the location information is updated by other applications, the application can directly read the received message. For example, if Baidu map and AMAP are already installed in the system (precise indoor location can be achieved), you only need to use them to locate the problem, using this method, you can certainly get more precise positioning information in your program.
You can directly specify a provider
String provider = mLocationManager.getProvider(LocationManager.GPS_PROVIDER);
You can also configure a provider that is the closest to your needs.
Criteria crite = new Criteria (); crite. setAccuracy (Crite. ACCURACY_FINE); // precision crite. setPowerRequirement (Crite. POWER_LOW); // select String provider = mLocationManager as the power consumption type. getBestProvider (crite, true );
Get Location
Location location = mLocationManager.getLocation(provider);
Then you will find that the returned location is always null and you cannot locate it. Then, the Internet is everywhere asking why the location obtained is null, and the network is everywhere the so-called solution to this problem.
Solution
Some people say on the Internet that the location is very likely to be null at the beginning, because the program has never requested it. You only need to re-request to update the location and register the listener to receive the updated location information.
LocationListener locationListener = new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @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 cannot obtain the location information.
Why can't I get the location?
In fact, as I mentioned above, all the above solutions have not solved the fundamental problem, that is, when you are developing indoors, your mobile phone cannot obtain location information, how do you tell the system to send location information to your program. Therefore, to fundamentally solve this problem, it is necessary to solve the problem of location information acquisition. As I mentioned earlier, only NETWORK_PROVIDER is a reliable method for indoor positioning, but most vendors do not use google services because of the strange network in mainland China, this positioning method is useless by default. What should we do? You can find an alternative service provider. Baidu's location information sdk can solve this problem. The basic principle has been mentioned above. It is to collect your wi-fi node information and your mobile phone base station information for locating.
The real solution is to use Baidu location positioning SDK
Download SDK:
Http://pan.baidu.com/s/1i3xGMih
You can download the latest sdk from the official website.
Http://lbsyun.baidu.com/sdk/download
SDK usage:
1. Apply for a service key for Baidu. For detailed operation steps, see the official website:
Http://api.map.baidu.com/lbsapi/cloud/geosdk.htm
2. Copy the downloaded sdk file locSDK_4.1.jar to your project's libs.
3. Modify the AndroidManifest file and add the following configuration to the file:
The value in meta-data is changed to your own key.
Call the sdk in the Code:
Public class LocationUtil {private final static boolean DEBUG = true; private final static String TAG = "LocationUtil"; private static LocationUtil mInstance; private BDLocation mLocation = null; private MLocation mBaseLocation = new MLocation (); public static LocationUtil getInstance (Context context) {if (mInstance = null) {mInstance = new LocationUtil (context);} return mInstance ;} context mContext; Stri Ng mProvider; public BDLocationListener myListener = new MyLocationListener (); private 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 & mLocationClient. 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 the address information option. setCoorType ("bd09ll"); // The return result is Baidu longitude and latitude. The default value is gcj02option. setScanSpan (5000); // you can specify a time interval of msoption to initiate a request. disableCache (true); // disable cache locating option. setPoiNumber (5); // maximum number of returned POI options. setPoiDistance (1000); // poi query distance option. setPoiExtraInfo (true); // whether the POI phone number, address, and other details are required. mLocationClient. setLocOption (option);} public class MyLocationListener implements BDLocationListener {@ Overridepublic void onReceiveLocation (BDLocation location) {if (location = null) {return;} mLocation = location; mBaseLocation. latitude = mLocation. getLatitude (); mBaseLocation. longpolling = mLocation. getlongpolling (); StringBuffer sb = new StringBuffer (256); 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. getlongpolling (); 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 longpolling ;}}
Of course, don't forget to enable gps location in setting.
/********************************
* This article is from the blog "love to kick the door"
* Reprinted please indicate the source: http://blog.csdn.net/itleaks
**************************************** **/