Skip to content

Instantly share code, notes, and snippets.

@AllThingsSmitty
Last active August 29, 2015 13:56
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 AllThingsSmitty/9311581 to your computer and use it in GitHub Desktop.
Save AllThingsSmitty/9311581 to your computer and use it in GitHub Desktop.
Implementing the Geolocation API with Google Maps API

Once you've called the GoogleMap function, you can set ID, height, and width of the map (mapcanvas.id, mapcanvas.style.height and mapcanvas.style.width). You can set map default functionality like zoom, position, and control under var myOptions:

var myOptions = {
  zoom: 15,
  center: latlng,
  mapTypeControl: false,
  navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
  mapTypeId: google.maps.MapTypeId.ROADMAP
};
// create Google map
function GoogleMap(position) {
'use strict';
var location = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 20,
disableDefaultUI: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
//mapTypeId: google.maps.MapTypeId.SATELLITE
//mapTypeId: google.maps.MapTypeId.HYBRID
//mapTypeId: google.maps.MapTypeId.TERRAIN
});
var marker = new google.maps.Marker({
map: map,
position: location,
animation: google.maps.Animation.DROP,
title: "This is your location"
});
map.setCenter(location);
}
// show error if location can't be found
function showError() {
'use strict';
alert("Location can't be found");
}
//execute geolocation
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(GoogleMap, showError);
} else {
alert("Your browser does not support Geolocation.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment