java - How can I get the longitude and latitude in my android app? -
i've been trying location
particular phone while now. have used following code instead of showing location crash when try , run app in emulator.
once i've got longitude , latitude going use map current location on google map!
public class homescreen extends actionbaractivity implements locationlistener { final textview t = (textview)findviewbyid(r.id.textview1); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_homescreen); locationmanager locationmanager = (locationmanager) getsystemservice(context.location_service); locationmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0, this); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.homescreen, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } @override public void onlocationchanged(location location) { // todo auto-generated method stub int latitude = (int) (location.getlatitude()); int longitude = (int) (location.getlongitude()); t.settext(latitude); // log.i("geo_location", "latitude: " + latitude + ", longitude: " + longitude); }
try class..
public class mainactivity extends service implements locationlistener { ..... // minimum distance change updates in meters private static final long min_distance_change_for_updates = 10; // 10 meters // minimum time between updates in milliseconds private static final long min_time_bw_updates = 1000 * 60 * 1; // 1 minute location location; // location double latitude; // latitude double longitude; // longitude public location getlocation() { try { locationmanager locationmanager = (locationmanager).getaplicationcontext() .getsystemservice(location_service); // getting gps status boolean isgpsenabled = locationmanager .isproviderenabled(locationmanager.gps_provider); // getting network status boolean isnetworkenabled = locationmanager .isproviderenabled(locationmanager.network_provider); if (!isgpsenabled && !isnetworkenabled) { // no network provider enabled } else { // first location network provider if (isnetworkenabled) { locationmanager.requestlocationupdates( locationmanager.network_provider, min_time_bw_updates, min_distance_change_for_updates, this); log.d("network", "network"); if (locationmanager != null) { location = locationmanager .getlastknownlocation(locationmanager.network_provider); if (location != null) { latitude = location.getlatitude(); longitude = location.getlongitude(); } } } // if gps enabled lat/long using gps services if (isgpsenabled) { if (location == null) { locationmanager.requestlocationupdates( locationmanager.gps_provider, min_time_bw_updates, min_distance_change_for_updates, this); log.d("gps enabled", "gps enabled"); if (locationmanager != null) { location = locationmanager .getlastknownlocation(locationmanager.gps_provider); if (location != null) { latitude = location.getlatitude(); longitude = location.getlongitude(); } } } } } } catch (exception e) { e.printstacktrace(); } return location; }
the double values latitude longitude returns lon,lat
Comments
Post a Comment