0

我正在开发一个使用谷歌地图将用户导航到给定位置的应用程序。我想定位地图,使目标点始终位于地图顶部,当前位置标记位于底部,两个标记在地图中垂直对齐并居中(例如,它们之间的垂直线垂直到屏幕)

我的方法是找到标记的中点,然后计算方位以旋转地图,使两个标记最终垂直对齐并居中。将中点居中将使标记居中,但我无法计算轴承的正确值。

任何帮助将不胜感激。

编辑:我已经尝试过Location.bearingTo并且Location.distanceBetween. 对于相同的输入,它们返回不同的值,而返回的值Location.distanceBetween是我正在寻找的。

EDIT2(代码示例)

public static void positionMap(GoogleMap map, LatLng start, LatLng end) {
    // zoom the map so both locations are visible
    LatLngBounds bounds = LatLngBounds.builder().include(start).include(end).build();
    map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 200));

    // find the bearing
    float[] results = new float[3];
    Location.distanceBetween(
        start.latitude,
        start.longitude,
        end.latitude,
        end.longitude,
        results);
    float bearing = results[2];

    // position the map so the two markers are vertically aligned
    CameraPosition position = map.getCameraPosition();
    CameraPosition cameraPosition = new CameraPosition.Builder(position)
        .target(Utils.median(start, end))
        .bearing(bearing)
        .build();
    map.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
4

2 回答 2

2

看一下位置对象的distanceBetween方法。

这是文档。初始方位是您从起点开始需要使用的方位,最终方位是您到达目的地时将使用的方位。我想你会对最初的方位感兴趣。

public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)

Added in API level 1
Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them. Distance and bearing are defined using the WGS84 ellipsoid.

The computed distance is stored in results[0]. If results has length 2 or greater, the initial bearing is stored in results[1]. If results has length 3 or greater, the final bearing is stored in results[2].

Parameters
startLatitude   the starting latitude
startLongitude  the starting longitude
endLatitude the ending latitude
endLongitude    the ending longitude
results an array of floats to hold the results
Throws
IllegalArgumentException    if results is null or has length < 1
于 2014-12-16T21:38:43.223 回答
0

您可以尝试使用Location.bearingTo().

不幸的是,这需要从LatLngto转换Location,但这应该很简单。

于 2014-12-16T21:39:47.063 回答