我使用lat / lng查找我的用户所在位置并计算他与地图中其他位置之间的距离(其lat / lng是静态的)。为此,我使用:
private void findloc() {
// TODO Auto-generated method stub
try{
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
Double lat = Double.valueOf(location.getLatitude());
Double lng = Double.valueOf(location.getLongitude());
Location location2 = new Location("gps");
location2.setLatitude(35.519583);
location2.setLongitude(24.016864);
float distance = location2.distanceTo(location);
TextView sitesdistancekm = (TextView) findViewById(R.id.sitesdistancekm);
sitesdistancekm.setText("Geographical Distance : "+distance/1000+" km");
Toast.makeText(otto.this, "lat: "+lat+"\nlng: "+lng,
Toast.LENGTH_LONG).show();
}
catch(Exception e){
TextView sitesdistancekm = (TextView) findViewById(R.id.sitesdistancekm);
sitesdistancekm.setText("No GPS signal");
}
}
我的问题是,有时位置堆栈并且我的距离错误。我调用findloc()作为菜单的刷新但它没有改变。
public boolean onOptionsItemSelected(MenuItem item) {
findloc();
return true;
}
有没有办法改善它?有什么想法吗?
修改
这会起作用吗?
private void findloc() {
// TODO Auto-generated method stub
try{
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
Double lat = Double.valueOf(location.getLatitude());
Double lng = Double.valueOf(location.getLongitude());
location2 = new Location("gps");
location2.setLatitude(35.519583);
location2.setLongitude(24.016864);
float distance = location2.distanceTo(location);
TextView sitesdistancekm = (TextView) findViewById(R.id.sitesdistancekm);
sitesdistancekm.setText("Geographical Distance : "+distance/1000+" km");
Toast.makeText(otto.this, "lat: "+lat+"\nlng: "+lng,
Toast.LENGTH_LONG).show();
onLocationChanged(location);
}
catch(Exception e){
TextView sitesdistancekm = (TextView) findViewById(R.id.sitesdistancekm);
sitesdistancekm.setText("No GPS signal");
}
}
private void onLocationChanged(Location location) {
// TODO Auto-generated method st
float distance = location2.distanceTo(location);
TextView sitesdistancekm = (TextView) findViewById(R.id.sitesdistancekm);
sitesdistancekm.setText("Geographical Distance : "+distance/1000+" km");
}
答案 0 :(得分:2)
我发现getLastKnownLocation()不如注册位置监听器那么可靠。有关如何实施的详细信息,请参阅this帖子。
getLastKnownLocation docs state:
返回指示上一个已知位置的数据的位置 修复从给定的提供者获得。这可以不用 启动提供者。请注意,此位置可能已过时, 例如,如果设备已关闭并移至另一个设备 位置。
对我来说,我使用getLastKnownLocation()来获取初始位置,但随后注册了一个侦听器。
答案 1 :(得分:1)
参考Jack的回答,不应将getLastKnownLocation用作位置检测的唯一措施。您需要将位置检测逻辑划分为几个步骤:
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
// you need to specify the criteria here
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String bestProvider = lm.getBestProvider(criteria, true);
Location locate = lm.getLastKnownLocation(bestProvider);
if(System.currentTimeMillis() - locate.getTime() < TIME_TO_EXPIRE) {
// this is a valid location, let's use this location
// as the last time the provider check is within reasonable boundary
}
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MIN_TIME, MIN_DISTANCE, locationListener);
如您所见,您不会手动调用onLocationChange,而是在超出MIN_TIME和MIN_DISTANCE时让提供程序调用侦听器。
请注意,如果您的位置未更改,则不会调用onLocationChanged()。如果在没有调用onLocationChanged的情况下经过了某段时间,您可以假设您的上一个已知位置是最准确的位置,您应该使用它。
还有可能无法调用onLocationChanged(),因为提供程序无法修复您的位置。在这种情况下,您可以选择调用其他商品以查看他们是否可以在依赖最后已知位置之前为您提供位置更新