After reading ~~~ The mainactivity is modified only in the original project. The source code is as follows:
Import COM. google. android. GMS. maps. cameraupdatefactory; import COM. google. android. GMS. maps. googleMap; import COM. google. android. GMS. maps. mapfragment; import COM. google. android. GMS. maps. supportmapfragment; import COM. google. android. GMS. maps. model. latlng; import COM. google. android. GMS. maps. model. polylineoptions; import android. app. progressdialog; import android. content. context; import android. location. locati On; import android. location. locationlistener; import android. location. locationmanager; import android. OS. bundle; import android. OS. handler; import android. support. v4.app. fragmentactivity; import android. widget. toast; public class mainactivity extends fragmentactivity implements locationlistener {locationmanager; mapfragment mfragment; GoogleMap GMAP; progressdialog; Boolean Flag = true; string pro = "GPS"; handler; latlng; protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); suppmapmapfragment sfragment = (supportmapfragment) This. getsuppfrfragmentmanager (). findfragmentbyid (R. id. map); // obtain the map object GMAP = sfragment. getmap (); // get a reference map GMAP. setmaptype (GoogleMap. map_type_terrain); GMAP. settraffic Enabled (true); // traffic map GMAP. setmylocationenabled (true); locationmanager = (locationmanager) getsystemservice (context. location_service); // obtain the location management service if (! Locationmanager. isproviderenabled ("GPS") {pro = "network"; toast. maketext (this, "Turn on GPS", 2000). Show (); If (! Locationmanager. isproviderenabled ("network") {toast. maketext (this, "Open the network", 2000). Show (); Pro = locationmanager. passive_provider; If (! Locationmanager. isproviderenabled (Pro) {toast. maketext (this, "Open the network... ", 3000) ;}} else {flag = true;} If (FLAG) {progressdialog = new progressdialog (this); progressdialog. settitle (""); progressdialog. setmessage ("locating... "); Progressdialog. show () ;}@ override protected void onresume () {// todo auto-generated method stub locationmanager. requestlocationupdates (Pro, 500, 0, this); super. onresume () ;}@ override protected void onpause () {// todo auto-generated method stub locationmanager. removeupdates (this); super. onpause () ;}@ overridepublic void onlocationchanged (location) {// todo auto-generated method stubprogressdialog. hide (); If (location = NULL) {toast. maketext (mainactivity. this, "positioning at the end", 1000 ). show ();} else {latlng templatlng = new latlng (location. getlatitude (), location. getlongpolling (); If (latlng = NULL) {latlng = templatlng;} else {polylineoptions = new polylineoptions (); polylineoptions. add (latlng); polylineoptions. add (templatlng); GMAP. addpolyline (polylineoptions);} GMAP. movecamera (cameraupdatefactory. newlatlng (latlng); // move the camera to GMAP. animatecamera (cameraupdatefactory. zoomto (15); }}@ overridepublic void onproviderdisabled (string provider) {// todo auto-generated method stub // network GPS Beidou} @ overridepublic void onproviderenabled (string provider) {// todo auto-generated method stub} @ overridepublic void onstatuschanged (string provider, int status, bundle extras) {// todo auto-generated method stub // loation is obtained based on the currently available provider // list <string> providers = locationmanager. getallproviders (); // find the service information // Criteria = new criteria (); // string bestprovider = locationmanager. getbestprovider (criteria, false); // Low Power }}
I will not post it here. Because the code is not complete enough, the running result will be affected by the Android version ~~~ You are welcome to give more comprehensive code to Android users ~~~