android - Get total distance travelled using GPS doesn't update my location -


hi i'm learning gps development in android. i've read basics through vogellas blog here. latitude , longitude , displayed on device don't know why doesn't update location though set runnable thread fetch location every 3 seconds. same reason why can't check if got correct formula total distance i've travelled.

this first time working maybe can give me tip or idea on how to.

i created repository in github maybe can me this. see file here :)

edit: okay main problem onchangelocation() seems doesn't work on end. tried create runnable display location every 3 seconds doesn't return result expected.

did missed out on here?

here's code far easier viewing:

main.java

import android.app.alertdialog; import android.content.context; import android.content.dialoginterface; import android.content.intent; import android.location.criteria; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.app.activity; import android.provider.settings; import android.view.menu; import android.view.view; import android.widget.button; import android.widget.textview; import android.widget.toast;  /*created kahel 08/16/2013 * read more location manager go here: http://www.vogella.com/articles/androidlocationapi/article.html * * */  public class main extends activity implements locationlistener {      private textview latitude, longitude, distance;     private button getlocation;     private locationmanager locationmanager;     private string provider;      double distance_travelled;     double latitude_prev;     double longitude_prev;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.main);          latitude = (textview)findviewbyid(r.id.txt_latitude);         longitude = (textview)findviewbyid(r.id.txt_longitude);         distance = (textview) findviewbyid(r.id.txt_distance);         getlocation = (button)findviewbyid(r.id.btn_getlocation);          checkgps();          getcurrentlocation();          getlocation.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 locatorrunner();             }         });      }      private void locatorrunner(){         thread thread = new thread()         {             @override             public void run() {                 try {                     while(true) {                         sleep(3000);                          runonuithread(new runnable() {                             @override                             public void run() {                                  /*needs ui thread*/                                 getcurrentlocation();                             }                         });                     }                 } catch (interruptedexception e) {                     e.printstacktrace();                 }             }         };          thread.start();     }      private void getcurrentlocation(){         // location manager         locationmanager = (locationmanager) getsystemservice(context.location_service);         // define criteria how select locatioin provider -> use         // default         criteria criteria = new criteria();         provider = locationmanager.getbestprovider(criteria, false);         location location = locationmanager.getlastknownlocation(provider);          toast.maketext(main.this,string.valueof(location),toast.length_short).show();           //locationmanager.requestlocationupdates(locationmanager.gps_provider,1000l,500.0f, (locationlistener) location);           // initialize location fields         if (location != null) {             system.out.println("provider " + provider + " has been selected.");             onlocationchanged(location);         } else {             latitude.settext("location not available");             longitude.settext("location not available");             distance.settext("0");         }     }      private void checkgps(){         locationmanager service = (locationmanager) getsystemservice(location_service);         boolean enabled = service.isproviderenabled(locationmanager.gps_provider);          toast.maketext(main.this,string.valueof(enabled),toast.length_short).show();          // check if enabled , if not send user gsp settings         // better solution display dialog , suggesting         // go settings         if (!enabled) {             showgpssettingdialog();         }     }      private void showgpssettingdialog(){         /*create dialog tell user enable gps settings pinpoint or location*/         alertdialog.builder alertdialog = new alertdialog.builder(main.this);         alertdialog.settitle("settings para sa lokasyon"); /*should on string values*/          alertdialog                 .setmessage("kasalukuyang hindi aktibo ang iyong gps para makuha ang iyong lokasyon. nais mo bang i-set ito ngayon?")                 .setcancelable(false)                 .setpositivebutton("oo", new dialoginterface.onclicklistener() {                     @override                     public void onclick(dialoginterface dialoginterface, int i) {                         intent intent = new intent(settings.action_location_source_settings);                         startactivity(intent);                     }                 })                 .setnegativebutton("hindi", new dialoginterface.onclicklistener() {                     @override                     public void onclick(dialoginterface dialoginterface, int i) {                         dialoginterface.cancel();                     }                 });          alertdialog gpssettingsdialog = alertdialog.create();         gpssettingsdialog.show();      }      /* request updates @ startup */     @override     protected void onresume() {         super.onresume();         locationmanager.requestlocationupdates(provider, 400, 1, this);     }      /* remove locationlistener updates when activity paused */     @override     protected void onpause() {         super.onpause();         locationmanager.removeupdates(this);     }       @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.main, menu);         return true;     }      @override     public void onlocationchanged(location location) {         getlocation(location);     }      private void getlocation(location location){          int r = 6371;          double lat = location.getlatitude();         double lon = location.getlongitude();          latitude_prev = lat;         longitude_prev = lon;          double dlat = latitude_prev - lat;         double dlon = longitude_prev - lon;          double = math.sin(dlat / 2) * math.sin(dlat / 2) + math.cos(latitude_prev) *                 math.cos(lat) * math.sin(dlon / 2) * math.sin(dlon / 2);         double c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a));          distance_travelled =  r * c;          latitude.settext(string.valueof(lat));         longitude.settext(string.valueof(lon));         distance.settext(string.valueof(distance_travelled));     }      /*private double torad(double d) {         return d * math.pi / 180;     }*/      @override     public void onstatuschanged(string s, int i, bundle bundle) {      }      @override     public void onproviderenabled(string s) {         toast.maketext(this, "enabled new provider " + provider,toast.length_short).show();     }      @override     public void onproviderdisabled(string s) {         toast.maketext(this, "disabled provider " + provider,toast.length_short).show();     } } 

main.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"               android:layout_width="match_parent"               android:layout_height="match_parent"               android:orientation="vertical" >      <linearlayout             android:id="@+id/linearlayout1"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:layout_margintop="40dip"             android:orientation="horizontal" >          <textview                 android:id="@+id/textview01"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:layout_marginleft="10dip"                 android:layout_marginright="5dip"                 android:text="latitude: "                 android:textsize="20dip" >         </textview>          <textview                 android:id="@+id/txt_latitude"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="unknown"                 android:textsize="20dip" >         </textview>     </linearlayout>      <linearlayout             android:id="@+id/linearlayout2"             android:layout_width="match_parent"             android:layout_height="wrap_content" >          <textview                 android:id="@+id/textview03"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:layout_marginleft="10dip"                 android:layout_marginright="5dip"                 android:text="longitute: "                 android:textsize="20dip" >         </textview>          <textview                 android:id="@+id/txt_longitude"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="unknown"                 android:textsize="20dip" >         </textview>      </linearlayout>      <linearlayout             android:layout_width="fill_parent"             android:layout_height="wrap_content">          <textview                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:textappearance="?android:attr/textappearancemedium"                 android:text="distance travelled:"                 android:id="@+id/textview"                 android:layout_gravity="left|center_vertical"                 android:layout_marginleft="10dp"                 android:layout_marginright="5dp"/>          <textview                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:textappearance="?android:attr/textappearancemedium"                 android:text="unknown"                 android:id="@+id/txt_distance"/>     </linearlayout>      <button             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="get location"             android:id="@+id/btn_getlocation"             android:layout_gravity="center"/>  </linearlayout> 

androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.example.geolocation"     android:versioncode="1"     android:versionname="1.0" >      <uses-permission android:name="android.permission.access_fine_location" />     <uses-permission android:name="android.permission.access_coarse_location " />     <uses-permission android:name="android.permission.internet" />      <uses-sdk         android:minsdkversion="7"         android:targetsdkversion="10" />      <application         android:allowbackup="true"         android:icon="@drawable/ic_launcher"         android:label="@string/app_name"         android:theme="@style/apptheme" >         <activity             android:name="com.example.geolocation.main"             android:label="@string/app_name" >             <intent-filter>                 <action android:name="android.intent.action.main" />                  <category android:name="android.intent.category.launcher" />             </intent-filter>         </activity>     </application>  </manifest> 

note:

i added button start tracking because i'm not sure if onlocationchanged working or not. hahaha!

basic approach distance:

public class test extends activity implements locationlistener{    private locationmanager lm;    private location last;    private long distance;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         lm = (locationmanager) getsystemservice(location_service);     }      @override     protected void onresume() {         super.onresume();         lm.requestlocationupdates(locationmanager.gps_provider,1000,1,this);      }      @override     protected void onpause() {         super.onpause();         lm.removeupdates(this);     }      @override     public void onlocationchanged(location location) {         if(last != null){             distance += location.distanceto(last);         }          last = new location(location);     }      @override     public void onstatuschanged(string s, int i, bundle bundle) {     }      @override     public void onproviderenabled(string s) {     }      @override     public void onproviderdisabled(string s) {     } } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -