【问题标题】:GeoMapping Bearing and Coordinate Calculation for GoogleMaps markersGoogleMaps 标记的 GeoMapping 方位角和坐标计算
【发布时间】:2014-03-19 15:00:19
【问题描述】:

我正在编写一个 Android 应用程序并集成 GoogleMapsV2 API。我在地图上锚点周围的不同位置有一系列标记。

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

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

这是我正在使用的两个函数(取自堆栈帖子和地理地图站点的合并,以供全面披露):

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。因此,它们倾向于远离我的锚点而不是朝向它。

有人可以帮我找出错误吗?我将度数传递给方位函数和坐标计算函数,但我会立即将它们转换为弧度以供算法使用,然后转换回度数以供其他地方使用。

[更新:

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

在我看来,输出经度正被归一化为 -180 到 180,我在 360 度空间上绘制,导致输出指向方位 90 和 270。关于三角数学更改的任何建议都需要解决这个问题?]

【问题讨论】:

标签: android algorithm google-maps geolocation geometry


【解决方案1】:

可能需要 360.0

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

有人回答了here

你还有另一个问题。您没有考虑地图中的任何倾斜。 为什么不只使用像素进行动画处理。曲率不会有太大的扭曲。您要做的是获取标记的像素位置。添加标记时您必须保存纬度,或者您必须使用.setAnchor 添加标记,这会为您提供像素偏移量。如果你有标记放置的纬度,那么你就知道了。

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);
}

【讨论】:

  • 感谢您的评论,尽管我对它的适用性感到困惑。方位计算不会被整数数学破坏,因此将其强制为 double 或 long 并没有帮助。我给人的印象是我的绘图是一个动画问题吗?您的大部分回复似乎都是针对该问题而不是方位角和纬度/经度计算?
  • 好吧 360.0 只是初步猜测。我假设弧度的 sin(0) = 1 和 sin(90) = 1 整数值为 0-1。有道理,这可能导致 90 180 270 360 问题。至于方位,您不需要它计算直角三角形的斜边,您可以在屏幕上找到点。这些就是将标记从一个位置移动到另一个位置所需的全部内容。 SinA = b/c,SinB = a/c,a^2 + b^2 = c^2。那么那些常见的三角公式和插值器。虽然我承认我有与你类似的代码,我沿着方位移动地图,但我将它添加到答案中,也许它会有所帮助。
  • 对回复中的努力表示支持。我将评估您的上述建议,并将您的计算与我的进行比较。如果我发现问题,我会在这里发布并接受您的回答。谢谢。
  • 感谢 danny117 的支持和反馈。赏金。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-04
  • 1970-01-01
  • 2010-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-30
相关资源
最近更新 更多