0
我需要將(谷歌地圖)邊界對象轉換爲區域對象。從邊界計算區域
樣品範圍:
{
northeast: {
lat: 47.5085913,
lng: 19.2656781
},
southwest: {
lat: 47.4179006,
lng: 19.0494985
}
}
樣本區域:
{
// centre of bounds
latitude,
longitude,
// delta of the centre in order to fit the original bounds
latitudeDelta,
longitudeDelta,
}
我到目前爲止已經實現:
const bounds2region = bounds => {
const earthRadiusInKM = 6371 // km
const radiusInKM = 1
const aspectRatio = 1
const [ latitude, longitude ] = bounds2center(bounds)
const radiusInRad = radiusInKM/earthRadiusInKM
const longitudeDelta = rad2deg(radiusInRad/Math.cos(deg2rad(latitude)))
const latitudeDelta = aspectRatio * rad2deg(radiusInRad)
return {
latitude,
longitude,
latitudeDelta,
longitudeDelta,
}
}
const bounds2center = bounds =>
toArrayIfObject(bounds)
.reduce((coordinate1, coordinate2) => {
const c1 = toArrayIfObject(coordinate1)
const c2 = toArrayIfObject(coordinate2)
return [
average([c1[0], c2[0]]),
average([c1[1], c2[1]]),
]
})
const average = numbers => numbers.reduce((sum, n) => sum + n, 0)/numbers.length
const deg2rad = n => n * Math.PI/180
const rad2deg = n => n/Math.PI * 180
const toArrayIfObject = object => typeof object === 'object' ? Object.values(object) : object
它的工作原理部分:它移動地圖中心的邊界,但作物或縮小太多。我不知道如何設置增量(latitudeDelta
,longitudeDelta
)。我想我需要計算aspectRatio
的值,而不是將其設置爲1
。