Map does not open to user's location in Android -
whenever run android code , map opens on real device (not on emulator), not pointing anywhere , shows map of world zoomed out instead of user's location. have tried various various combinations right , nothing has helped me.
below code tried getting user's , still behavior remains same. tried changing gps_service network_provider , still no help. might solution here?
the device using testing here 1 plus 3.
manifest.xml:
<uses-permission android:name="android.permission.access_fine_location" /> <uses-permission android:name="android.permission.internet" />
mapsactivity:
public class mapsactivity extends fragmentactivity implements onmapreadycallback { private googlemap mmap; locationmanager locationmanager; locationlistener locationlistener; @override public void onrequestpermissionsresult(int requestcode, @nonnull string[] permissions, @nonnull int[] grantresults) { super.onrequestpermissionsresult(requestcode, permissions, grantresults); if(requestcode == 1) { if (grantresults.length > 0 && grantresults[0] == packagemanager.permission_granted) { if (contextcompat.checkselfpermission(this, manifest.permission.access_fine_location) == packagemanager.permission_granted) { locationmanager.requestlocationupdates(locationmanager.network_provider, 0, 0, locationlistener); } } } } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_maps); // obtain supportmapfragment , notified when map ready used. supportmapfragment mapfragment = (supportmapfragment) getsupportfragmentmanager() .findfragmentbyid(r.id.map); mapfragment.getmapasync(this); } /** * manipulates map once available. * callback triggered when map ready used. * can add markers or lines, add listeners or move camera. in case, * add marker near sydney, australia. * if google play services not installed on device, user prompted install * inside supportmapfragment. method triggered once user has * installed google play services , returned app. */ @override public void onmapready(googlemap googlemap) { mmap = googlemap; // mmap.setmaptype(googlemap.map_type_hybrid); locationmanager = (locationmanager) this.getsystemservice(context.location_service); locationlistener = new locationlistener() { @override public void onlocationchanged(location location) { // add marker on user's location , move camera. latlng yourlocation = new latlng(location.getlatitude(), location.getlongitude()); mmap.clear(); mmap.addmarker(new markeroptions().position(yourlocation).title("your location")); mmap.movecamera(cameraupdatefactory.newlatlng(yourlocation)); // address of user location. geocoder geocoder = new geocoder(getapplicationcontext(), locale.getdefault()); try { // 1 - represents 1 location required. list<address> listaddresses = geocoder.getfromlocation(location.getlatitude(), location.getlongitude(), 1); if(listaddresses != null && listaddresses.size() > 0) { log.i("placeinfo", listaddresses.get(0).tostring()); string address = ""; if(listaddresses.get(0).getsubthoroughfare() != null) { address += listaddresses.get(0).getsubthoroughfare() + " "; } if(listaddresses.get(0).getthoroughfare() != null) { address += listaddresses.get(0).getthoroughfare() + ", "; } if(listaddresses.get(0).getlocality() != null) { address += listaddresses.get(0).getlocality() + ", "; } if(listaddresses.get(0).getpostalcode() != null) { address += listaddresses.get(0).getpostalcode() + ", "; } if(listaddresses.get(0).getcountryname() != null) { address += listaddresses.get(0).getcountryname(); } toast.maketext(mapsactivity.this, address, toast.length_long).show(); log.i("address", address); } } catch (ioexception e) { e.printstacktrace(); } } @override public void onstatuschanged(string s, int i, bundle bundle) { } @override public void onproviderenabled(string s) { } @override public void onproviderdisabled(string s) { } }; if(build.version.sdk_int < 23) { locationmanager.requestlocationupdates(locationmanager.network_provider, 0, 0, locationlistener); } else { if (contextcompat.checkselfpermission(this, manifest.permission.access_fine_location) != packagemanager.permission_granted) { activitycompat.requestpermissions(this, new string[]{manifest.permission.access_fine_location}, 1); } else { locationmanager.requestlocationupdates(locationmanager.network_provider, 0, 0, locationlistener); // users's current location. location lastknownlocation = locationmanager.getlastknownlocation(locationmanager.network_provider); latlng yourlocation = new latlng(lastknownlocation.getlatitude(), lastknownlocation.getlongitude()); mmap.clear(); mmap.addmarker(new markeroptions().position(yourlocation).title("your location")); mmap.movecamera(cameraupdatefactory.newlatlng(yourlocation)); } } } }
Comments
Post a Comment