从边界计算区域

问题描述:

我需要将(谷歌地图)边界对象转换为区域对象。从边界计算区域

样品范围:

{ 
    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 

它的工作原理部分:它移动地图中心的边界,但作物或缩小太多。我不知道如何设置增量(latitudeDeltalongitudeDelta)。我想我需要计算aspectRatio的值,而不是将其设置为1

好的,经过一些更冲浪我设法修补的溶液:

const earthRadiusInMetre = 6371e3 

const boundsToRegion = (bounds, aspectRatio = 1, margin = 1) => { 
    const [ latitude, longitude ] = boundsToCenter(bounds) 
    const R = (calculateDistance(bounds[0], { latitude, longitude })) * margin 

    return { 
    latitude, 
    longitude, 
    ...calculateRegionDelta({ latitude }, R, aspectRatio), 
    } 
} 

const calculateRegionDelta = ({ latitude }, radiusInMetre, aspectRatio = 1) => { 
    const radiusInRad = radiusInMetre/earthRadiusInMetre 
    const longitudeDelta = rad2deg(radiusInRad/Math.cos(deg2rad(latitude))) 
    const latitudeDelta = aspectRatio * rad2deg(radiusInRad) 

    return { 
    latitudeDelta, 
    longitudeDelta, 
    } 
} 

const boundsToCenter = bounds => 
    bounds.reduce((coordinate1, coordinate2) => [ 
    average([coordinate1.latitude, coordinate2.latitude]), 
    average([coordinate1.longitude, coordinate2.longitude]), 
    ]) 

const calculateDistance = (coord1, coord2) => { 
    const R = earthRadiusInMetre 
    const lat1 = coord1.latitude 
    const lat2 = coord2.latitude 
    const lon1 = coord1.longitude 
    const lon2 = coord2.longitude 
    const φ1 = deg2rad(lat1) 
    const φ2 = deg2rad(lat2) 
    const Δφ = deg2rad(lat2 - lat1) 
    const Δλ = deg2rad(lon2 - lon1) 

    const a = Math.sin(Δφ/2) * Math.sin(Δφ/2) + Math.cos(φ1) * Math.cos(φ2) * Math.sin(Δλ/2) * Math.sin(Δλ/2) 
    const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)) 

    return R * c 
} 

const deg2rad = n => n * Math.PI/180 

const rad2deg = n => n/Math.PI * 180 

const average = numbers => numbers.reduce((sum, n) => sum + n, 0)/numbers.length 

功能boundsToRegion取的2个坐标bounds,一个aspect ratiomargin参数阵列。

实施例:

const bounds = Object.values(boundsObject).map(coordinate => ({ 
    latitude: coordinate.lat, 
    longitude: coordinate.lng, 
    })) 

const region = boundsToRegion(bounds, aspectRatio, 2) 

资源:set-the-bounds-of-a-mapview