【问题标题】:Is it posible to get the current location in Android MapView without using Location Listener?是否可以在不使用位置侦听器的情况下获取 Android MapView 中的当前位置?
【发布时间】:2011-03-09 04:38:32
【问题描述】:

我有一个 MapView 应用程序。当应用程序启动时,它应该显示当前位置。如何做到这一点?

【问题讨论】:

    标签: android


    【解决方案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();
                     :
           }
    
    }
    

    【讨论】:

    【解决方案2】:

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

    选择位置提供商

    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

    【讨论】:

    • 是啊,好像很多白痴连看cmets都不看就给出垃圾答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    • 2011-07-15
    相关资源
    最近更新 更多