是否可以在不使用位置监听器的情况下获取Android MapView中的当前位置?

时间:2011-03-09 04:38:33

标签: android

我有一个MapView应用程序。应用程序启动时,它应显示当前位置。怎么做?

2 个答案:

答案 0 :(得分:1)

使用位置管理器,您可以:获取当前位置

选择位置提供商

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

使用标准查找位置提供商

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

详情请访问:http://developer.android.com/reference/android/location/LocationManager.html

答案 1 :(得分:1)

试试这个

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

}