Skip to content

Instantly share code, notes, and snippets.

@carlobianchi89
Created November 11, 2017 04:57
Show Gist options
  • Save carlobianchi89/bdd01cd4110680eabba7bdb2e725619a to your computer and use it in GitHub Desktop.
Save carlobianchi89/bdd01cd4110680eabba7bdb2e725619a to your computer and use it in GitHub Desktop.
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
function geocodePosition(pos) {
geocoder.geocode({
latLng: pos
}, function(responses) {
if (responses && responses.length > 0) {
updateMarkerAddress(responses[0].formatted_address);
} else {
updateMarkerAddress('Cannot determine address at this location.');
}
});
}
function updateMarkerStatus(str) {
document.getElementById('markerStatus').innerHTML = str;
}
function updateMarkerPosition(latLng) {
document.getElementById('info').innerHTML = [
latLng.lat(),
latLng.lng()
].join(', ');
}
function updateMarkerAddress(str) {
document.getElementById('address').innerHTML = str;
}
var geocoder = new google.maps.Geocoder();
var map;
var goldStar = {
path: 'M 125,5 155,90 245,90 175,145 200,230 125,180 50,230 75,145 5,90 95,90 z',
fillColor: 'yellow',
fillOpacity: 0.8,
scale: 0.1,
strokeColor: 'gold',
strokeWeight: 1
};
function addMarker(lat, lon, city, url) {
var newmarker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lon),
map: map,
title: city
});
newmarker['infowindow'] = new google.maps.InfoWindow({
content: url
});
google.maps.event.addListener(newmarker, 'click', function() {
this['infowindow'].open(map, this);
});
}
function initialize() {
// create as a global variable
map = new google.maps.Map(document.getElementById('mapCanvas'), {
zoom: 8,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
position: latLng,
title: 'Point A',
map: map,
draggable: true
});
// Update current position info.
updateMarkerPosition(latLng);
geocodePosition(latLng);
// Add dragging event listeners.
google.maps.event.addListener(marker, 'dragstart', function() {
updateMarkerAddress('Dragging...');
});
google.maps.event.addListener(marker, 'drag', function() {
updateMarkerStatus('Dragging...');
updateMarkerPosition(marker.getPosition());
});
google.maps.event.addListener(marker, 'dragend', function() {
updateMarkerStatus('Drag ended');
geocodePosition(marker.getPosition());
});
}
// Onload handler to fire off the app.
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<style>
#mapCanvas {
# width: 1000px;
width: 102%;
height: 500px;
float: left;
margin-left: -7px;
margin-right: -10px;
margin-top: -7px;
margin-bottom: 10px;
}
#infoPanel {
float: center;
margin-left: 20px;
}
#infoPanel div {
margin-bottom: 10px;
}
</style>
<font size="3" color="black" face="verdana">
<div id="mapCanvas"></div>
<div id="infoPanel">
<font size="-10" color="white" face="verdana">
<!-- <b>Marker status:</b> -->
<div id="markerStatus"><i>Click and drag the marker.</i></div>
<font size="3" color="black" face="verdana">
<b>Current position:</b>
<div id="info"></div>
<b>Closest matching address:</b>
<div id="address"></div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment