We go online search Android positioning location NULL can not locate the problem, it is estimated that a large pile of articles on how to solve, but finally we found that the basic useless. This article will be based on the Android positioning principle to in-depth analysis of the reasons can not locate and propose a real solution. Before analyzing, we must first look at the Android official Locator SDK.
Default Android GPS Positioning example
Get Locationmanager:
Mlocationmanager = (Locationmanager) getsystemservice (Context.location_service);
Select Location Provider:
The Android system has a variety of provider, respectively
Gps_provider:
This is the mobile phone has a GPS chip, and then use the chip can use the satellite to obtain their own location information. But in the interior, GPS positioning is basically useless, difficult to locate.
Network_provider:
This is the use of network positioning, usually using mobile phone base station and WiFi node address to approximate location,
This positioning depends on the server, that is, the ability of the server that translates the base station or WIF node information into location information. Since most Android phones are not installed in Google's official Location Manager library, the mainland network is not allowed, that is, there is no server to do this thing, naturally this method is basically unable to achieve positioning.
Passive_provider:
Passive positioning method, this meaning is also more obvious, is to use ready-made, when other applications using positioning updated positioning information, the system will be saved, the application received a message directly after reading. For example, if the system has installed the Baidu map, the gold map (indoor can achieve accurate positioning), you just use them after positioning, and then use this method in your program is sure to get more accurate positioning information.
A user can specify a provider directly
String Provider = Mlocationmanager.getprovider (Locationmanager.gps_provider);
can also provide configuration, by the system according to the user's configuration for the user to select a closest user needs provider
Criteria Crite = new criteria ();
Crite.setaccuracy (Crite.accuracy_fine); Precision
crite.setpowerrequirement (crite.power_low);//Power type Select
String Provider = Mlocationmanager.getbestprovider (Crite, true);
Get location
Location Location = mlocationmanager.getlocation (provider);
Then you will find that the returned location is always null and you are naturally unable to locate it. Then the Internet is full of advice on why the location is null, and the same network is full of so-called solutions to this problem.
The so-called solution
It is said on the Internet that the first location is very likely to be null, because the program has never requested, simply request the update 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) {
}< c7/> @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 invoked and you still can't get the location information.