google maps - android locationListener not working? -
i having trouble using loactionlistener in eclipse android. have been googling while can't seem see why shouldn't work. thing can think of maybe because testing device has no sim. (the internet provided via wifi).
i have used this reference , still, nothing.
could me problem.
here relevant parts of activity:
public class mainmenu extends activity implements locationlistener{ @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main_menu); themap = ((mapfragment)getfragmentmanager().findfragmentbyid(r.id.the_map)).getmap(); themap.setmaptype(googlemap.map_type_satellite); locman = (locationmanager)getsystemservice(location_service); } @override public void onlocationchanged(location location) { final double lat = location.getlatitude(); final double lng = location.getlongitude(); latlng lastlatlng = new latlng(lat, lng); string title = getstring(new stringlang().textset(userlang,"marker_title")); string snippit = getstring(new stringlang().textset(userlang,"marker_snip")); if(usermarker!=null) usermarker.remove(); usermarker = themap.addmarker(new markeroptions() .position(lastlatlng) .title(title) .icon(bitmapdescriptorfactory.fromresource(r.drawable.ic_launcher)) .snippet(snippit)); reversegeocode(lat, lng); } }
i using different method display location on map, worked never updated. showed location @ last place used gps, turns out 60mile accross country. can see working there better way of doing this.
old method:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main_menu); if(findviewbyid(r.id.the_map) != null){ //map has loaded continue themap = ((mapfragment)getfragmentmanager().findfragmentbyid(r.id.the_map)).getmap(); themap.setmaptype(googlemap.map_type_satellite); locman = (locationmanager)getsystemservice(context.location_service); android.location.location lastloc = locman.getlastknownlocation(locationmanager.network_provider); latlng lastlatlng ; if(lastloc == null){ /* use locationmanager class obtain gps locations */ double latitude = 0; double longitude = 0; lastlatlng = new latlng(latitude, longitude); lat = latitude; lng = longitude; string title = getstring(new stringlang().textset(userlang,"marker_title")); string snippit = getstring(new stringlang().textset(userlang,"marker_snip")); if(usermarker!=null) usermarker.remove(); usermarker = themap.addmarker(new markeroptions() .position(lastlatlng) .title(title) .icon(bitmapdescriptorfactory.fromresource(r.drawable.your_loc_icon)) .snippet(snippit)); cameraposition cameraposition = new cameraposition.builder() .target(lastlatlng) // sets center of map user position .zoom(0) // sets zoom .bearing(90) // sets orientation of camera east .tilt(20) // sets tilt of camera 30 degrees .build(); // creates cameraposition builder currentloc = lastlatlng; themap.animatecamera (cameraupdatefactory.newcameraposition(cameraposition), 3000, null); final textview geotagtext = (textview)findviewbyid(r.id.text_geotag); geotagtext.settext("we cannot find current location, please check settings."); }else{ double latitude = lastloc.getlatitude(); double longitude = lastloc.getlongitude(); lastlatlng = new latlng(latitude, longitude); lat = latitude; lng = longitude; animatemap(lastlatlng); } }else{ themap = ((mapfragment)getfragmentmanager().findfragmentbyid(r.id.the_map)).getmap(); themap.setmaptype(googlemap.map_type_satellite); locman = (locationmanager)getsystemservice(context.location_service); android.location.location lastloc = locman.getlastknownlocation(locationmanager.network_provider); latlng lastlatlng; double latitude = lastloc.getlatitude(); double longitude = lastloc.getlongitude(); lastlatlng = new latlng(latitude, longitude); lat = latitude; lng = longitude; animatemap(lastlatlng); //no map load } } @override public void onlocationchanged(location location) { final double lat = location.getlatitude(); final double lng = location.getlongitude(); latlng lastlatlng = new latlng(lat, lng); string title = getstring(new stringlang().textset(userlang,"marker_title")); string snippit = getstring(new stringlang().textset(userlang,"marker_snip")); if(usermarker!=null) usermarker.remove(); usermarker = themap.addmarker(new markeroptions() .position(lastlatlng) .title(title) .icon(bitmapdescriptorfactory.fromresource(r.drawable.ic_launcher)) .snippet(snippit)); }
it useful add there button relocate user manually if want.
relocbtn.setonclicklistener(new onclicklistener(){ @override public void onclick(view arg0) { if(menuactive == true){ playsound(); locationmanager loc = (locationmanager)getsystemservice(context.location_service); android.location.location gpsloc = (location) loc.getlastknownlocation(locationmanager.gps_provider); double lat = gpsloc.getlatitude(); double lng = gpsloc.getlongitude(); latlng lastlatlng = new latlng(lat, lng); animatemap(lastlatlng); } } });
i'm not sure why either method doesn't update, second method seams work better on first load.
you don't appear using requestlocationupdates() anywhere. in method pass locationlistener
object calls location updates.
i.e. in case:
locman.requestlocationupdates(locationmanager.gps_provider, 0, 0, this); locman.requestlocationupdates(locationmanager.network_provider, 0, 0, this);
Comments
Post a Comment