Android development - GPS not working - Application hangs when co-ordinates sent -
problem background:
i new android development. i'm trying do, now, gps coordinates gps device of phone , display them on google maps or in textview
, or whatever. main task them.
i've read number of tutorials. i'm using
- android 2.3.3
- api level 10
- eclipse 3.6.1
- windows 7
problem description :
i have written class geoupdatehandler
implements locationlistener
, implements methods onlocationchange()
etc. , in mymapsactivity
, use regular requestlocationupdates
of locationmanager
periodic updates.
when run application, send gps coordinates emulator control of eclipse (window --> show view --> other --> android --> emulator control) send longitude , latitude. emulated device gets coordinates, application hangs. nothing happens , cursor changes blue circle (which signifies thinking/stuck) , nothing happens. if use locationmanager.getlastknownlocation(string provider)
, gets null. obviously, because there no last known location. gets stuck before knowing one!
i have tried sending coordinates through telnet well. (cmd --> telnet localhost 5554 --> geo fix . same thing happens.
i have tried starting device independent of application, sending coordinates, , starting application. same thing happens when run application: hangs.
the following code helloitemizedoverlay
taken android's mapview tutorial , works fine manually given coordinates. problem arises when gps location tried retrieved.
public class mymapsactivity extends mapactivity { private mapcontroller mapcontroller; private mapview mapview; private locationmanager locationmanager; linearlayout linlayout; mapview mview; list<overlay> mapoverlays; drawable drawable; helloitemizedoverlay itemizedoverlay; geoupdatehandler handler; location location; public void oncreate(bundle bundle) { super.oncreate(bundle); setcontentview(r.layout.main); // bind layout activity mview = (mapview) findviewbyid(r.id.mapview); mview.setbuiltinzoomcontrols(true); mapoverlays = mview.getoverlays(); drawable = this.getresources().getdrawable(r.drawable.icon); itemizedoverlay = new helloitemizedoverlay(drawable); mapcontroller = mview.getcontroller(); locationmanager = (locationmanager) getsystemservice(context.location_service); handler = new geoupdatehandler(); locationmanager.requestlocationupdates(locationmanager.gps_provider, 3000, 0, handler); location = locationmanager.getlastknownlocation(locationmanager.gps_provider); geopoint point = new geopoint((int)location.getlongitude(),(int)location.getlatitude()); overlayitem overlayitem = new overlayitem(point, "", ""); mview.setbuiltinzoomcontrols(true); itemizedoverlay.addoverlay(overlayitem); mapoverlays.add(itemizedoverlay); mapcontroller.setzoom(8); mapcontroller.animateto(point); } @override protected boolean isroutedisplayed() { return false; } public class geoupdatehandler implements locationlistener { @override public void onlocationchanged(location location) { int lat = (int) (location.getlatitude()); int lng = (int) (location.getlongitude()); geopoint point = new geopoint(lat, lng); mapcontroller.animateto(point); // mapcontroller.setcenter(point); } @override public void onproviderdisabled(string provider) { } @override public void onproviderenabled(string provider) { } @override public void onstatuschanged(string provider, int status, bundle extras) { } } @override protected void onresume() { handler = new geoupdatehandler(); locationmanager = (locationmanager) getsystemservice(context.location_service); locationmanager.requestlocationupdates(locationmanager.gps_provider, 5000, 0, handler); super.onresume(); } }
according this, geopoint
constructor takes (latitude * 1e6, longitude * 1e6) parameters ; whereas put (longitude, latitude) when calling getlastknownlocation
, , forgot scale factor in other call (in onlocationchanged
). latitude , logitude not scaled in location objects.
anyways, error should result in displaying blue map ((0,0) being in atlantic ocean), there may other problems.
edit:
1/ in replay comment, if mix longitude , latitude, may unexisting point (latitude stays within [-90°,90°] whereas longitude can vary in [-180°,180°])
2/ there bug in sdk 2.3 (api level 9), makes emulator crash when sending mock locations. don't know wether true 2.3.3 (lvl 10), used 2.1u1 (lvl 7) test.
3/ following code works me :
package test.testmap; import java.util.list; import android.content.context; import android.graphics.drawable.drawable; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.util.log; import android.widget.linearlayout; import com.google.android.maps.geopoint; import com.google.android.maps.mapactivity; import com.google.android.maps.mapcontroller; import com.google.android.maps.mapview; import com.google.android.maps.overlay; import com.google.android.maps.overlayitem; public class mymapsactivity extends mapactivity { private mapcontroller mapcontroller; private locationmanager locationmanager; linearlayout linlayout; mapview mview; list<overlay> mapoverlays; drawable drawable; helloitemizedoverlay itemizedoverlay; geoupdatehandler handler; location location; public void oncreate(bundle bundle) { super.oncreate(bundle); setcontentview(r.layout.main); // bind layout activity drawable = this.getresources().getdrawable(r.drawable.icon); itemizedoverlay = new helloitemizedoverlay(drawable); mview = (mapview) findviewbyid(r.id.mapview); mview.setbuiltinzoomcontrols(true); mapoverlays = mview.getoverlays(); mapoverlays.add(itemizedoverlay); mapcontroller = mview.getcontroller(); } @override protected boolean isroutedisplayed() { return false; } public class geoupdatehandler implements locationlistener { @override public void onlocationchanged(location location) { log.d(this.getclass().getname(),"onlocationchanged : lat = "+location.getlatitude()+" lon = "+location.getlongitude()); int lat = (int) math.round(location.getlatitude()*1.0e6); int lng = (int) math.round(location.getlongitude()*1.0e6); geopoint point = new geopoint(lat, lng); itemizedoverlay.addoverlay(new overlayitem(point, "", "")); mapcontroller.animateto(point); mapcontroller.setzoom(8); } @override public void onproviderdisabled(string provider) { log.d(this.getclass().getname(),"onproviderdisabled"); } @override public void onproviderenabled(string provider) { log.d(this.getclass().getname(),"onproviderenabled"); } @override public void onstatuschanged(string provider, int status, bundle extras) { log.d(this.getclass().getname(),"onstatuschanged"); } } @override protected void onresume() { handler = new geoupdatehandler(); locationmanager = (locationmanager) getsystemservice(context.location_service); locationmanager.requestlocationupdates(locationmanager.gps_provider, 5000, 0, handler); super.onresume(); } }
Comments
Post a Comment