android - Getting Current Address using GPS -
i want current address using gps provider .i getting lat lng values 0 while use gps.i want current location without using wifi.and got exception when convert alt lng values address using geocoder ("unable parse response server")i unable please me
my code
public class mainactivity extends activity implements locationlistener, onclicklistener{ locationmanager locationmanager ; string provider; button btn; private string stradd; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); btn = (button)findviewbyid(r.id.button1); btn.setonclicklistener(this); // getting locationmanager object locationmanager = (locationmanager)getsystemservice(context.location_service); // creating empty criteria object criteria criteria = new criteria(); // getting name of provider meets criteria provider = locationmanager.getbestprovider(criteria, false); if(provider!=null && !provider.equals("")){ // location given provider location location = locationmanager.getlastknownlocation(provider); locationmanager.requestlocationupdates(provider, 10, 30, this); if(location!=null) onlocationchanged(location); else toast.maketext(getbasecontext(), "location can't retrieved", toast.length_short).show(); }else{ toast.maketext(getbasecontext(), "no provider found", toast.length_short).show(); } } @override public void onlocationchanged(location location) { // getting reference textview tv_longitude textview tvlongitude = (textview)findviewbyid(r.id.textview1); textview tvadd = (textview)findviewbyid(r.id.textview2); // getting reference textview tv_latitude //textview tvlatitude = (textview)findviewbyid(r.id.tv_latitude); // setting current longitude double lat = location.getlatitude(); double lng = location.getlongitude(); log.e("latlng", ""+lat+lng); geocoder geocoder = new geocoder(mainactivity.this, locale.getdefault()); try{ list<address> addresses = geocoder.getfromlocation(lat,lng, 1); if (addresses != null) { address returnedaddress = addresses.get(0); stringbuilder strreturnedaddress = new stringbuilder(""); (int = 0; < returnedaddress.getmaxaddresslineindex(); i++) { strreturnedaddress.append(returnedaddress.getaddressline(i)).append("\n"); } stradd = strreturnedaddress.tostring(); } } catch (exception e) { // todo: handle exception e.printstacktrace(); } tvlongitude.settext("lat lng:" + lat+lng); tvadd.settext("address:" + stradd); // setting current latitude // tvlatitude.settext("latitude:" + location.getlatitude() ); } @override public void onproviderdisabled(string provider) { // todo auto-generated method stub } @override public void onproviderenabled(string provider) { // todo auto-generated method stub } @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub }
permissions
<uses-permission android:name="android.permission.access_fine_location" /> <uses-permission android:name="android.permission.access_course_location" /> <uses-permission android:name="android.permission.write_external_storage" />
if you're getting 0 value in lat long think you're unable receive gps response. gps might take lot of time first fix upto 15 minutes depending on visibility of satellites. so, when you're using gps make sure you're in outdoor location faster fix.
also, you'll need include <uses-permission android:name="android.permission.internet" />
since geocoder need send lat lng online servers address.
Comments
Post a Comment