Created
March 19, 2014 22:34
-
-
Save adamalex/9652875 to your computer and use it in GitHub Desktop.
Simple Map Location
This file contains 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="viewport" content="initial-scale=1.0, user-scalable=no"> | |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> | |
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script> | |
</head> | |
<body> | |
<div id="content" style="height: 450px"></div> | |
<script type="text/javascript"> | |
var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687); | |
var siberia = new google.maps.LatLng(60, 105); | |
var infowindow = new google.maps.InfoWindow(); | |
var lat, lng; | |
var map = new google.maps.Map(document.getElementById("content"), { | |
mapTypeId: google.maps.MapTypeId.ROADMAP, | |
zoom: 13 | |
}); | |
if (navigator.geolocation) { | |
navigator.geolocation.getCurrentPosition(showPosition, onError); | |
navigator.geolocation.watchPosition(showPosition); | |
} else { | |
onError(); | |
} | |
function showPosition(position) { | |
var newLat = +position.coords.latitude.toFixed(4); | |
var newLng = +position.coords.longitude.toFixed(4); | |
if (lat === newLat && lng === newLng) return; | |
lat = newLat; | |
lng = newLng; | |
updateMap({ | |
location: new google.maps.LatLng(lat, lng), | |
message: lat + " " + lng, | |
zoom: 13 | |
}); | |
} | |
function onError() { | |
var initialLocation; | |
var contentString; | |
if (navigator.geolocation) { | |
initialLocation = newyork; | |
contentString = "Error: The Geolocation service failed."; | |
} else { | |
initialLocation = siberia; | |
contentString = "Error: Your browser doesn't support geolocation. Are you in Siberia?"; | |
} | |
updateMap({ | |
location: initialLocation, | |
message: contentString, | |
zoom: 4 | |
}); | |
} | |
function updateMap(data) { | |
map.setZoom(data.zoom); | |
map.setCenter(data.location); | |
infowindow.setContent(data.message); | |
infowindow.setPosition(data.location); | |
infowindow.open(map); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment