Skip to content

Instantly share code, notes, and snippets.

@nevosegal
Created June 11, 2013 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nevosegal/5757467 to your computer and use it in GitHub Desktop.
Save nevosegal/5757467 to your computer and use it in GitHub Desktop.
Simple google maps implementation including a search field Just add API key and Default address.
html {
height: 100%;
}
body {
height: 100%;
margin: 0;
padding: 0;
}
#map-canvas{
height: 100%;
}
.textBox{
padding:0;
width:15%;
margin:10px auto 0 auto;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="textBox">
<input type="text" id="address" value="">
<input type="button" value="Search" onClick="codeAddress()"></div>
<div id="map-canvas"></div>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=API_KEY&sensor=true"></script>
<script type="text/javascript" src="maps.js"></script>
</body>
</html>
var map, myGeocoder, initialAddress, address;
function initialize() {
myGeocoder = new google.maps.Geocoder();
address = "DEFAULT ADDRESS";
myGeocoder.geocode({'address': address}, function (results, status) {
map.setCenter(results[0].geometry.location);
});
var mapOptions = {
'zoom':14,
'mapTypeId': google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
}
function codeAddress() {
address = document.getElementById('address').value;
myGeocoder.geocode({'address': address}, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
'map': map,
'position': results[0].geometry.location
});
} else{
alert("No results found because: " + status);
}
});
}
initialize();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment