gpt4 book ai didi

android - GoogleMaps 标记的 GeoMapping 方位角和坐标计算

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:33:40 27 4
gpt4 key购买 nike

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

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

我有一个循环运行,它将调用每个标记 B,并从 B 的位置计算到 anchor 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。因此,它们倾向于远离我的 anchor 而不是朝向它。

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

[更新:

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

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

最佳答案

可能需要 360.0

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

这有点回答 here

您还有另一个问题。您没有考虑 map 中的任何倾斜。 为什么不只使用像素进行动画处理。曲率不会有太大的扭曲。您要做的是获取标记的像素位置。添加标记时必须保存 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);
}
}
});

以上代码来自这个question.对于您在使用上述方法时遇到的任何性能问题,我深表歉意。但是您仍然可以将像素位置用于更传统的动画方法。

我得到的公式几乎与您在另一个程序中使用的公式相同,在该程序中,我根据方位角和速度为 map 设置动画以移动到预期位置。最后的公式与您的公式略有不同。我从 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);
}

关于android - GoogleMaps 标记的 GeoMapping 方位角和坐标计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22509861/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com