java - Geolocation google map v2 -
i'm using google map v2 current location , itinerary mobile connected internet , gps work , result disappointing times have :" not available" , , same times gaves me location of laaast point connected not current here code
public class itineraryactivity extends activity { private static final latlng event_place = new latlng(33.59648, -7.664723); private static final latlng mountain_view = new latlng(37.4, -122.1); static final latlng hamburg = new latlng(53.558, 9.927); private googlemap map; private locationmanager service; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.mapevent); findviewbyid(r.id.direction).setvisibility(view.invisible); map = ((mapfragment) getfragmentmanager().findfragmentbyid(r.id.map)) .getmap(); map.setmaptype(googlemap.map_type_normal); marker hamburg = map.addmarker(new markeroptions() .position(event_place).title("our event") .snippet("we waiting ..")); map.movecamera(cameraupdatefactory.newlatlngzoom(event_place, 15)); /** geo */ service = (locationmanager) getsystemservice(location_service); boolean enabled = service .isproviderenabled(locationmanager.gps_provider); // activer gps if (!enabled) { intent intent = new intent(settings.action_location_source_settings); startactivity(intent); log.e("eeee", "law 3lem"); } else { log.e("eeee", "law "); } // ** criteria criteria = new criteria(); string provider = service.getbestprovider(criteria, false); location location = service.getlastknownlocation(provider); // initialize location fields if (location != null) { system.out.println("provider " + provider + " has been selected."); onlocationchanged(location); new routing(this, map, color.black).execute( new latlng(location.getlatitude(), location.getlongitude()), event_place); } else { log.e("", "not available"); } } public void onlocationchanged(location location) { double lat = (double) (location.getlatitude()); double lng = (double) (location.getlongitude()); log.e("lat + long", string.valueof(lat) + " / " + string.valueof(lng)); map.movecamera(cameraupdatefactory.newlatlngzoom(new latlng(lat, lng), 14)); map.addmarker(new markeroptions() .position(new latlng(lat, lng)) .title("vous êtes là") .snippet("nous comptons sur votre présence ...") .icon(bitmapdescriptorfactory .defaultmarker(bitmapdescriptorfactory.hue_blue))); map.animatecamera(cameraupdatefactory.zoomto(14), 2000, null); } }
try code
public class itineraryactivity extends activity implements locationlistener { public static final double = 6372.8; // in kilometers private static final latlng event_place = new latlng(33.59648, -7.664723); static final latlng hamburg = new latlng(53.558, 9.927); private googlemap map; private locationmanager service; private locationmanager locationmanager; private string provider; marker mark; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.mapevent); findviewbyid(r.id.direction).setvisibility(view.invisible); map = ((mapfragment) getfragmentmanager().findfragmentbyid(r.id.map)) .getmap(); map.setmaptype(googlemap.map_type_normal); marker hamburg = map.addmarker(new markeroptions() .position(event_place).title("our event") .snippet("we waiting ..")); map.movecamera(cameraupdatefactory.newlatlngzoom(event_place, 15)); /** geo */ service = (locationmanager) getsystemservice(location_service); boolean enabledgps = service .isproviderenabled(locationmanager.gps_provider); boolean enabledwifi = service .isproviderenabled(locationmanager.network_provider); if (!enabledgps) { toast.maketext(this, "gps signal not found", toast.length_long) .show(); intent intent = new intent(settings.action_location_source_settings); startactivity(intent); } locationmanager = (locationmanager) .getsystemservice(context.location_service); // boolean enabled = service .isproviderenabled(locationmanager.gps_provider); // activer gps criteria criteria = new criteria(); provider = locationmanager.getbestprovider(criteria, false); location location = locationmanager.getlastknownlocation(provider); // initialize location fields if (location != null) { toast.maketext(this, "selected provider " + provider, toast.length_short).show(); onlocationchanged(location); new routing(this, map, color.black) .execute( new latlng(location.getlatitude(), location .getlongitude()), event_place); } else { // } // initialize location fields } public void onlocationchanged(location location) { // mark.remove(); double lat = location.getlatitude(); double lng = location.getlongitude(); /*toast.maketext(this, " location " + lat + "," + lng, toast.length_long) .show();*/ latlng coordinate = new latlng(lat, lng); toast.maketext( this, "location " + coordinate.latitude + "," + coordinate.longitude + "\n distance :" + haversine(coordinate.latitude, coordinate.longitude, event_place.latitude, event_place.longitude) + "km", toast.length_long).show(); log.e("lat + long", string.valueof(lat) + " / " + string.valueof(lng)); map.movecamera(cameraupdatefactory.newlatlngzoom(new latlng(lat, lng), 14)); if (mark != null) { mark.remove(); } mark = map.addmarker(new markeroptions() .position(new latlng(lat, lng)) .title("vous êtes là") .snippet("nous comptons sur votre présence ...") .icon(bitmapdescriptorfactory .defaultmarker(bitmapdescriptorfactory.hue_blue))); map.animatecamera(cameraupdatefactory.zoomto(14), 2000, null); } @override public void onproviderdisabled(string provider) { // todo auto-generated method stub toast.maketext(this, "enabled new provider " + provider, toast.length_short).show(); } @override public void onproviderenabled(string provider) { // todo auto-generated method stub toast.maketext(this, " disabled provider " + provider, toast.length_short).show(); } @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub } /* request updates @ startup */ @override protected void onresume() { super.onresume(); locationmanager.requestlocationupdates(provider, 400, 10, this); } /* remove locationlistener updates when activity paused */ @override protected void onpause() { super.onpause(); locationmanager.removeupdates(this); } public double haversine(double lat1, double lon1, double lat2, double lon2) { double dlat = math.toradians(lat2 - lat1); double dlon = math.toradians(lon2 - lon1); lat1 = math.toradians(lat1); lat2 = math.toradians(lat2); double = math.sin(dlat / 2) * math.sin(dlat / 2) + math.sin(dlon / 2) * math.sin(dlon / 2) * math.cos(lat1) * math.cos(lat2); double c = 2 * math.asin(math.sqrt(a)); return * c; } }
Comments
Post a Comment