3

我想要一个类似雷达屏幕的东西,它以用户所在的位置为中心,同时缩放到足以包含 v2 api 的目标点。现在我正在使用

bounds = new LatLngBounds.Builder().include(destPos)
                .include(yourPos).build();
        map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50));

但这以两点之间的某个点为中心,并缩放以包括两者。有没有像这样简单的方法来做我想做的事?或者我是否必须或多或少从头开始并做一些数学运算(例如计算两点之间的距离,计算 LatLngBounds 的纬度/经度,以便用户位于定义的矩形的中心并且矩形的边缘包括目的地——考虑到地图/屏幕尺寸)?

这是我所拥有的:

在此处输入图像描述

这就是我想要的:

在此处输入图像描述

4

3 回答 3

5

您自己的答案过于复杂。

您需要计算位于您位置另一侧的点并将其包含在LatLngBounds.

LatLng otherSidePos = new LatLng(2 * yourPos.latitude - destPos.latitude, 2 * yourPos.longitude - destPos.longitude);
bounds = new LatLngBounds.Builder().include(destPos)
                .include(otherSidePos).build();
        map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding));

注意:您不想将 50 像素硬编码为填充。它在不同的设备上看起来会有所不同。请改用与密度无关的像素。

于 2013-05-05T12:21:02.040 回答
1

我想出了上面给出的艰难方法。我为 LatLngBounds 设置了一个正方形区域,以简化获取方位编号(只要它适合屏幕,我就可以:您可以计算出从非正方形矩形的中心到角落的方位角再加上一些触发)。我还添加了一些填充,因为点之间的距离将与从中心到东北角的对角线相同,这将大于从中心到顶部边缘的距离,这意味着第二个点可能在顶部边缘之上并且不可见(如果它是,例如,正北)。我从http://www.movable-type.co.uk/scripts/latlong.html大量借用并从轴承和距离计算纬度和经度

这是我获取 LatLngBounds 的方法:

public LatLngBounds getBounds(double lat1, double lng1, double lat2,
        double lng2) {

    // defines a square area with point lat1,lng1 at center and includes
    // point
    // lat2, long2 with a buffer between edge and second point

    // get distance between two points
    float[] results = new float[1];

    Location.distanceBetween(lat1, lng1, lat2, lng2, results);
    double d = results[0];

    d = (d * 1.5); // add padding. The shortest distance to an edge of the
//square box is d * cos(45 degrees). Thus it's possible that the second point will be out of the box. To compensate I make the radius larger (d * 1/cos(45) = d * 1.41). I put a bit extra so that the second point isn't right on the edge of the screen. 

    long R = 6371000; // distance of earth's radius in meters

    d = d /(double) R;

    lat1 = Math.toRadians(lat1); // Current lat point converted to radians
    lng1 = Math.toRadians(lng1); // Current long point converted to radians

    // calculate northeast corner of LatLngBounds

    double brng = Math.toRadians(45); // bearing from center to northeast in
                                        // radians

    double resultLat1 = Math.asin(Math.sin(lat1) * Math.cos(d)
            + Math.cos(lat1) * Math.sin(d) * Math.cos(brng));

    double resultLng1 = lng1
            + Math.atan2(Math.sin(brng) * Math.sin(d) * Math.cos(lat1),
                    Math.cos(d) - Math.sin(lat1) * Math.sin(resultLat1));

    resultLat1 = Math.toDegrees(resultLat1);
    resultLng1 = Math.toDegrees(resultLng1);

    Log.i("My Code", "resultLat1: " + resultLat1 + " resultLng1: " + resultLng1);

    LatLng northEast = new LatLng(resultLat1, resultLng1);

    // calculate southwest corner of LatLngBounds. Everything is the same
    // except the bearing value

    brng = Math.toRadians(225); // bearing from center to southwest corner
                                // in radians
    double resultLat2 = Math.asin(Math.sin(lat1) * Math.cos(d)
            + Math.cos(lat1) * Math.sin(d) * Math.cos(brng));

    double resultLng2 = lng1
            + Math.atan2(Math.sin(brng) * Math.sin(d) * Math.cos(lat1),
                    Math.cos(d) - Math.sin(lat1) * Math.sin(resultLat2));

    resultLat2 = Math.toDegrees(resultLat2);
    resultLng2 = Math.toDegrees(resultLng2);

    Log.i("My Code", "resultLat2: " + resultLat2 + " resultLng2: " + resultLng2);

    LatLng southWest = new LatLng(resultLat2, resultLng2);

    LatLngBounds bounds = new LatLngBounds(southWest, northEast);

    return bounds;
}
于 2013-05-04T20:01:22.357 回答
1

用于缩放以包括所有标记而不更改相机目标(地图中心)。

LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker marker : markers) {
    builder.include(marker.getPosition());
}
LatLngBounds bounds = builder.build();

LatLng currentLoc = _googleMapView.getCameraPosition().target;

//make sure that centre location doesn't change
double deltaLat = Math.max(Math.abs(bounds.southwest.latitude - currentLoc.latitude),
                Math.abs(bounds.northeast.latitude - currentLoc.latitude));

double deltaLon = Math.max(Math.abs(bounds.southwest.longitude - currentLoc.longitude),
                Math.abs(bounds.northeast.longitude - currentLoc.longitude));

LatLngBounds.Builder displayBuilder = new LatLngBounds.Builder();
displayBuilder.include(currentLoc); //not necessary but hey
displayBuilder.include(new LatLng(currentLoc.latitude + deltaLat, currentLoc.longitude + deltaLon));
displayBuilder.include(new LatLng(currentLoc.latitude - deltaLat, currentLoc.longitude - deltaLon));

_googleMapView.moveCamera(CameraUpdateFactory.newLatLngBounds(displayBuilder.build(), 0));
于 2014-12-09T07:23:56.377 回答