{Android App}我如何显示距离当前位置和指定点的距离?
问题描述:
我研究这几个小时,唯一的答案,我看到我指向 http://developer.android.com/reference/android/location/Location.html{Android App}我如何显示距离当前位置和指定点的距离?
和使用方法
公共静态无效distanceBetween(双startLatitude,双startLongitude,双endLatitude,双endLongitude,浮动[]结果)
我需要帮助理解这是如何工作与我的应用程序。这是我如何检索我的位置。
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
}
private void updateWithNewLocation(Location location) {
String latLongString;
TextView myLocationText;
myLocationText = (TextView)findViewById(R.id.myLocationText);
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);
}
可有人请帮我出一点点了解如何导入我的当前位置到这个方程,然后让我的应用程序显示的距离是多少? 谢谢
答
创建一个位置监听器
方法isAccurateForUse是简单地检查位置精度的自定义方法。
这应该工作:
public boolean locChanged = false;
public LocationManager locMgr = null;
public Location referenceL = null; // Your reference location
public Location currentKL = null; // Current known location
public Location currentLKL = null; // Current last known location
//Initialise the GPS listener
locMgr = (LocationManager) appCtx.getSystemService(Context.LOCATION_SERVICE);
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, fixGpsListener);
currentLKL = locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//Define the GPS listener
LocationListener fixGpsListener = new LocationListener()
{
public void onLocationChanged(Location location)
{
locChanged = true;
if (currentKL == null)
currentKL = location;
else if (isAccurateForUse())
{
currentLKL = currentKL;
currentKL = location;
updateDistance(meterToKilometer(currentKL.distanceTo(referenceL)));
}
}
public void onProviderDisabled(String provider)
{}
public void onProviderEnabled(String provider)
{}
public void onStatusChanged(String provider, int status, Bundle extras)
{}
};
currentKL.distanceTo(referenceL)>>给出米
meterToKilometer的距离>>转换米到千平方米
updateDistance >>这种方法需要一个字符串并更新显示距离的文本视图。
干杯
答
public double calcdist()
{
int MILLION = 1000000;
int EARTH_RADIUS_KM = 6371;
double lat1 = la1/MILLION;// latitude of location 1
double lon1 = lo1/MILLION; //longitude of location 1
double lat2 = la2/MILLION; //latitude of location 2
double lon2 = lo2/MILLION;//longitude of location 2
double lat1Rad = Math.toRadians(lat1);
double lat2Rad = Math.toRadians(lat2);
double deltaLonRad = Math.toRadians(lon2 - lon1);
double dist = Math.acos(Math.sin(lat1Rad) * Math.sin(lat2Rad) + Math.cos(lat1Rad) * Math.cos(lat2Rad)
* Math.cos(deltaLonRad))
* EARTH_RADIUS_KM;
return dist;
}
您可以使用此代码,如果你已经有纬度和两个位置的经度。
首先你没有得到*当前位置,使用[requestSingleUpdate()](http://developer.android.com/reference/android/location/LocationManager.html)来做到这一点。其次,你正在计算当前位置和....之间的距离? – Reno 2011-03-04 03:41:38