【发布时间】:2011-03-16 20:31:40
【问题描述】:
大家好 我想获取用户位置的纬度和经度。 我已经尝试过开发人员站点和堆栈溢出,有很多针对移动用户的示例,它们使用以下内容:- onLocationChanged() 我不需要更新位置,用户只需打开一两分钟 所以我需要找到应用程序启动时的位置,如果用户之前从未使用过任何地理定位应用程序怎么办,因此我也想要一个替代方案 LocationManager.getLastKnownLocation()
我的代码是:-
LocationManager lm = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
// connect to the GPS location service
Location loc = lm.getLastKnownLocation(lm.NETWORK_PROVIDER);
Toast.makeText(currentLocation.this, "Latitude -> "+Double.toString(loc.getLatitude())
+" Longitude is -> "+Double.toString(loc.getLongitude()), Toast.LENGTH_LONG).show();
并且工作正常。 但我担心第一次使用 getLastKnownLocation 可能会返回 null。
谢谢
【问题讨论】:
-
您已经提出了 9 个问题,但从未接受过答案。接受您之前问题的正确答案,更多用户会关注您的新问题。
标签: android geolocation locationmanager