Skip to content

Instantly share code, notes, and snippets.

@BigGillyStyle
Created February 28, 2018 21:07
Show Gist options
  • Save BigGillyStyle/02a999e881dee853cad6630dda23553d to your computer and use it in GitHub Desktop.
Save BigGillyStyle/02a999e881dee853cad6630dda23553d to your computer and use it in GitHub Desktop.
/**
* https://stackoverflow.com/a/6055653/961045
*
* this.map = instance of NguiMapComponent ( https://github.com/ng2-ui/map)
* this.mapOptions = options to be passed in to map component ( https://developers.google.com/maps/documentation/javascript/reference#MapOptions )
* this.sitesWithLocations = objects with a location property of type { coordinates: [long, lat] }
*/
calculateZoom() {
if (this.map && this.map.el) {
const GLOBE_WIDTH = 256; // a constant in Google's map projection
const longs = this.sitesWithLocations.map(site => site.location.coordinates[0]);
const west = Math.min(...longs);
const east = Math.max(...longs);
let angle = east - west;
if (angle < 0) {
angle += 360;
}
const mapWidth = this.map.el.offsetWidth;
let zoom = Math.round(Math.log(mapWidth * 360 / angle / GLOBE_WIDTH) / Math.LN2) - 1;
if (zoom === Infinity) { // when there are no sites with locations
zoom = SitesMapComponent.DEFAULT_ZOOM; // a static value set to 10 in the JS class this was pulled from
}
this.mapOptions.zoom = zoom;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment