google maps - Get latitude and longitude for Android App (getLastKnownLocation is null) -
i latitude , longitude current location in app. testing on galaxy wi-fi 5.0. need numbers in double format gonna use in google-maps-url. problem lm.getlastknownlocation(locationmanager.gps_provider) returns null , nullpointerexception.
private void getlatitudelongitude() { locationmanager lm = (locationmanager)getsystemservice(context.location_service); location location = lm.getlastknownlocation(locationmanager.gps_provider); if(location != null) { showmyaddress(location); } final locationlistener locationlistener = new locationlistener() { public void onlocationchanged(location location) { showmyaddress(location); } public void onproviderdisabled(string arg0) { // todo auto-generated method stub } public void onproviderenabled(string arg0) { // todo auto-generated method stub } public void onstatuschanged(string arg0, int arg1, bundle arg2) { // todo auto-generated method stub } }; lm.requestlocationupdates(locationmanager.gps_provider, 2000, 10, locationlistener); geocoder mylocation = new geocoder(getapplicationcontext(), locale.getdefault()); try { system.out.println(mylocation.getfromlocation(latitude, longitude, 1).tostring()); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } private void showmyaddress(location location) { double latitude = location.getlatitude(); double longitude = location.getlongitude(); geocoder mylocation = new geocoder(getapplicationcontext(), locale.getdefault()); try { system.out.println( mylocation.getfromlocation(latitude, longitude, 1).tostring()); } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } }
Comments
Post a Comment