How to get accurate GPS location Using google API(Fused Location) in android? -


i using google api location gps in android. got accurate location if moving. if in same location above 15 mins give more locations.

my code is:

public class locationservicetesting extends service         implements locationlistener, connectioncallbacks, onconnectionfailedlistener {        // location updates intervals in sec     private static int update_interval = 10000; // 10 sec     private static int fatest_interval = 5000; // 5 sec     private static int displacement = 10; // 10 meters      locationrequest mlocationrequest;     googleapiclient mgoogleapiclient;     location mcurrentlocation,mlastlocation,test3lastlocation,test2lastlocation;     string mlastupdatetime;       string devicecode = "", imeinumber = "";      protected void createlocationrequest() {                 mlocationrequest = new locationrequest();         mlocationrequest.setinterval(update_interval);         mlocationrequest.setfastestinterval(fatest_interval);         mlocationrequest.setpriority(locationrequest.priority_high_accuracy);         mlocationrequest.setsmallestdisplacement(displacement);     }      private boolean isgoogleplayservicesavailable(context context) {         int status = googleplayservicesutil.isgoogleplayservicesavailable(this);         if (connectionresult.success == status) {             return true;         } else {             googleplayservicesutil.geterrordialog(status, (activity) context, 0).show();             return false;         }     }      public locationservicetesting() {         // todo auto-generated constructor stub     }        @override     public void oncreate() {         // todo auto-generated method stub         super.oncreate();          log.d(tag, "oncreate ...............................");         // show error dialog if goolgleplayservices not available         log.d(tag, "on play service "+checkplayservices());         if (checkplayservices()) {              log.e("oogleplay service found", "service");             buildgoogleapiclient();             createlocationrequest();         }       }      @override     public int onstartcommand(intent intent, int flags, int startid) {         // todo auto-generated method stub         super.onstartcommand(intent, flags, startid);         if(!mgoogleapiclient.isconnected())             mgoogleapiclient.connect();         return start_sticky;      }     @override     public void onstart(intent intent, int startid) {         // todo auto-generated method stub         super.onstart(intent, startid);          if (mgoogleapiclient != null) {                 mgoogleapiclient.connect();             }     }        @override     public void ondestroy() {         // todo auto-generated method stub         super.ondestroy();         if(mgoogleapiclient.isconnected())             mgoogleapiclient.disconnect();     }     @override     public void onconnectionfailed(connectionresult connectionresult) {         // todo auto-generated method stub         log.d(tag, "connection failed: in service " + connectionresult.tostring());      }      @override     public void onconnected(bundle arg0) {         // todo auto-generated method stub         log.e(tag, "onconnected - isconnected inservice " + mgoogleapiclient.isconnected());         startlocationupdates1();      }      protected void startlocationupdates() {         pendingresult<status> pendingresult = locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient,                 mlocationrequest, this);         log.d(tag, "location update started ..............: ");      }      @override     public void onconnectionsuspended(int arg0) {         // todo auto-generated method stub         mgoogleapiclient.connect();      }      // distance between 2 latlong     public double getdistance(location lastlocation, location currentlocation) {         double distance = 0;         distance = lastlocation.distanceto(currentlocation);         return distance;      }      @override     public void onlocationchanged(location location) {         // todo auto-generated method stub           sync synctoserver = new sync();         log.e("with out api location ", "location  "+location);                  if (location.getaccuracy() < 20) {                     location curloc;                     double distance=0.0;                     curloc= locationservices.fusedlocationapi.getlastlocation(mgoogleapiclient);                     if(test3lastlocation!=null)                     {                         distance=getdistance(test3lastlocation, curloc);                         log.e("distance", "distance "+distance);                     }                      if(distance>20)                     {                         dbo.insertgps(dbo, devicecode, imeinumber, "" + location.getlongitude(), "" + location.getlatitude(),                                 _contrl.getcurrenttimestamp());                      }                   test3lastlocation=curloc;                 }           mlastlocation = location;          displaylocation();       }      @override     public ibinder onbind(intent arg0) {         // todo auto-generated method stub         return null;     }       private boolean checkplayservices() {         int resultcode = googleplayservicesutil                 .isgoogleplayservicesavailable(this);         if (resultcode != connectionresult.success) {             if (googleplayservicesutil.isuserrecoverableerror(resultcode)) {                 //googleplayservicesutil.geterrordialog(resultcode, this,play_services_resolution_request).show();                 toast.maketext(getbasecontext(), "play service not", toast.length_long).show();             } else {                 toast.maketext(getapplicationcontext(),                         "this device not supported.", toast.length_long)                         .show();                 //finish();             }             return false;         }         return true;     }      protected synchronized void buildgoogleapiclient() {         mgoogleapiclient = new googleapiclient.builder(this)                 .addconnectioncallbacks(this)                 .addonconnectionfailedlistener(this)                 .addapi(locationservices.api).build();     }      private void displaylocation() {          mlastlocation = locationservices.fusedlocationapi                 .getlastlocation(mgoogleapiclient);          if (mlastlocation != null) {             double latitude = mlastlocation.getlatitude();             double longitude = mlastlocation.getlongitude();              //lbllocation.settext(latitude + ", " + longitude);             log.e("location chnage ", ""+mlastlocation+" "+mlastlocation.gettime()+" "+mlastlocation.getelapsedrealtimenanos());          } else {             log.e("location chnage ", "null values");              //lbllocation.settext("(couldn't location. make sure location enabled on device)");         }     }       protected void startlocationupdates1() {          locationservices.fusedlocationapi.requestlocationupdates(                 mgoogleapiclient, mlocationrequest, this);      }      protected void stoplocationupdates() {         locationservices.fusedlocationapi.removelocationupdates(                 mgoogleapiclient, this);     }    } 

please me. in advance.....

i refereed link http://www.androidhive.info/2015/02/android-location-api-using-google-play-services/

please me on come problem....

i getting location if in same location enter image description here

i had same issue before few days ago.i have changed setpriority() priority_balanced_power_accuracy. might resolve problem.

you have used priority_high_accuracy in createlocationrequest() method.

instead of line mlocationrequest.setpriority(locationrequest.priority_high_accuracy);

add line mlocationrequest.setpriority(locationrequest.priority_balanced_power_accuracy);

this issue occurred because of onlocationchanged method called priority_high_accuracy , gives new location different.

my suggestion use priority_balanced_power_accuracy.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -