Cannot Determine Location In Android -
i trying create android showing user's location in map-view.but when run app says "can't determinate location" .i in stuck .can me code or link guide on how implement correctly?
here code:
public class mymaplocationactivity extends mapactivity { private mapview mapview; private mylocationoverlay mylocationoverlay; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // main.xml contains mapview setcontentview(r.layout.main); // extract mapview layout mapview = (mapview) findviewbyid(r.id.mapview); mapview.setbuiltinzoomcontrols(true); // create overlay shows our current location mylocationoverlay = new fixedmylocationoverlay(this, mapview); // add overlay mapview , refresh mapview.getoverlays().add(mylocationoverlay); mapview.postinvalidate(); // call convenience method zooms map on our location zoomtomylocation(); } @override protected void onresume() { super.onresume(); // when our activity resumes, want register location updates mylocationoverlay.enablemylocation(); } @override protected void onpause() { super.onpause(); // when our activity pauses, want remove listening location updates mylocationoverlay.disablemylocation(); } /** * method zooms user's location zoom level of 10. */ private void zoomtomylocation() { geopoint mylocationgeopoint = mylocationoverlay.getmylocation(); if(mylocationgeopoint != null) { mapview.getcontroller().animateto(mylocationgeopoint); mapview.getcontroller().setzoom(10); } else { toast.maketext(this, "cannot determine location", toast.length_short).show(); } } @override protected boolean isroutedisplayed() { return false; }}
fixedmylocationoverlay.java
public class fixedmylocationoverlay extends mylocationoverlay { private boolean bugged = false; private drawable drawable; private paint accuracypaint; private point center; private point left; private int width; private int height; public fixedmylocationoverlay(context context, mapview mapview) { super(context, mapview); } @override protected void drawmylocation(canvas canvas, mapview mapview, location lastfix, geopoint mylocation, long when) { if(!bugged) { try { super.drawmylocation(canvas, mapview, lastfix, mylocation, when); } catch (exception e) { // found buggy phone, draw location icons ourselves bugged = true; } } if(bugged) { if(drawable == null) { accuracypaint = new paint(); accuracypaint.setantialias(true); accuracypaint.setstrokewidth(2.0f); drawable = mapview.getcontext().getresources().getdrawable(r.drawable.ic_maps_indicator_current_position); width = drawable.getintrinsicwidth(); height = drawable.getintrinsicheight(); center = new point(); left = new point(); } projection projection = mapview.getprojection(); double latitude = lastfix.getlatitude(); double longitude = lastfix.getlongitude(); float accuracy = lastfix.getaccuracy(); float[] result = new float[1]; location.distancebetween(latitude, longitude, latitude, longitude + 1, result); float longitudelinedistance = result[0]; geopoint leftgeo = new geopoint((int)(latitude*1e6), (int)((longitude-accuracy/longitudelinedistance)*1e6)); projection.topixels(leftgeo, left); projection.topixels(mylocation, center); int radius = center.x - left.x; accuracypaint.setcolor(0xff6666ff); accuracypaint.setstyle(style.stroke); canvas.drawcircle(center.x, center.y, radius, accuracypaint); accuracypaint.setcolor(0x186666ff); accuracypaint.setstyle(style.fill); canvas.drawcircle(center.x, center.y, radius, accuracypaint); drawable.setbounds(center.x - width/2, center.y - height/2, center.x + width/2, center.y + height/2); drawable.draw(canvas); } }}
in manifest file add below this:
<uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="com.google.android.providers.gsf.permission.read_gservices" /> <uses-permission android:name="android.permission.write_external_storage" /> <!-- required show current location --> <uses-permission android:name="android.permission.access_coarse_location" /> <uses-permission android:name="android.permission.access_fine_location" />
below link useful, worked me:
http://ramsandroid4all.blogspot.in/2013/06/google-maps-android-api-v2-showing.html
the above link explains 2 things,
- how implement google map version 2 in android.
- how implement location listener determine user location.
the overall code shows how current location using network_provider , display marker on google map. device need have google map installed , internet connection run properly. below code important part determine user location when changes position:
@override public void onlocationchanged(location location) { map.clear(); markeroptions mp = new markeroptions(); mp.position(new latlng(location.getlatitude(), location.getlongitude())); mp.title("my position"); map.addmarker(mp); map.animatecamera(cameraupdatefactory.newlatlngzoom( new latlng(location.getlatitude(), location.getlongitude()), 16)); }
Comments
Post a Comment