嘿伙计们,我想获取用户位置的纬度和经度。我已经尝试过开发人员站点和堆栈溢出,有很多针对移动用户的示例,它们使用以下内容:- 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。
谢谢