获取当前位置(gps / wifi)

时间:2010-04-23 14:13:13

标签: android location

我正在尝试使用它来获取我的位置:

LocationManager myLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

Criteria criteria = new Criteria(); 
criteria.setAccuracy(Criteria.ACCURACY_FINE); 
criteria.setAltitudeRequired(false); 
criteria.setBearingRequired(false); 
criteria.setCostAllowed(true); 
criteria.setPowerRequirement(Criteria.POWER_LOW); 

String provider = myLocationManager.getBestProvider(criteria,true); 

if (provider != null) { 
    //there is a provider
    myLocationManager.requestLocationUpdates(provider, 10L, 500.0f, (LocationListener) mainContext);
    Location myCurLocation = myLocationManager.getLastKnownLocation(provider); 
            //here i'm trying to get some data from the
            //myCurLocation but 
            //myCurLocation == NULL
}

但myCurLocation总是== NULL

我做错了什么?

1 个答案:

答案 0 :(得分:8)

getLastKnownLocation()的调用不会阻止 - 这意味着如果当前没有可用的位置,它将返回null - 因此您可能希望查看将LocationListener传递给相反,requestLocationUpdates() method会为您提供位置的异步更新。

查看this question for an example of using a LocationListener