在Android中偏移LatLng一定量的米 [英] Offset LatLng by some amount of meters in Android

查看:142
本文介绍了在Android中偏移LatLng一定量的米的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 LatLng 对象,我想把它的坐标移到东边500米处。我找不到内置的方法。我见过 https://gis.stackexchange.com/a/2964 但我的结果太不准确了(关于15%)实际使用。如何以米为单位进行精确移位?

I have a LatLng object and I want to shift it's coordinates 500 meters to the east. I couldn't find a built-in method for that. I've seen https://gis.stackexchange.com/a/2964 but my results are just too inaccurate (about 15%) to use practically. How can I make a precise shift in meters?

注意:我正在寻找移动谷歌地图相机,我知道怎么做那个。

Note: I am NOT looking for shifting a Google Maps camera, I know how to do that.

我试过了:

static final double KILOMETERS_PER_DEGREE = 111.111;

static LatLng offsetLongitude(LatLng initialCoords, float horizontalOffsetInMeters){
    double degreeOffset = 1.0 / KILOMETERS_PER_DEGREE * horizontalOffsetInMeters / 1000.0;
    double longitudeOffset = Math.cos(initialCoords.latitude * Math.PI / 180.0) * degreeOffset;
    return new LatLng(initialCoords.latitude, initialCoords.longitude + longitudeOffset);
}

public static LatLngBounds boundsForSpanWidth(LatLng midpoint, float targetSpanWidth){
    LatLng east = offsetLongitude(midpoint, -targetSpanWidth);
    LatLng west = offsetLongitude(midpoint, targetSpanWidth);
    LatLngBounds newBounds = new LatLngBounds(west, east);
    return newBounds;
}

然而,当我用一个点(不靠近两极或任何东西)来称呼它时)目标跨度为5000米,我得到的两个点相距 6170米。为什么?

However, when I call it with a point (not close to poles or anything) with a target span of 5000 meters, I'm getting two points that are about 6170 meters apart. Why?

推荐答案

您可以使用Google地图中的 computeOffset 方法Android API实用程序库( https://developers.google.com/maps/documentation / android-api / utility / ):

You can use the computeOffset method from the Google Maps Android API Utility Library (https://developers.google.com/maps/documentation/android-api/utility/):


public static LatLng computeOffset(LatLng from,double distance,double标题)

返回从指定标题中的原点移动距离得到的LatLng(以北方向顺时针方向表示)。

Returns the LatLng resulting from moving a distance from an origin in the specified heading (expressed in degrees clockwise from north).

参数:


  • from - 从中​​开始的LatLng。

  • 距离 - 行程距离。

  • heading - 从北方顺时针方向的航向。

在你的情况下(距离参数以米为单位):

In your case (the distance parameter is measured in meters):

LatLng east = SphericalUtil.computeOffset(midpoint, 500, 90); // Shift 500 meters to the east
LatLng west = SphericalUtil.computeOffset(midpoint, 500, 270); // Shift 500 meters to the west

这篇关于在Android中偏移LatLng一定量的米的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆