获取 farLat、farLng 和 nearLat、nearLng 之间的距离
Getting distance between farLat, farLng and nearLat, nearLng
我越来越远 LatLng 如下:
mFarLeft = mMapView.getProjection().getVisibleRegion().farLeft;
mFarRight = mMapView.getProjection().getVisibleRegion().farRight;
mNearLeft = mMapView.getProjection().getVisibleRegion().nearLeft;
mNearRight = mMapView.getProjection().getVisibleRegion().nearRight;
现在,如何计算这个远点和近点之间的距离?
是否有任何方法可以获取以公里和英尺为单位的距离?
字段farLeft
、farRight
等实际上是LatLng
对象,根据Androiddocumentation。但是,有一个字段 latLngBounds
表示包括可见区域的最小边界框。这个边界对象本身有两个 LatLng
字段,用于框的东北角和西南角。使用 Haversine 公式,我们可以计算这个盒子的角之间的距离。例如,如果您想计算边界框的高度,我们可以尝试:
public static double haversine(double lat1, double lon1, double lat2, double lon2) {
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
lat1 = Math.toRadians(lat1);
lat2 = Math.toRadians(lat2);
double a = Math.pow(Math.sin(dLat / 2),2) + Math.pow(Math.sin(dLon / 2),2) * Math.cos(lat1) * Math.cos(lat2);
double c = 2 * Math.asin(Math.sqrt(a));
return 6372.8 * c; // in kilometers
// if you want to return feet, then use 6372.8 * 3280.84 instead
}
public static void main(String[] args) {
LatLng ne = mMapView.getProjection().getVisibleRegion().latLngBounds.northeast;
LatLng sw = mMapView.getProjection().getVisibleRegion().latLngBounds.southwest;
double latLow = sw.latitude;
double latHigh = ne.latitude;
double longitude = sw.longitude;
// now compute the "height" of the bounding box
// note that the longitude value is the same
double height = haversine(latLow, longitude, latHigh, longitude);
}
Google 为地图提供实用程序库,除其他外,
Calculate distances, areas and headings via spherical geometry
Using the spherical geometry utilities in SphericalUtil
, you can compute distances, areas, and headings based on latitudes and longitudes. Here are some of the methods available in the utility:
computeDistanceBetween()
– Returns the distance, in meters, between two latitude/longitude coordinates.
computeHeading()
– Returns the bearing, in degrees, between two latitude/longitude coordinates.
computeArea()
– Returns the area, in square meters, of a closed path on the Earth.
interpolate()
– Returns the latitude/longitude coordinates of a point that lies a given fraction of the distance between two given points. You can use this to animate a marker between two points, for example.
Refer to the reference documentation for a full list of methods in the utility.
该库在 Maven Central 上可用。
dependencies {
compile 'com.google.maps.android:android-maps-utils:0.5+'
}
来源:https://developers.google.com/maps/documentation/android-api/utility/
我越来越远 LatLng 如下:
mFarLeft = mMapView.getProjection().getVisibleRegion().farLeft;
mFarRight = mMapView.getProjection().getVisibleRegion().farRight;
mNearLeft = mMapView.getProjection().getVisibleRegion().nearLeft;
mNearRight = mMapView.getProjection().getVisibleRegion().nearRight;
现在,如何计算这个远点和近点之间的距离?
是否有任何方法可以获取以公里和英尺为单位的距离?
字段farLeft
、farRight
等实际上是LatLng
对象,根据Androiddocumentation。但是,有一个字段 latLngBounds
表示包括可见区域的最小边界框。这个边界对象本身有两个 LatLng
字段,用于框的东北角和西南角。使用 Haversine 公式,我们可以计算这个盒子的角之间的距离。例如,如果您想计算边界框的高度,我们可以尝试:
public static double haversine(double lat1, double lon1, double lat2, double lon2) {
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
lat1 = Math.toRadians(lat1);
lat2 = Math.toRadians(lat2);
double a = Math.pow(Math.sin(dLat / 2),2) + Math.pow(Math.sin(dLon / 2),2) * Math.cos(lat1) * Math.cos(lat2);
double c = 2 * Math.asin(Math.sqrt(a));
return 6372.8 * c; // in kilometers
// if you want to return feet, then use 6372.8 * 3280.84 instead
}
public static void main(String[] args) {
LatLng ne = mMapView.getProjection().getVisibleRegion().latLngBounds.northeast;
LatLng sw = mMapView.getProjection().getVisibleRegion().latLngBounds.southwest;
double latLow = sw.latitude;
double latHigh = ne.latitude;
double longitude = sw.longitude;
// now compute the "height" of the bounding box
// note that the longitude value is the same
double height = haversine(latLow, longitude, latHigh, longitude);
}
Google 为地图提供实用程序库,除其他外,
Calculate distances, areas and headings via spherical geometry
Using the spherical geometry utilities in
SphericalUtil
, you can compute distances, areas, and headings based on latitudes and longitudes. Here are some of the methods available in the utility:
computeDistanceBetween()
– Returns the distance, in meters, between two latitude/longitude coordinates.computeHeading()
– Returns the bearing, in degrees, between two latitude/longitude coordinates.computeArea()
– Returns the area, in square meters, of a closed path on the Earth.interpolate()
– Returns the latitude/longitude coordinates of a point that lies a given fraction of the distance between two given points. You can use this to animate a marker between two points, for example.Refer to the reference documentation for a full list of methods in the utility.
该库在 Maven Central 上可用。
dependencies {
compile 'com.google.maps.android:android-maps-utils:0.5+'
}
来源:https://developers.google.com/maps/documentation/android-api/utility/