此代码用于mapactivity中的oncreate块

lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
        {
            Toast.makeText(this,
                    "Por favor, habilita el GPS antes de usar esta opción",
                    Toast.LENGTH_SHORT).show();
            finish();
        }
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        doYourMapMagicBaby(loc);

不管怎样,loc总是返回null。不管怎样,当我在GPS上注册更新时,Logcat会正确记录我的位置。
08-24 09:21:18.479: D/libloc(260): Latitude:  40.4399979 (intermediate)
08-24 09:21:57.500: D/libloc(260): Longitude: -3.6700022

所以我想我可以通过onLocationChanged()方法实现我想做的事情。也没有运气。
public void onLocationChanged(Location location)
{
    if (location != null)
    {
        System.out.println("La localizacion es: (lon: "
                + location.getLongitude() + ", lat: "
                + location.getLatitude() + ")");
        doYourMapMagicBaby(location);
    }
    else
    {
        System.out.println("La location es nula");
    }

}

我做错什么了?

最佳答案

请检查提供程序是否当前已启用,因为如果提供程序当前已禁用,则返回null。
http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation(java.lang.String

10-07 19:37
查看更多