Skip to content

Instantly share code, notes, and snippets.

@nulltask
Last active February 25, 2016 17:30
Show Gist options
  • Save nulltask/a95a2a5901b27ab04900 to your computer and use it in GitHub Desktop.
Save nulltask/a95a2a5901b27ab04900 to your computer and use it in GitHub Desktop.
Draggable landmark editor for Google Maps
var canvas = document.getElementById('map');
var center = new google.maps.LatLng(35.792621, 139.806513);
var options = {
zoom: 5,
center: center
};
var map = new google.maps.Map(canvas, options);
var xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.addEventListener('load', function(e) {
var obj = e.currentTarget.response;
console.log(obj);
var markers = obj.map(function(data, index) {
var info = new google.maps.InfoWindow({
content: data.title
});
var marker = new google.maps.Marker({
map: map,
draggable: true,
animation: google.maps.Animation.DROP,
position: new google.maps.LatLng(data.latitude, data.longtitude)
});
google.maps.event.addListener(marker, 'mouseover', function(e) {
info.open(map, marker);
});
google.maps.event.addListener(marker, 'mouseout', function(e) {
info.close();
});
return marker;
});
window.addEventListener('keyup', function(e) {
if (e.code != 'Space') return false;
var out = markers.map(function(marker, index) {
return Object.assign({}, obj[index], {
latitude: marker.position.lat(),
longtitude: marker.position.lng()
});
});
var anchor = document.createElement('a');
anchor.download = 'data.json';
anchor.href = 'data:application/json,' + encodeURIComponent(JSON.stringify(out, ' ', 2));
var event = new MouseEvent('click', {
'view': window,
'bubbles': true,
'cancelable': true
});
anchor.dispatchEvent(event);
});
});
xhr.open('GET', 'data.json');
xhr.send(null);
[
{
"title": "test 1",
"latitude": 35.681382,
"longtitude": 139.766084
},
{
"title": "test 2",
"latitude": 34.686316,
"longtitude": 135.519711
}
]
<!DOCTYPE html>
<html>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_APP_KEY"></script>
<style>
html {
position: relative;
height: 100%;
}
body {
position: relative;
margin: 0;
width: 100%;
height: 100%;
}
#map {
height: 100%;
width: 100%;
height: 100%;
}
.help {
position: fixed;
pointer-events: none;
font-family: sans-serif;
bottom: 1em;
left: 1em;
}
</style>
<body>
<div id="map"></div>
<div class="help">Press space bar to save json</div>
<script src="app.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment