Last active
August 5, 2017 13:15
-
-
Save dverbovyi/0b575118520067d37e72582b657fd77b to your computer and use it in GitHub Desktop.
Determine google map circle bounds offset, convert meters to coords (lat, lng), calc max zoom level to fit in map bounds
This file contains 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
//explanation https://en.wikipedia.org/wiki/Geographic_coordinate_system | |
const EARTH_RADIUS = 6378137; | |
const center = { | |
lat: YOUR_CENTER_LATIDUTE, | |
lng: YOUR_CENTER_LONGITUDE | |
}; | |
const radianLat = YOUR_RADIUS_IN_METERS / EARTH_RADIUS; | |
const radianLng = radiusInMeters / (EARTH_RADIUS * Math.cos(Math.PI * center.lat / 180)); | |
const offset0 = { | |
lat: center.lat + radianLat * 180/Math.PI, | |
lng: center.lng + radianLng * 180/Math.PI | |
}; | |
const offset1 = { | |
lat: center.lat - radianLat * 180/Math.PI, | |
lng: center.lng - radianLng * 180/Math.PI | |
}; | |
let angle = offset0.lng - offset1.lng; | |
if (angle < 0) | |
angle += 360; | |
const GLOBE_WIDTH = 256; // a constant in Google's map projection | |
let zoom = Math.floor(Math.log((YOUR_MAP_OFFSET_WIDTH) * 360 / angle / GLOBE_WIDTH) / Math.LN2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment