Skip to content

Instantly share code, notes, and snippets.

@marchawkins
Created March 6, 2014 06:01
Show Gist options
  • Save marchawkins/9383363 to your computer and use it in GitHub Desktop.
Save marchawkins/9383363 to your computer and use it in GitHub Desktop.
How to use the HTML 5 geolocation api to geocode and address and plot it on a google map. For this test, input an address and hit the "Map It" button. The location will be shown on the map.
<div class="row">
<div class="col-md-2 col-sm-2 col-xs-2">
<p><button class="btn btn-primary btn-sm" id="get-map-btn"><span>Map It</span></button></p>
</div><!-- .col -->
<div class="col-md-10 col-sm-10 col-xs-10">
<div class="panel panel-default">
<div class="panel-heading">Location Response</div>
<div class="panel-body">
<p>Enter Address: <input id="address" type="text" class="form-control"/></p>
<div id="map-canvas" style="min-height: 400px;"></div>
</div>
</div>
</div><!-- .col -->
</div><!-- .row -->
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
"use strict";
var geocoder;
var map;
// setup initial map
function initialize() {
geocoder = new google.maps.Geocoder(); // create geocoder object
var latlng = new google.maps.LatLng(40.6700, -73.9400); // set default lat/long (new york city)
var mapOptions = { // options for map
zoom: 8,
center: latlng
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); // create new map in the map-canvas div
}
// function to geocode an address and plot it on a map
function codeAddress(address) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location); // center the map on address
var marker = new google.maps.Marker({ // place a marker on the map at the address
map: map,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize); // setup initial map
$(document).ready(function() {
// get map button functionality
$("#get-map-btn").click(function(event){
event.preventDefault();
var address = $("#address").val(); // grab the address from the input field
codeAddress(address); // geocode the address
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment