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

Source: Internet
Author: User



everyone went online to search for Android positioning location is null Unable to locate the problem. A whole bunch of articles are expected to explain how to solve them. 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:


Android systems exist in a variety of provider,


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, very difficult to locate the.


Network_provider:

This is the use of network positioning, generally 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 the base station or WIF node information into the location information of the server. Because most Android phones don't have Google's official Location Manager library installed at the moment. The continental network also disagreed. There is no server to do this, naturally this method is basically unable to achieve positioning.

Passive_provider:

Passive positioning mode. The meaning is more obvious. is to use off-the-shelf, when other applications use positioning updated positioning information. The system will be saved. The app will be able to read the message directly after it receives it. For example, if the system has installed Baidu map, the German map (indoor can achieve accurate positioning). You just have to use them to locate them later. Again using this method in your program is sure to be able to get accurate positioning information.


A user can specify a provider directly




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


can also provide the configuration, the system according to the user's configuration for the user to choose a closest user needs provider


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, the same network is everywhere to solve the problem of the so-called solution.



The so-called settlement method


someone online said. the location is very likely to be null, because the program has never been requested, just one more request to 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) {
        }

        @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, as I mentioned above, all of the above solutions do not solve the fundamental problem, which is when you are developing indoors. Your phone simply can't get location information, you ask the system how to notify your location information to your program.



So to solve the problem fundamentally, we should solve the problem of location information acquisition. Just also mentioned, just have network_provider such mode is the indoor location reliable way, just because of the strange network of the mainland, and most manufacturers also do not use Google services, such positioning method by default is not available. So what? Good to do, find an alternative service provider will be able to, Baidu's location information SDK can solve the problem.



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





the real solution, using the Baidu Location Locator SDK
SDK Download:


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



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



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


     SDK using:

1.   Apply for Baidu's service key. For detailed 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 include 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";
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;
String 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 includes the address information.
option.setCoorType("bd09ll");//The returned positioning result is Baidu latitude and longitude, the default value is gcj02
option.setScanSpan(5000);//Set the interval for initiating a positioning request to 5000ms
option.disableCache(true);//Disable caching cache positioning
option.setPoiNumber(5); //Returns the maximum number of POIs
option.setPoiDistance(1000); //poi query distance
option.setPoiExtraInfo(true); //Do you need specific information such as the phone and address of the POI?
mLocationClient.setLocOption(option);
}


Public class MyLocationListener implements BDLocationListener {
@Override
Public void onReceiveLocation(BDLocation location) {
If (location == null) {
Return ;
}
mLocation = location;
mBaseLocation.latitude = mLocation.getLatitude();
mBaseLocation.longitude = mLocation.getLongitude();

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.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



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





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


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.