Last active
August 29, 2015 14:21
-
-
Save japboy/a8d35fd1e5ca63fb2c97 to your computer and use it in GitHub Desktop.
A Map class added lacking methods for Google Maps API
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
'use strict'; | |
let w = global, d = w.document, n = w.navigator; | |
let _ = w._, GM = w.google.maps; | |
/** | |
* My Map class | |
* @class | |
* @extends google.maps.Map | |
* @see https://developers.google.com/maps/documentation/javascript/reference#Map | |
*/ | |
export class Map extends GM.Map { | |
constructor (mapDiv, opts={}) { | |
super(mapDiv, opts); | |
this.events_ = []; | |
this.overlayView_ = new GM.OverlayView(); | |
this.overlayView_.setMap(this); | |
this.overlayView_.draw = _.noop; | |
this.overlayView_.onAdd = _.bind(this.emit, this, 'ready'); | |
} | |
addListener (eventName, func) { | |
GM.event.addListener(this, eventName, _.bind(func, this)); | |
this.events_.push({ name: eventName, func: func }); | |
} | |
on (eventName, func) { | |
_.bind(this.addListener, this, eventName, func); | |
} | |
once (eventName, func) { | |
_.bind(this.addListener, this, eventName, () => { | |
func(); | |
this.removeListener(eventName); | |
}); | |
} | |
removeListener (eventName) { | |
_.each(this.events, (ev) => { | |
if (eventName === ev.name) GM.event.removeListener(ev.func); | |
}); | |
this.events_ = _.reject(this.events, (ev) => eventName === ev.name); | |
} | |
removeAllListeners () { | |
this.events_ = []; | |
GM.event.clearInstanceListeners(this); | |
} | |
emit (eventName) { | |
GM.event.trigger(this, eventName); | |
} | |
redraw () { | |
this.emit('resize'); | |
this.setCenter(this.getCenter()); | |
this.setZoom(this.getZoom()); | |
} | |
fromContainerPixelToLatLng (x, y) { | |
let projection = this.overlayView_.getProjection(); | |
let point = new GM.Point(x, y); | |
return projection.fromContainerPixelToLatLng(point); | |
} | |
panToThen (latLng, cb) { | |
let bindedCallback = _.bind(() => { | |
cb(this); | |
w.clearTimeout(timeoutId); | |
GM.event.removeListener(idleEnd); | |
}, this); | |
let idleEnd = GM.event.addListener(this, 'idle', bindedCallback); | |
let timeoutId = w.setTimeout(bindedCallback, 300); | |
this.panTo(latLng); | |
} | |
panZoomTo (max, count) { | |
let _count = count ? count : this.getZoom(); | |
w.setTimeout(() => { this.setZoom(_count); }, 80); | |
if (_count === max) return; | |
let id = GM.event.addListener(this, 'zoom_changed', (ev) => { | |
GM.event.removeListener(id); | |
let level = _count > max ? _count - 1 : _count + 1; | |
this.panZoomTo(max, level); | |
}); | |
} | |
panAndZoomTo (latLng, zoom) { | |
this.panToThen(latLng, _.bind(this.setZoom, this, zoom)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment