Android successfully uses GPS to obtain the current geographic location (if getlastknownlocation is resolved, null is returned). GPS cannot be used to locate the current geographic location. That is, the getlastknownlocation method always returns NULL. A blog post later Getlastknownlocation () returns NULL. I am very helpful. I would like to express my gratitude to the author of this blog. However, I think it is necessary to add a few points. Otherwise, I still cannot find the current location after reading this blog.First, when using GPS positioning, it is best not to use the getlastknownlocation method to obtain the location of the current location object, because this object can be given by the system in the onlocationchanged parameter (according to the document, getlastknownlocation has two functions: 1. obtain the current geographic location 2. if the current location is not successfully located, you can use this method to obtain the location of the last time the map was opened in the cache ). This avoids NULL pointer exceptions. More importantly, GPS positioning is not successful at once. In the case of more than 90%, getlastknownlocation returns NULL.
Second, it is best to instantiate the locationlistener in the oncreate () method of the activity. Implementation of the system callback method: Onlocationchanged (finallocation LOC) Onproviderdisabled (final strings) Onproviderenabled (final strings) Onstatuschanged (final string S, final int I, final bundle B)Third: requestlocationupdates must be registered and listened to in onresume (), and reversed in onpause (). .
Fourth: test whether GPS is positioned successfully. Go to an empty location and avoid blocking. This is very important. Otherwise, you will never know whether your GPS positioning is successful.
The following is an example of successfully obtaining the current geographic location using GPS. Hope to help you get rid of the haze of GPS positioning.
@ Overridepublic void oncreate (final bundle icicicle ){ Super. oncreate (icicle ); This. setcontentview (R. layout. activity_mapview ); Mbtndone = (button) findviewbyid (R. Id. btn_done ); Mbtndone. setonclicklistener (this ); Mapview = (mapview) findviewbyid (R. Id. map_view ); Mapview. setbuiltinzoomcontrols (true ); Mapcontroller = mapview. getcontroller (); Mlocationmanager = (locationmanager) getsystemservice (context. location_service ); Mdefamarmarker = getresources (). getdrawable (R. drawable. map_redpin ); Mdefamarmarker. setbounds (0, 0, mdefamarmarker. getintrinsicwidth (), Mdefamarmarker. getintrinsicheight ()); Mbuoyoverlay = new buoyitemizedoverlay (mdefamarmarker, this ); Initdensitydpi (); Mzoomlevel = mapview. getmaxzoomlevel ()-1;// It is best to instantiate locationlistener in the oncreate () method of activity. When GPS gets location Dynamic callOnlocationchanged method. Mlocationlistener = new locationlistener (){ @ Override Public voidonlocationchanged (final location LOC ){ Loghelper. I (TAG, "onlocationchanged. LOC:" + LOC ); If (loc! = NULL ){ Loghelper. I (TAG, "onlocationchanged. Latitude :" + LOC. getlatitude () + ", longtitude:". getlongdistance ()); Geopoint = maputils. getgeopoint (LOC ); Mapcontroller. animateto (geopoint ); Initbuoyoverlayitems (LOC ); } Else { Toast ( Mapviewactivity. This, "Yourcurrent location is temporarily unavailable .", Toast. length_short). Show (); } } // When setting> Location & Security> Use wirelessnetworks is deselected, use GPS Called When satellites is deselected Public void onproviderdisabled (final string s ){ Loghelper. I (TAG, "onproviderdisabled ."); } // When the system setting-> Location & Security-> Use wirelessnetworks check box, use GPS satellites check box Call upon selection Public void onproviderenabled (final string s ){ Loghelper. I (TAG, "onproviderenabled ."); } Public void onstatuschanged (final string S, final int I, final bundle B ){ Loghelper. I (TAG, "onstatuschanged ."); } }; } @ Override Public void onstart (){ Super. onstart (); Mapcontroller. setzoom (mzoomlevel ); If (! Dosomegoodutils. isnetworkavailable (this )){ Mbtndone. setenabled (false ); Showdialog (dialog_no_network ); } Else { // Determine whether usegps satellites is selected Booleanisgpsenabled = maputils. isgpsprovideravaliable (this ); // Determine whether usewireless networks is checked Booleaniswifienabled = maputils. iswifiprovideravaliable (this ); If (! Isgpsenabled &&! Iswifienabled ){ If none of them are selected, a dialog box is displayed, prompting you to select. } Else { Location lastknownlocation = NULL; // If you only select use gpssatellites, that is, GPS positioning is allowed. If (isgpsenabled &&! Iswifienabled ){ Lastknownlocation = mlocationmanager. getlastknownlocation (locationmanager. gps_provider ); Mprovidername = locationmanager. gps_provider; // If you only select use wirelessnetworks, only Network Positioning is allowed. } Else if (! Isgpsenabled & iswifienabled ){ Lastknownlocation = Mlocationmanager. getlastknownlocation (locationmanager. network_provider ); Mprovidername = locationmanager. network_provider; // If both are checked, use GPS first because GPS positioning is more precise. } Else if (isgpsenabled & iswifienabled ){ Lastknownlocation = mlocationmanager. getlastknownlocation (locationmanager. gps_provider ); Mprovidername = locationmanager. gps_provider; If (lastknownlocation = NULL ){ Lastknownlocation = mlocationmanager. getlastknownlocation (locationmanager. network_provider ); Mprovidername = locationmanager. network_provider; } } If (! Textutils. isempty (mprovidername )){ Mlocationmanager. requestlocationupdates ( Mprovidername, 1000, 1, mlocationlistener ); } // If the location can be located at once, execute the following code. When the location is located on the network, the location can be located at one time. When GPS is used, the code will not play a major role. If (lastknownlocation! = NULL ){ Mbtndone. setenabled (true ); // Obtain the current geographic location Geopoint lastknownpoint = getlastknownpoint (lastknownlocation ); // Animation to this location Mapcontroller. animateto (lastknownpoint ); // Update the buoy. This method will not be made public here. Just know what it means. Initbuoyoverlayitems (lastknownlocation ); } } } } @ Override Protected void onresume (){ Super. onresume (); Loghelper. I (TAG, "onresume. provider name:" + mprovidername ); If (! Textutils. isempty (mprovidername )){ // Register the requestlocationupdates listener here When GPS is located.Without this sentence, the positioning fails. Mlocationmanager. requestlocationupdates (mprovidername, 1000, 1, Mlocationlistener ); } } @ Override Protected void onpause (){ Super. onpause (); // Cancel registration listening If (mlocationmanager! = NULL ){ Mlocationmanager. removeupdates (mlocationlistener ); } }} Positioning Methods: some of them are more inclined to use the getbestprovider method, but I think this method has its drawbacks. Not all mobile phones support it. "Use getbestprovider to obtain the most suitable location" It is best to use Network Positioning and GPS positioning ....Note: maputils is a tool class I write myself, there is a publisher email me, microsoftyulei@163.com