Android 谷歌地图标记的地理制图方位和坐标计算

Android 谷歌地图标记的地理制图方位和坐标计算,android,algorithm,google-maps,geolocation,geometry,Android,Algorithm,Google Maps,Geolocation,Geometry,我正在编写一个Android应用程序并集成GoogleMapSv2API。我在地图上锚周围的不同位置有一系列标记 我希望这些标记以增量的方式聚集在锚的位置上 我已经运行了一个循环,它将调用每个标记B,并从B的位置计算到锚a的方位。然后我计算沿着该方位的固定距离的目标坐标并更新 以下是我正在使用的两个功能(摘自stack Post和GeoMapping站点的合并,以供全面披露): public double calcBearing(double lat1, double lon1, double

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

我希望这些标记以增量的方式聚集在锚的位置上

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

以下是我正在使用的两个功能(摘自stack Post和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);
}
公共双计算轴承(双lat1、双lon1、双lat2、双lon2){
双纵1=1;
双纵2=lon2;
双纬度1=数学环面(纬度1);
双纬度2=数学环面(纬度2);
double longDiff=数学环面(longitude2-longitude1);
双y=数学sin(longDiff)*数学cos(latitude2);
double x=Math.cos(latitude1)*Math.sin(latitude2)-Math.sin(latitude1)*Math.cos(latitude2)*Math.cos(longDiff);
双计算轴承=(数学到度数(数学到度数2(y,x))+360)%360;
返回计算轴承;
}
公共坐标CalcCordFromPoint方位(双板条1、双长1、双方位、双距离){
双后角=6371.01;//地球的平均半径,单位为km
double epsilon=0.000001;//浮点相等的阈值
双rLat1=deg2rad(lat1);
双rLon1=deg2rad(lon1);
双轴承=除2rad(轴承);
双R距离=距离/后距离;
double rlat=Math.asin(Math.sin(rLat1)*Math.cos(rdistance)+Math.cos(rLat1)*Math.sin(rdistance)*Math.cos(rbearing));
双rlon;
if(Math.cos(rlat)==0 | Math.abs(Math.cos(rlat))
简言之,我相信我已经把上面的计算搞砸了。我看到的行为是标记不稳定地移动,频率很高,最后指向两个方向:90和270。因此,他们倾向于远离我的锚,而不是向它移动

有人能帮我找出错误吗?我将度传递给方向角函数和坐标计算函数,但我会立即将它们转换为弧度以用于算法,并返回到度以便在其他地方使用

[更新:

大部分代码来自此示例:

在我看来,输出经度正被标准化为-180到180,我正在360度空间上绘制该经度,导致输出指向轴承90和270。关于修复此问题所需的三角数学更改的任何建议?]

可能需要360.0

 double calcBearing =  (Math.toDegrees(Math.atan2(y, x))+360.0)%360.0;
这是一种回答

你还有另一个问题。你没有考虑地图上的任何倾斜。为什么不直接用像素设置动画呢。曲率不会有太大的变形。你要做的是得到标记的像素位置。在添加标记时,您必须保存latlon,或者您必须使用
.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);
            }
        }
    });
以上代码来自此,对于使用上述方法时出现的任何性能问题,我深表歉意。但您仍然可以使用像素位置进行更传统的动画处理

我得到的公式和你在另一个程序中工作时的公式几乎相同,在这个程序中,我根据位置方位和速度为地图设置动画,以移动到预期的位置。最后的公式和你的略有不同。我把它从中提出来,改成了更长的名字

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

谢谢你的评论,尽管我对它的适用性感到困惑。轴承计算没有被整数数学破坏,因此强制它为双精度或长精度没有帮助。我给人的印象是我的绘图是一个动画问题吗?您的大部分回复似乎都是为了解决这个问题,而不是轴承和纬度/经度计算?Ok 360.0只是一个初步猜测。我假设弧度的sin(0)=1和sin(90)=1整数值为0-1。有道理,这可能导致90 180 270 360问题。至于方位,你不需要计算直角三角形的斜边,你在屏幕上有点。这些就是将标记从一个位置动画化到另一个位置所需的全部内容。新浪=b/c,新浪=a/c,a^2+b^2=c^2。这些常用的三角公式和插值器。虽然我承认我有和你类似的代码,我沿着一个方向移动一张地图,我把它添加到答案中也许会有帮助。投票支持回复中的努力。我将努力评估您的上述建议,并将您的计算结果与我的进行比较。如果我发现了问题,我会把它贴在这里并接受你的回答。谢谢。感谢danny117的支持和反馈。悬赏奖。我已经按照以前的计算了。