Created
January 7, 2014 09:31
-
-
Save dkdndes/8296940 to your computer and use it in GitHub Desktop.
Example: GoogleMaps Geolocation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
html, body, #map-canvas { | |
height: 100%; | |
margin: 0px; | |
padding: 0px | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="Geolocation with GoogleMaps example" /> | |
<meta charset=utf-8 /> | |
<title>Geolocation</title> | |
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"> | |
<meta charset="utf-8"> | |
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"> | |
</script> | |
</head> | |
<body> | |
<p>Geolocation with GoogleMaps</p> | |
<div id="map-canvas"></div> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Note: This example requires that you consent to location sharing when | |
// prompted by your browser. If you see a blank space instead of the map, this | |
// is probably because you have denied permission for location sharing. | |
var map; | |
function initialize() { | |
var mapOptions = { | |
zoom: 6 | |
}; | |
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); | |
// Try HTML5 geolocation | |
if(navigator.geolocation) { | |
navigator.geolocation.getCurrentPosition(function(position) { | |
var pos = new google.maps.LatLng(position.coords.latitude, | |
position.coords.longitude); | |
var infowindow = new google.maps.InfoWindow({ | |
map: map, | |
position: pos, | |
content: 'Location found using HTML5.' | |
}); | |
map.setCenter(pos); | |
}, function() { | |
handleNoGeolocation(true); | |
}); | |
} else { | |
// Browser doesn't support Geolocation | |
handleNoGeolocation(false); | |
} | |
} | |
function handleNoGeolocation(errorFlag) { | |
if (errorFlag) { | |
var content = 'Error: The Geolocation service failed.'; | |
} else { | |
var content = 'Error: Your browser doesn\'t support geolocation.'; | |
} | |
var options = { | |
map: map, | |
position: new google.maps.LatLng(60, 105), | |
content: content | |
}; | |
var infowindow = new google.maps.InfoWindow(options); | |
map.setCenter(options.position); | |
} | |
google.maps.event.addDomListener(window, 'load', initialize); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment