Android Wear get GPS Coordinates -
i'm unable location on android wear emulator. i've tried many tutorials non worked , don't understand whats wrong. below sample source code pretty similar original android demo google. onlocationchanged never gets executed , when try retrieve last location alway got null
import android.app.activity; import android.location.location; import android.location.locationlistener; import android.os.bundle; import android.support.wearable.view.watchviewstub; import android.view.view; import android.widget.textview; import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.api.googleapiclient; import com.google.android.gms.common.api.resultcallback; import com.google.android.gms.common.api.status; import com.google.android.gms.location.locationrequest; import com.google.android.gms.location.locationservices; import com.google.android.gms.wearable.wearable; public class mainactivity extends activity implements googleapiclient.connectioncallbacks, googleapiclient.onconnectionfailedlistener, com.google.android.gms.location.locationlistener { private textview mtextview; private static final long update_interval_ms = 2000; private static final long fastest_interval_ms = 1000; private googleapiclient mgoogleapiclient; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final watchviewstub stub = (watchviewstub) findviewbyid(r.id.watch_view_stub); stub.setonlayoutinflatedlistener(new watchviewstub.onlayoutinflatedlistener() { @override public void onlayoutinflated(watchviewstub stub) { mtextview = (textview) stub.findviewbyid(r.id.text); } }); mgoogleapiclient = new googleapiclient.builder(this) .addapi(locationservices.api) .addapi(wearable.api) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .build(); system.out.println("connect playservices..."); mgoogleapiclient.connect(); } @override public void onconnected(bundle bundle) { system.out.println("onconnected"); locationrequest locationrequest = locationrequest.create() .setpriority(locationrequest.priority_high_accuracy) .setinterval(update_interval_ms) .setfastestinterval(fastest_interval_ms); locationservices.fusedlocationapi .requestlocationupdates(mgoogleapiclient, locationrequest, this) .setresultcallback(new resultcallback<status>() { @override public void onresult(status status) { if (status.getstatus().issuccess()) { system.out.println("successfully requested location updates"+status.getstatusmessage()); } else { system.out.println( "failed in requesting location updates, " + "status code: " + status.getstatuscode() + ", message: " + status .getstatusmessage()); } } }); } public void getlocation(view view) { location loc=locationservices.fusedlocationapi.getlastlocation(mgoogleapiclient); if(loc!=null) { system.out.println("location: " + loc.getaltitude() + "\n" + loc.getprovider() + "\n" + loc.getlongitude()); } else { system.out.println("location null"); } } @override public void onconnectionsuspended(int i) { system.out.println("onconnectionsuspended(): connection location client suspended"); locationservices.fusedlocationapi.removelocationupdates(mgoogleapiclient, this); } @override public void onconnectionfailed(connectionresult connectionresult) { system.out.println("onconnectionfailed(): connection location client failed"); } @override public void onlocationchanged (location location) { system.out.println("onlocationchanged: new location"+location.getaltitude()); } @override protected void onstop() { super.onstop(); if (mgoogleapiclient.isconnected()) { locationservices.fusedlocationapi.removelocationupdates(mgoogleapiclient, this); } mgoogleapiclient.disconnect(); system.out.println("onstop"); } @override protected void onresume() { super.onresume(); mgoogleapiclient.connect(); system.out.println("onresume"); } @override protected void onpause() { super.onpause(); if (mgoogleapiclient.isconnected()) { locationservices.fusedlocationapi .removelocationupdates(mgoogleapiclient, this); } mgoogleapiclient.disconnect(); system.out.println("onpause"); } }
Comments
Post a Comment