Is it possible to get the current location in Android MapView without using a location receiver?

I have a MapView application. When the application starts, it should show the current location. How to do it?

0
source share
2 answers

try it

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();
                 :
       }

}
+1
source

Using the location manager, you can: get the current location

Location Provider Selection

String providerName = LocationManager.GPS_PROVIDER;
LocationProvider gpsProvider;
gpsProvider = locationManager.getProvider(providerName);

Search for 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);

Learn more Visit: http://developer.android.com/reference/android/location/LocationManager.html

+1
source

All Articles