Reputation: 6687
I have a MapView application. When application launches it should show the current location. How to do this?
Upvotes: 0
Views: 2072
Reputation: 2785
Try this
public class MyLocationOnMap extends MapActivity {
private LocationManager hdLocMgr;
private String hdLocProvider;
onCreate(...) {
:
Criteria hdCrit = new Criteria();
hdCrit.setAccuracy(Criteria.ACCURACY_COARSE);
hdCrit.setAltitudeRequired(false);
hdCrit.setBearingRequired(false);
hdCrit.setCostAllowed(true);
hdCrit.setPowerRequirement(Criteria.POWER_LOW);
hdLocMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
hdLocProvider = hdLocMgr.getBestProvider(hdCrit, true);
Location location = hdLocMgr.getLastKnownLocation(hdLocProvider);
Double dlat = location.getLatitude();
Double dlon = location.getLongitude();
:
}
}
Upvotes: 1
Reputation: 267
Using the Location Manager, you can : Obtain your current location
Selecting a location provider
String providerName = LocationManager.GPS_PROVIDER;
LocationProvider gpsProvider;
gpsProvider = locationManager.getProvider(providerName);
Finding Location Providers Using Criteria
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
Criteria.setCostAllowed(true);
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location =
locationManager.getLastKnownLocation(bestProvider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(bestProvider , 2000, 10,
locationListener);
For details Visit : http://developer.android.com/reference/android/location/LocationManager.html
Upvotes: 1