Skip to content

Instantly share code, notes, and snippets.

@mikedeng
Last active April 14, 2021 12:06
Show Gist options
  • Select an option

  • Save mikedeng/ff7206aa974b1833644a6e1e32dfd864 to your computer and use it in GitHub Desktop.

Select an option

Save mikedeng/ff7206aa974b1833644a6e1e32dfd864 to your computer and use it in GitHub Desktop.
find top left and bottom right -- 找到 geo json 的左上边界,与右下边界
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}]`);
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment