GoogleMaps标记的GeoMapping方位和坐标计算

时间:2014-03-19 15:00:19

标签: android algorithm google-maps geolocation geometry

我正在编写Android应用并集成GoogleMapsV2 API。我在地图上的锚点附近的各个位置都有一系列标记。

我希望这些标记能够逐渐收敛到锚点的位置。

我有一个循环运行,它将调用每个标记B,并从B的位置计算到锚A的方位。然后我计算沿着该方位的固定距离的目标坐标并更新。

以下是两个功能(取自叠加帖子和GeoMapping网站的合并,用于完整披露)我正在使用:

public double calcBearing(double lat1, double lon1, double lat2, double lon2){
    double longitude1 = lon1;
    double longitude2 = lon2;
    double latitude1 = Math.toRadians(lat1);
    double latitude2 = Math.toRadians(lat2);
    double longDiff= Math.toRadians(longitude2-longitude1);
    double y= Math.sin(longDiff)*Math.cos(latitude2);
    double x=Math.cos(latitude1)*Math.sin(latitude2)-Math.sin(latitude1)*Math.cos(latitude2)*Math.cos(longDiff);

    double calcBearing =  (Math.toDegrees(Math.atan2(y, x))+360)%360;
    return calcBearing;
}

public Coordinate calcCoordFromPointBearing(double lat1, double lon1, double bearing, double distance){
    double rEarth = 6371.01; // Earth's average radius in km
    double epsilon = 0.000001; // threshold for floating-point equality

    double rLat1 = deg2rad(lat1);
    double rLon1 = deg2rad(lon1);
    double rbearing = deg2rad(bearing);
    double rdistance = distance / rEarth;

    double rlat = Math.asin( Math.sin(rLat1) * Math.cos(rdistance) + Math.cos(rLat1) * Math.sin(rdistance) * Math.cos(rbearing) );
    double rlon;
    if (Math.cos(rlat) == 0 || Math.abs(Math.cos(rlat)) < epsilon) // Endpoint a pole
            rlon=rLon1;
    else
        rlon = ( (rLon1 - Math.asin( Math.sin(rbearing)* Math.sin(rdistance) / Math.cos(rlat) ) + Math.PI ) % (2*Math.PI) ) - Math.PI;

    double lat = rad2deg(rlat);
    double lon = rad2deg(rlon);
    return new Coordinate(lat,lon);
}

private double deg2rad(double deg) {
    return (deg * Math.PI / 180.0);
}

private double rad2deg(double rad) {
    return (rad * 180.0 / Math.PI);
}

简而言之,我相信我已经搞砸了上述计算。我看到的行为是不规则地移动的标记,并且高频率朝向两个轴承:90和270.因此,它们倾向于远离我的锚而不是朝向它。

有人能帮助我发现错误吗?我正在向度量函数和坐标计算函数传递度数,但是我将它们立即转换为算法的弧度并返回到其他地方使用的度数。

[UPDATE:

大多数代码来自这个例子:
Calculating coordinates given a bearing and a distance

在我看来,输出经度被标准化为-180到180,我在360度空间上绘图,导致输出前进到轴承90和270.有关trig数学的任何建议修改此问题需要更改吗?]

1 个答案:

答案 0 :(得分:3)

可能需要360.0

 double calcBearing =  (Math.toDegrees(Math.atan2(y, x))+360.0)%360.0;

这有点回答here

你还有另一个问题。你不考虑地图中的任何倾斜。为什么不用像素动画。不会有过多的曲率变形。你需要做的是获得标记的像素位置。添加标记时,您必须保存latlon,或者必须使用.setAnchor添加标记,这样可以提供像素偏移量。如果您有标记放置的latlon,那么您可以得到点。

LatLon ll;
Point p = mMap.getProjection().toScreenLocation(ll);

然后您可以使用这样的代码来设置标记的动画。我通过插入y轴使标记反弹到下面。你必须插入两个轴。

    final Handler handler = new Handler();
    final long start = SystemClock.uptimeMillis();
    final long duration = 2500;

    final Interpolator interpolator = new BounceInterpolator();

    handler.post(new Runnable() {
        @Override
        public void run() {
            long elapsed = SystemClock.uptimeMillis() - start;
            float t = Math.max(
                    1 - interpolator.getInterpolation((float) elapsed
                            / duration), 0);

            marker.setAnchor(0.5f, 1.0f + 6 * t);

            if (t > 0.0) {
                // Post again 16ms later.
                handler.postDelayed(this, 16);
            }
        }
    });

以上代码来自question.我对您在使用上述方法时遇到的任何性能问题表示歉意。但你仍然可以使用像素位置来进行更传统的动画制作。

我在另一个程序中使用了几乎相同的公式,在该程序中,我根据位置方位和速度为地图设置动画以移动到预期位置。公式与您的公式略有不同。我把它从here抬起来,换成了更长的名字。

    // Define the callback method that receives location updates
@Override
public void onLocationChanged(Location location) {

    // Given the bearing, speed, and current location
    // calculate what the expected location is traveling for an
    // interval that is slightly larger than two times fastest interval of
    // the location provider and animate the map movement to the
    // expected location over the same slightly larger interval.

    // In Theory by using an interval that is slightly larger
    // than two times fastest interval of the location provider for the
    // animation length a new animation will start before the
    // currently running animation finishes. This should ensure a
    // smooth animation of the map while traveling under most
    // circumstances.

    // Negative acceleration (braking)
    // should have acceptable map animation because the map
    // animation in theory never finishes.

    // Note longer intervals, large negative accelerations, just
    // braking at the start of an interval may result in the map moving
    // backwards. But it will still be animated.

    // Some handhelds might not be able to keep up

    // TODO CHECK THE age of the location

    // location.getSpeed() =meters/second
    // interval 1/1000 seconds
    // distance in radians km/6371

    // changed.
    // (location.getSpeed()m/s)(1/1000 interval seconds)( 1/1000 km/m)
    // (1/6371 radians/km) = radians/6371000000.0
    double expectedDistance = location.getSpeed() * expectedDistMultiplier;
    // latitude in Radians
    double currentLatitude = Math.toRadians(location.getLatitude());
    // longitude in Radians
    double longitude1 = Math.toRadians(location.getLongitude());
    double bearing;
    bearing = (location.hasBearing()) ? Math.toRadians(location
            .getBearing()) : 0;

    // calculate the expected latitude and longitude based on staring
    // location
    // , bearing, and distance

    double expectedLatitude = Math.asin(Math.sin(currentLatitude)
            * Math.cos(expectedDistance) + Math.cos(currentLatitude)
            * Math.sin(expectedDistance) * Math.cos(bearing));
    double a = Math.atan2(
            Math.sin(bearing) * Math.sin(expectedDistance)
                    * Math.cos(currentLatitude),
            Math.cos(expectedDistance) - Math.sin(currentLatitude)
                    * Math.sin(expectedLatitude));
    double expectedLongitude = longitude1 + a;
    expectedLongitude = (expectedLongitude + 3 * Math.PI) % (2 * Math.PI)
            - Math.PI;

    // convert to degrees for the expected destination
    double expectedLongitudeDestination = Math.toDegrees(expectedLongitude);
    double expectedLatitudeDestination = Math.toDegrees(expectedLatitude);

    // log everything for testing.
    Log.d("Location", "Bearing in radians" + bearing);
    Log.d("Location", "distance in km" + expectedDistance);
    Log.d("Location", "Current Latitude = " + location.getLatitude()
            + " Current Longitude = " + location.getLongitude());
    Log.d("Location", "New Latitude = " + expectedLatitudeDestination
            + " New Longitude = " + expectedLongitudeDestination);

    // build a camera update to animate positioning map to the expected
    // destination
    LatLng ll = new LatLng(expectedLatitudeDestination,
            expectedLongitudeDestination);
    CameraPosition.Builder cb = CameraPosition.builder()
            .zoom(mMap.getCameraPosition().zoom)
            .bearing(mMap.getCameraPosition().bearing)
            .tilt(mMap.getCameraPosition().tilt).target(ll);
    if (location.hasBearing()) {
        cb.bearing(location.getBearing());
    }
    CameraPosition camera = cb.build();
    CameraUpdate update = CameraUpdateFactory.newCameraPosition(camera);
    mMap.animateCamera(update, interval, this);
}