Identify user location in android -


i'm quite beginner android , i'm trying out best learn android. i'm trying create android application identify user location using google maps. able identify user's location, not expected.

as in following picture shows default location. once click button in top left corner, map shows location.

enter image description here

i know location straight away, without clicking button. want know user location , store in string, not in longitude , latitude values. help?

code:

package com.example.lalinda.googlemap1;  import android.support.v4.app.fragmentactivity; import android.os.bundle;  import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.onmapreadycallback; import com.google.android.gms.maps.supportmapfragment; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.markeroptions;  public class mapsactivity extends fragmentactivity implements onmapreadycallback {      private googlemap mmap;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_maps);         // obtain supportmapfragment , notified when map ready used.         supportmapfragment mapfragment = (supportmapfragment) getsupportfragmentmanager()                 .findfragmentbyid(r.id.map);         mapfragment.getmapasync(this);     }      @override     public void onmapready(googlemap googlemap) {         mmap = googlemap;         mmap.setmylocationenabled(true);     } } 

google maps uses user's lastknownlocation location, if lastknownlocation unknown takes time fetch location through different provider gps or network. suggest use lastknownlocation base location , update locationlistener.

       @override        public void onmapready(googlemap googlemap) {         if (activitycompat.checkselfpermission                 (this, manifest.permission.access_fine_location) != packagemanager.permission_granted                 &&                 activitycompat.checkselfpermission                         (this, manifest.permission.access_coarse_location) != packagemanager.permission_granted)         {             requestpermissions(new string[]{                     manifest.permission.access_coarse_location,                     manifest.permission.access_fine_location             }, 1);          }         else {              // enable location buttons             googlemap.setmylocationenabled(true);             googlemap.getuisettings().setmylocationbuttonenabled(true);              // fetch last location if provider - gps.             final locationmanager locationmanager = (locationmanager) getsystemservice(location_service);             final location loc = locationmanager.getlastknownlocation(locationmanager.gps_provider);             //if last known location not available             if (loc == null) {                  final locationlistener locationlistener = new locationlistener() {                     @override                     public void onlocationchanged(final location location) {                         clearmap(googlemap); // clear map every new location update marker unless `n` number of markers form.                         // getting location of user                         final double latitude = location.getlatitude();                         final double longitude = location.getlongitude();                         final latlng usercurrentlocation = new latlng(latitude, longitude);                         //focus added here **edited**     googlemap.movecamera(cameraupdatefactory.newlatlngzoom(usercurrentlocation, 14));                          googlemap.addmarker(new markeroptions()                        .position(usercurrentlocation)                        .draggable(true)        .icon(bitmapdescriptorfactory.fromresource(r.drawable.your_marker_icon_from_deawable)));                         //do lat , lng, parse string if u want or set marker.                      }                      @override                     public void onstatuschanged(string provider, int status, bundle extras) {                     }                      @override                     public void onproviderenabled(string provider) {                         //when user enables gps setting, method triggered.                     }                      @override                     public void onproviderdisabled(string provider) {                         //when no provider available in case gps provider, trigger gpsdialog here.                     }                 };                  //update location every 10sec in 500m radius both provider gps , network.              locationmanager.requestlocationupdates(locationmanager.gps_provider, 10*1000, 500, locationlistener);                 locationmanager.requestlocationupdates(locationmanager.network_provider, 10000, 500, locationlistener);             }             else {                 //do last known location.                 // getting location of user                 final double latitude = loc.getlatitude();                 final double longitude = loc.getlongitude();                 final latlng usercurrentlocation = new latlng(latitude, longitude);                  //focus added here **edited**     googlemap.movecamera(cameraupdatefactory.newlatlngzoom(usercurrentlocation, 14));                  googlemap.addmarker(new markeroptions()                 .position(usercurrentlocation)                 .draggable(true)        .icon(bitmapdescriptorfactory.fromresource(r.drawable.your_marker_icon_from_deawable)));             }            }         } 

permission handling:

     @override public void onrequestpermissionsresult(int requestcode, @nonnull string[] permissions, @nonnull int[] grantresults) {     switch (requestcode) {          case 1:             if (grantresults[0] != packagemanager.permission_granted){                 //do             }         default:             super.onrequestpermissionsresult(requestcode, permissions, grantresults);     } } 

this example demonstration (example purpose) , did not optimized code using methods. please edit in future.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -