LocationManagerAndroid上的API似乎对于仅需要偶尔粗略估计用户位置的应用程序来说有点痛苦.
我正在处理的应用程序本身并不是一个位置应用程序,但它确实需要获取用户的位置才能显示附近的商家列表.它不需要担心用户是否在移动或类似的东西.
这是我想做的事情:
ActivityX中需要它时,它将可用.看起来它应该不那么难,但在我看来,我必须启动两个不同的位置提供商(GPS和NETWORK)并管理每个生命周期.不仅如此,我还必须在多个活动中复制相同的代码以满足#2.我曾尝试过使用getBestProvider()过去将解决方案缩减为只使用一个位置提供商,但这似乎只能为您提供最好的"理论"提供商,而不是实际上会给您带来最佳结果的提供商.
有没有更简单的方法来实现这一目标?
我想在Android应用程序中找到我当前位置的纬度和经度.当我用手机稳定时,我只需要它.
我的代码是:
LocationManager locationManager;
locationManager = (LocationManager)getSystemService
(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation
(LocationManager.GPS_PROVIDER);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,0,0,(LocationListener) this);
updateWithNewLocation(location);
}
private void updateWithNewLocation(Location location) {
TextView myLocationText = (TextView)findViewById(R.id.myLocationText);
String latLongString;
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
} else {
latLongString = "No location found";
}
myLocationText.setText("Your Current Position is:\n" +
latLongString);
}
Run Code Online (Sandbox Code Playgroud)
在模拟器上运行后,我总是找到"找不到位置".为什么会这样?