【发布时间】:2012-08-19 17:59:39
【问题描述】:
此代码用于我的 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");
}
}
我做错了什么?
【问题讨论】:
-
系统退出不会带您到任何地方。使用 android Log 类。
-
我只是在调试。 System.out.println 实际上打印在日志中。完成后我会删除它们。
-
静止。 android Log 类更有帮助
-
您确定该位置是由 GPS 提供的吗?你为什么不检查供应商呢?
标签: android android-mapview locationmanager android-location