java - How to get Latitude and Longitude in android? -
how current latitude , longitude in android2.3.3?
i try follow this can't run programs.
my code
latview = (textview)findviewbyid(r.id.lattext); lngview = (textview)findviewbyid(r.id.lngtext); locationmanager lm = (locationmanager)getsystemservice(context.location_service); location location = lm.getlastknownlocation(locationmanager.gps_provider); double longitude = location.getlongitude(); double latitude = location.getlatitude();
in android manifest put this.
<uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_coarse_location" /> <uses-permission android:name="android.permission.access_fine_location" />
please help. thanks.
this have in application find device current location, works fine:
public class mylocationactivity extends activity implements locationlistener { private locationmanager mgr; private string best; public static double mylocationlatitude; public static double mylocationlongitude; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mgr = (locationmanager) getsystemservice(location_service); dumpproviders(); criteria criteria = new criteria(); best = mgr.getbestprovider(criteria, true); log.d("best provider", best); location location = mgr.getlastknownlocation(best); dumplocation(location); //may intent here } public void onlocationchanged(location location) { // todo auto-generated method stub dumplocation(location); } public void onproviderdisabled(string provider) { // todo auto-generated method stub } public void onproviderenabled(string provider) { // todo auto-generated method stub } public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub } @override protected void onpause() { super.onpause(); mgr.removeupdates(this); } @override protected void onresume() { super.onresume(); mgr.requestlocationupdates(best, 15000, 1, this); } private void dumplocation(location l) { if (l == null) { mylocationlatitude = 0.0; mylocationlongitude = 0.0; } else { mylocationlatitude = l.getlatitude(); mylocationlongitude = l.getlongitude(); } } private void dumpproviders() { list<string> providers = mgr.getallproviders(); (string p : providers) { dumpproviders(p); } } private void dumpproviders(string s) { locationprovider info = mgr.getprovider(s); stringbuilder builder = new stringbuilder(); builder.append("name: ").append(info.getname()); } }
here mylocationlatitude , mylocationlongitude gives values of lat , long needed.
Comments
Post a Comment