java - Google Maps API V2 marker not showing on correct position -
i've been following series of tutorials make basic restaurant locator app android using google places maps api v2. problem having, gps location correct, marker drawn on map show users location approximately 60-65 miles south east of users actual location.
here code location class:
public class gpstracker extends service implements locationlistener { private final context mcontext; boolean isgpsenabled = false; boolean isnetworkenabled = false; boolean cangetlocation = false; location location = null; double latitude; double longitude; private static final long min_distance_change_for_updates = 10; // 10 meters private static final long min_time_bw_updates = 1000 * 60 * 1; // 1 minute protected locationmanager locationmanager; public gpstracker(context context) { this.mcontext = context; getlocation(); } public location getlocation() { try { locationmanager = (locationmanager) mcontext .getsystemservice(location_service); isgpsenabled = locationmanager .isproviderenabled(locationmanager.gps_provider); isnetworkenabled = locationmanager .isproviderenabled(locationmanager.network_provider); if (!isgpsenabled && !isnetworkenabled) { // no network provider enabled } else { this.cangetlocation = true; if (isnetworkenabled) { locationmanager.requestlocationupdates( locationmanager.network_provider, min_time_bw_updates, min_distance_change_for_updates, this); log.d("network", "network enabled"); if (locationmanager != null) { location = locationmanager .getlastknownlocation(locationmanager.network_provider); if (location != null) { latitude = location.getlatitude(); longitude = location.getlongitude(); } } } if (isgpsenabled) { if (location == null) { locationmanager.requestlocationupdates( locationmanager.gps_provider, min_time_bw_updates, min_distance_change_for_updates, this); log.d("gps", "gps enabled"); if (locationmanager != null) { location = locationmanager .getlastknownlocation(locationmanager.gps_provider); if (location != null) { latitude = location.getlatitude(); longitude = location.getlongitude(); } } } } } } catch (exception e) { e.printstacktrace(); } return location; } public void stopusinggps() { if (locationmanager != null) { locationmanager.removeupdates(gpstracker.this); } } public double getlatitude() { if (location != null) { latitude = location.getlatitude(); } return latitude; } public double getlongitude() { if (location != null) { longitude = location.getlongitude(); } return longitude; } public boolean cangetlocation() { return this.cangetlocation; }
and here code map activity:
public class placesmapactivity extends fragmentactivity { double latitude; double longitude; latlng user=null; latlng location=null; placeslist nearplaces; private googlemap map; private int usericon, locationicon; string address; string name; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.map_places); usericon = r.drawable.mark_red; locationicon = r.drawable.mark_blue; intent = getintent(); string user_latitude = i.getstringextra("user_latitude"); string user_longitude = i.getstringextra("user_longitude"); string user_location = (user_latitude + ", " + user_longitude); supportmapfragment fm = (supportmapfragment)getsupportfragmentmanager().findfragmentbyid(r.id.mapview); map = fm.getmap(); user = new latlng((int) (double.parsedouble(user_latitude)), (int) (double.parsedouble(user_longitude))); marker user = map.addmarker(new markeroptions().position(user) .title("this you.") .snippet(user_location) .icon(bitmapdescriptorfactory .fromresource(usericon))); nearplaces = (placeslist) i.getserializableextra("near_places"); if(nearplaces != null) { for(place place : nearplaces.results) { latitude = place.geometry.location.lat; longitude = place.geometry.location.lng; name = place.name; address= place.vicinity; } final latlngbounds.builder builder = new latlngbounds.builder(); for(int c = 0; c < nearplaces.results.size(); c++){ final latlng pos = new latlng(nearplaces.results.get(c).geometry.location.lat, nearplaces.results.get(c).geometry.location.lng); builder.include(pos); map.addmarker(new markeroptions().position(pos) .title(nearplaces.results.get(c).name) .snippet(nearplaces.results.get(c).vicinity) .icon(bitmapdescriptorfactory .fromresource(locationicon))); } } map.movecamera(cameraupdatefactory.newlatlngzoom(user, 14)); map.animatecamera(cameraupdatefactory.zoomto(9), 2000, null);
}
i tried attach screenshot, apparently can't yet. set marker snippet show users latitude , longitude when clicked. when click users marker shows correct longitude , latitude (checked them on internet, within block of actual location), stated marker user shows 60-65 miles south east. also, locations of places results correct , markers in correct locations.
any appreciated. cheers.
in code below
user = new latlng((int) (double.parsedouble(user_latitude)), (int) (double.parsedouble(user_longitude)));
don't convert latitude-longitude int
, instead keep them double
.
cheers !
Comments
Post a Comment