【发布时间】:2011-06-30 05:24:04
【问题描述】:
我正在尝试使用 LocationListener 以及相关的类和方法来查找 android 中两个位置之间的距离。
- 我能够通过使用地理编码类获得给定地址的坐标。并且还能够使用 distanceto 方法获取两个地址之间的距离。
2.现在我想给出地址并获取坐标,然后实时获取先前给定地址位置与当前位置(变化)之间的距离。由于 android 中没有当前的定位方法,我必须使用 Location Listener。
- 当使用locationlistener和其他位置类和方法移动时,想要给定地址位置坐标和实时变化位置之间的距离。我在清单中具有所有权限的设备上编写和部署,但是一旦我启动应用程序,gps 就会被禁用。下面是我的代码
//下面代码获取地址位置坐标成功
int i=1;
Geocoder geo=new Geocoder(this);
List<Address> addressList = geo.getFromLocationName(adrs,i);
Address address = addressList.get(0);
if(address.hasLatitude() && address.hasLongitude()){
lat = address.getLatitude();
lon = address.getLongitude();
String adlalo= "Latitude of above address: "+lat +" " +
" "+"Longitude of above address: "+lon;
t2.setText(adlalo);
}
//下面是位置监听类
public class LoctListner implements LocationListener
{
public void onLocationChanged(Location loc)
{
t3 = (TextView) findViewById(R.id.textView3);
l1.setLatitude(lat);
l1.setLongitude(lon);
float d=l1.distanceTo(loc);
String s="the distance between starting and ending point is "+d;
t3.setText(s);
}
}
帮助我在哪里犯了错误。无法看到日志,因为我必须把设备拿出来测试。因为这个应用程序需要移动位置。提前致谢
【问题讨论】:
-
lat,lon 是双精度数,l1,l2 是在活动的 oncreate 之外声明的 Location 对象。