Javascript 如何计算两台lat/lng之间的距离

Javascript 如何计算两台lat/lng之间的距离,javascript,react-native,lodash,Javascript,React Native,Lodash,我正在使用react原生gmaps模块进行跟踪应用。这是一个光包,暴露了一些方法 <RNGMap ref={'gmap'} style={{ width: this.state.mapWidth, height: this.state.mapHeight }} markers={this.state.markers} zoomLevel={this.state.zoom} onMapChange={(e) => this._handleMapChange(e) } center={t

我正在使用react原生gmaps模块进行跟踪应用。这是一个光包,暴露了一些方法

<RNGMap
ref={'gmap'}
style={{ width: this.state.mapWidth, height: this.state.mapHeight }}
markers={this.state.markers}
zoomLevel={this.state.zoom}
onMapChange={(e) => this._handleMapChange(e) }
center={this.state.center}
onMarkerClick={(e) => this._handleMarkerPress(e) }/>
问题是每次贴图移动时都会触发此方法。因此,我需要设置最小lat/lng距离更改,以防止每次将多段线点添加到地图时触发我的函数。我怎么计算这个?对于这一点,类似的问题似乎过于复杂了

所以我想应该是这样的。但我不知道该如何比较或比较什么

    var oldLat;
var oldLng;
_handleMapChange(e) {

    if (compare old with new  )
    if (this.state.isFollowing) {
        this.setState({ followingIconBackground: colors.rgba.mapBlue })
        TimerMixin.setTimeout.call(this, () => {
            this.setState({ followingIconBackground: colors.rgba.mapGrey })
        }, 100)
    }
    oldLat = e.lat
    oldLng = e.lng
}

对于高级地理空间演算,我使用
(可用于服务器端和前端)

资料来源:


您需要将点创建为GeoJson(如上所述)

对于高级地理空间演算,我使用
(可用于服务器端和前端)

资料来源:


您需要将点创建为GeoJson(如上所述)

您可能希望计算欧几里德距离。这是sqrt(Δx^2+Δy^2)。有点让我不知所措。你能演示一下如何使用它吗?那将是
d=sqrt((x2-x1)^2+(y2-y1)^2)
实际上这是最简单的事情之一。我在这里使用的Δ符号实际上意味着变化——新值和旧值之间的差值,有任何符号。你想计算两个点之间的距离,给出它们的lat/long吗?如果是这样,请查看:您可能希望计算欧几里德距离。这是sqrt(Δx^2+Δy^2)。有点让我不知所措。你能演示一下如何使用它吗?那将是
d=sqrt((x2-x1)^2+(y2-y1)^2)
实际上这是最简单的事情之一。我在这里使用的Δ符号实际上意味着变化——新值和旧值之间的差值,有任何符号。你想计算两个点之间的距离,给出它们的lat/long吗?如果是,请查看:
    var oldLat;
var oldLng;
_handleMapChange(e) {

    if (compare old with new  )
    if (this.state.isFollowing) {
        this.setState({ followingIconBackground: colors.rgba.mapBlue })
        TimerMixin.setTimeout.call(this, () => {
            this.setState({ followingIconBackground: colors.rgba.mapGrey })
        }, 100)
    }
    oldLat = e.lat
    oldLng = e.lng
}
var point1 = {
  "type": "Feature",
  "properties": {},
  "geometry": {
    "type": "Point",
    "coordinates": [-75.343, 39.984]
  }
};
var point2 = {
  "type": "Feature",
  "properties": {},
  "geometry": {
    "type": "Point",
    "coordinates": [-75.534, 39.123]
  }
};
var units = "miles";

var points = {
  "type": "FeatureCollection",
  "features": [point1, point2]
};

//=points

var distance = turf.distance(point1, point2, units);

//=distance