Last active
April 14, 2021 12:06
-
-
Save mikedeng/ff7206aa974b1833644a6e1e32dfd864 to your computer and use it in GitHub Desktop.
find top left and bottom right -- 找到 geo json 的左上边界,与右下边界
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const shenzhen = require('./shenzhen.json'); | |
| // console.log(shenzhen); | |
| const leftTop = [114.13634890625, 22.7185524726563]; | |
| const rightBottom = [114.13634890625, 22.7185524726563]; | |
| let left = leftTop[0]; | |
| let top = leftTop[1]; | |
| let right = rightBottom[0]; | |
| let bottom = rightBottom[1]; | |
| shenzhen.features.forEach(feature => { | |
| const coordinates = feature.geometry.coordinates.flat().flat(); | |
| coordinates.forEach(coordinate => { | |
| const [lon, lat] = coordinate; | |
| // 向左更小 | |
| if (lon < left) { | |
| left = lon; | |
| } | |
| // 向右更大 | |
| if (lon > right) { | |
| right = lon; | |
| } | |
| // 向上更大 | |
| if (lat > top) { | |
| top = lat; | |
| } | |
| // 向下更小 | |
| if (lat < bottom) { | |
| bottom = lat; | |
| } | |
| }); | |
| }); | |
| const centerX = left + (right - left) / 2; | |
| const centerY = bottom + (top - bottom) / 2; | |
| console.log(`leftTop: [${left}, ${top}], rightBottom: [${right}, ${bottom}], centerXY: [${centerX}, ${centerY}]`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment