Last active
August 29, 2015 14:23
-
-
Save rlivsey/e51ac5fb0d94cd4cacf2 to your computer and use it in GitHub Desktop.
Google static map component
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
import Ember from 'ember'; | |
import config from 'minutebase/config/environment'; | |
export default Ember.Component.extend({ | |
classNames: "google-static-map", | |
tagName: "img", | |
attributeBindings: ["src"], | |
width: null, | |
height: null, | |
zoom: 14, | |
parts: null, | |
center: null, | |
setupParts: Ember.on("init", function() { | |
this.set("parts", []); | |
}), | |
calculateDimensions: Ember.on("didInsertElement", function() { | |
Ember.run.next(() => { | |
const width = this.$().width(); | |
const height = this.$().height(); | |
this.setProperties({ width, height }); | |
}); | |
}), | |
monitorWindowSize: Ember.on("didInsertElement", function() { | |
this._listener = () => Ember.run.debounce(this, "calculateDimensions", 100); | |
Ember.$(window).on("resize", this._listener); | |
}), | |
stopMonitioringWindowSize: Ember.on("willDestroyElement", function() { | |
if (this._listener) { | |
Ember.$(window).off("resize", this._listener); | |
} | |
}), | |
src: Ember.computed("center", "width", "height", "zoom", "parts.@each.params", { | |
get() { | |
let url = "https://maps.googleapis.com/maps/api/staticmap?"; | |
const {center, zoom, width, height, parts} = this.getProperties("center", "zoom", "width", "height", "parts"); | |
if (!center || !width || !height) { | |
return; | |
} | |
let params = []; | |
params.push(`center=${center.lat},${center.lng}`); | |
params.push(`zoom=${zoom}`); | |
params.push(`size=${width}x${height}`); | |
params.push(`key=${config.googleKey}`); | |
params = params.concat(parts.mapBy("params")); | |
return url + params.join("&"); | |
} | |
}), | |
// could we do this by listening to childViews? | |
register(component) { | |
this.get("parts").pushObject(component); | |
}, | |
unregister(component) { | |
this.get("parts").removeObject(component); | |
} | |
}); |
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
import Ember from 'ember'; | |
export default Ember.Component.extend({ | |
tagName: '', | |
map: null, | |
position: null, | |
title: null, | |
color: 'red', | |
params: Ember.computed("position", "title", "color", { | |
get() { | |
const { position, color } = this.getProperties("position", "color"); | |
const { lat, lng } = position; | |
return `markers=color:${color}|${lat},${lng}`; | |
} | |
}), | |
register: Ember.on("didInsertElement", function() { | |
Ember.run.next(() => { // doesn't fix it | |
this.get("map").register(this); | |
}); | |
}), | |
unregister: Ember.on("willDestroyElement", function() { | |
this.get("map").unregister(this); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment