Android 地理点与位置

Android 地理点与位置,android,location,distance,geo,Android,Location,Distance,Geo,这可能是一个noob问题,但我不是100%确定 如何使用地理点创建位置对象?我想用它来计算两点之间的距离。 我已经找到了一条写着 Location loc1 = new Location("Location"); loc.setLatitude(geoPoint.getLatitudeE6); loc.setLongitude(geoPoint.getLongitudeE6); Location loc2 = new Location("Location2"); loc2.setLatitu

这可能是一个noob问题,但我不是100%确定

如何使用地理点创建位置对象?我想用它来计算两点之间的距离。 我已经找到了一条写着

Location loc1 = new Location("Location"); 
loc.setLatitude(geoPoint.getLatitudeE6);
loc.setLongitude(geoPoint.getLongitudeE6);

Location loc2 = new Location("Location2");
loc2.setLatitude(geoPoint.getLatitudeE6);
loc2.setLongitude(geoPoint.getLongitudeE6);
然后我将使用distanceTo()得到两点之间的距离

我的问题 Providername的用途是什么。。。新位置(“这是什么?”) 那么我必须在之前定义一个提供者吗? 我想在for()中使用这段代码在多个地质点之间计算。 顺便说一句,我必须将E6值转换回正常值

不完全是

loc.setLatitude()采用双纬度。因此正确的代码是:

  loc.setLatitude( geoPoint.getLatitudeE6() / 1E6);

Location()构造函数采用所用GPS提供程序的名称。它可以是LocationManager.GPS\u PROVIDER或NETWORK\u PROVIDER以及其他值

要获得两点之间的距离,您可以使用该类,更精确地说是静态方法

文档非常清楚它的功能,但这里有一个快速代码示例:

float[] results = new float[3];
Location.distanceBetween(destLatitude, destLongitude, mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude(), results);
// result in meters, convert it in km
String distance = String.valueOf(Math.round(results[0] / 1000)) + " km");
要从分/秒转换为度,可以使用Location类的方法

Log.i("MapView", "Map distance to mark (in meters): " + myLocation.distanceTo(GeoToLocation(point)) + "m");
然后


提供商是GPS或网络。。。全球定位系统是精确的,但可能需要一些时间和网络是相反的。。。是的,您必须设置一个提供程序才能获得gps定位。好的,因此我需要在之前定义一个位置管理器-或者我可以将此“LocationManager.gps\u提供程序”直接添加到括号中,如
location loc=new location(LocationManager.gps\u provider)
?是的,您不需要LocationManager。是的,你就是这样做的。如果答案适合你,请将你的问题标记为已解决。
public Location GeoToLocation(GeoPoint gp) {
        Location location = new Location("dummyProvider");
        location.setLatitude(gp.getLatitudeE6()/1E6);
        location.setLongitude(gp.getLongitudeE6()/1E6);
        return location;
    }