android - Getting user location takes a very long time inside Google Map FragmentActivity -


i having issues getting location in map activity. basically, gets location, takes long time (a few minutes). i'm using singleton requests single location update retrieve location. works fine across multiple other instances in app. when try in map activity, takes long time. here class use location:

public class singleshotlocationprovider {  public interface locationcallback {     void onnewlocationavailable(gpscoordinates location); }  public static void requestsingleupdate(final context context, final locationcallback callback) {     final locationmanager locationmanager = (locationmanager) context.getsystemservice(context.location_service);      //if network available     if (locationmanager.isproviderenabled(locationmanager.network_provider)) {         log.i("singleshot", "network available");         criteria criteria = new criteria();         criteria.setaccuracy(criteria.accuracy_coarse);         try {             locationmanager.requestsingleupdate(criteria, new locationlistener() {                 @override                 public void onlocationchanged(location location) {                     callback.onnewlocationavailable(new gpscoordinates(location.getlongitude(), location.getlatitude()));                 }                 @override public void onstatuschanged(string provider, int status, bundle extras) {}                 @override public void onproviderenabled(string provider) {}                 @override public void onproviderdisabled(string provider) {}             }, null);         }         catch (securityexception e){             log.e("location", "security exception");         }     }     //if gps enabled     else if (locationmanager.isproviderenabled(locationmanager.gps_provider)){         log.i("singleshot", "gps available");         criteria criteria = new criteria();         criteria.setaccuracy(criteria.accuracy_fine);         try {             locationmanager.requestsingleupdate(criteria, new locationlistener() {                 @override                 public void onlocationchanged(location location) {                     callback.onnewlocationavailable(new gpscoordinates(location.getlongitude(), location.getlatitude()));                 }                 @override public void onstatuschanged(string provider, int status, bundle extras) {}                 @override public void onproviderenabled(string provider) {}                 @override public void onproviderdisabled(string provider) {}             }, null);         }         catch (securityexception e){             log.e("singleshot", "security exception");         }     }     else{         toast.maketext(context, "no network or gps available. try again later.", toast.length_long).show();     } }   public static class gpscoordinates {     public float longitude = -1;     public float latitude = -1;      public gpscoordinates(double lon, double lat) {         longitude = (float) lon;         latitude = (float) lat;     } } } 

this class works in areas in app. works in services , fragments. when use in map activity (fragmentactivity), singleton takes long time use callback send . here how requesting new location map activity:

public class traffic extends fragmentactivity     implements onmapreadycallback {     mapfragment mapfragment;     googlemap map;     //...      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_map);         mapfragment = (mapfragment) getfragmentmanager()             .findfragmentbyid(r.id.map);         mapfragment.getmapasync(this);          //...     }      @override     public void onmapready(googlemap googlemap) {         map = googlemap;         map.setmaptype(googlemap.map_type_normal);          singleshotlocationprovider.requestsingleupdate(getapplicationcontext(), new singleshotlocationprovider.locationcallback() {                 @override                 public void onnewlocationavailable(singleshotlocationprovider.gpscoordinates location) {                     log.i(tag, location.tostring());                 }             });     } } 

any thoughts on why taking long?

try changing criteria coarse or less accurate adequate.

criteria criteria = new criteria(); criteria.setaccuracy(criteria.accuracy_coarse); 

https://developer.android.com/reference/android/location/criteria.html


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -