Author: fly_binbin
Android GPS Positioning implementation
Today, I spent more than an hour writing a small example of GPS code for obtaining geographic locations, including some online code and some modifications to the code, hope to help you. The specific code is as follows: to use the GPS device of the adnroid platform, you must first add the permission. Therefore, you must add the following permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
The specific implementation code is as follows:
First, determine whether the GPS module exists or is Enabled:
Private void opengpssettings () {locationmanager ALM = (locationmanager) This. getsystemservice (context. location_service); If (ALM. isproviderenabled (Android. location. locationmanager. gps_provider) {toast. maketext (this, "GPS module normal", toast. length_short ). show (); return;} toast. maketext (this, "Turn on GPS! ", Toast. length_short ). show (); intent = new intent (settings. action_security_settings); startactivityforresult (intent, 0); // return to the get interface after the setting is complete}
If the function is enabled normally, the display page is displayed. If the function is enabled improperly, the GPS settings page is displayed:
The Code is as follows:
Private void getlocation () {// get Location Management Service locationmanager; string servicename = context. location_service; locationmanager = (locationmanager) This. getsystemservice (servicename); // find the service information Criteria = new criteria (); criteria. setaccuracy (criteria. accuracy_fine); // high-precision criteria. setaltituderequired (false); criteria. setbearingrequired (false); criteria. setcostallowed (true); criteria. setpowerrequirement (criteria. power_low); // Low Power string provider = locationmanager. getbestprovider (criteria, true); // obtain the GPS information location = locationmanager. getlastknownlocation (provider); // get the location updatetonewlocation (location) through GPS; // set the listener. The minimum interval for automatic update is n seconds (1 second is 1*1000, in this way, the write is mainly for convenience) or the minimum displacement is greater than N meters locationmanager. requestlocationupdates (provider, 100*1000,500, locationlistener );}
Here we can get the geographic location information, but we still need to display it, so we can use the following method to display it:
Private void updatetonewlocation (location) {textview TV1; TV1 = (textview) This. findviewbyid (R. Id. TV1); If (location! = NULL) {double latitude = location. getlatitude (); double longdistance = location. getlongpolling (); tv1.settext ("dimension:" + latitude + "\ N longitude" + longpolling);} else {tv1.settext ("unable to obtain geographic information ");}}
In this way, you can get the geographic location of the current user. At least how to implement the map below will be obtained and displayed below! Thank you for your reference code!