<head>
<meta name="description" content="Updating GoogleMapMarker Content">
<meta charset="utf-8">
<base href="http://polygit.org/components/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>
<link href="google-map/google-map.html" rel="import">
<style>
body {
font-family: sans-serif;
}
</style>
</head>
<body>
<x-example></x-example>
<dom-module id="x-example">
<style>
:host {
display: block;
}
google-map {
width: 300px;
height: 300px;
}
</style>
<template>
<site-data sites="{{sites}}"> </site-data>
<google-map fit-to-markers >
<template is="dom-repeat" items="{{sites}}" as="site">
<template is="dom-repeat" items="{{site}}">
<google-map-marker id="{{item.project_name}}" latitude="{{item.latitude}}"
longitude="{{item.longitude}}"
title="{{item.project_name}}">
<h1>{{item.project_name}}</h1>
<p style="margin: 0;">Location: <b>{{item.town}}, {{item.country}}</b></p>
<p style="margin: 0;" foo$="{{item.tech_desc}}">Tech Description: <b>{{item.tech_desc}}</b></p>
</google-map-marker>
</template>
</template>
</google-map>
<hr>
<button on-tap="moar">Moar Election</button>
<button on-tap="change">Change Tech</button>
<button on-tap="reattach">Re-attach</button>
</template>
<script>
// only need this when in the main document and on non-Chrome
addEventListener('WebComponentsReady', function() {
var marker = document.createElement('google-map-marker').__proto__;
marker._contentChanged = function() {
if (this._contentObserver)
this._contentObserver.disconnect();
// Watch for future updates.
this._contentObserver = new MutationObserver( this._contentChanged.bind(this));
this._contentObserver.observe( this, {
childList: true,
subtree: true,
///----------------
// monkey patch
characterData: true
//-----------------
});
var content = this.innerHTML.trim();
if (content) {
if (!this.info) {
// Create a new infowindow
this.info = new google.maps.InfoWindow();
this.openInfoHandler_ = google.maps.event.addListener(this.marker, 'click', function() {
this.open = true;
}.bind(this));
this.closeInfoHandler_ = google.maps.event.addListener(this.info, 'closeclick', function() {
this.open = false;
}.bind(this));
}
this.info.setContent(content);
} else {
if (this.info) {
// Destroy the existing infowindow. It doesn't make sense to have an empty one.
google.maps.event.removeListener(this.openInfoHandler_);
google.maps.event.removeListener(this.closeInfoHandler_);
this.info = null;
}
}
};
marker.detached = function() {
if (this.marker) {
google.maps.event.clearInstanceListeners(this.marker);
this._listeners = {};
this.marker.setMap(null);
///----------------
// monkey patch
this.info = null;
//-----------------
}
if (this._contentObserver)
this._contentObserver.disconnect();
};
marker.attached = function() {
// If element is added back to DOM, put it back on the map.
if (this.marker) {
this.marker.setMap(this.map);
///----------------
// monkey patch
this._contentChanged();
//-----------------
}
};
Polymer({
is: 'x-example',
properties: {
sites: {
value: function() {
return [[
{project_name: "IA", latitude: 41.9349079, longitude:-94.5121359, town: 'Up', country: 'US', tech_desc: 44},
{project_name: "NH", latitude: 43.9977267, longitude:-72.7009582, town: 'Down', country: 'Pastoral'}
]];
}
}
},
moar: function() {
this.push('sites', [{project_name: "SC", latitude: 33.6222803, longitude:-82.0681717}]);
},
change: function() {
this.set('sites.0.0.tech_desc', String(Math.floor((Math.random()+1)*2000)));
},
reattach: function() {
var node = this.$$('#IA');
var parent = node.parentElement;
node.remove();
parent.appendChild(node);
}
});
});
</script>
</dom-module>
</body>
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |