Below is the basic code to demonstrate plotting location on google maps:
<style>
#map {
height: 500px;
width: 100%;
}
</style>
<!--API Reference start-->
<!--Note that this is Geolocation API Reference without key-->
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<!--API Reference end-->
<!--Interface start-->
<button onclick="getLocation()">Get My Location on Google Maps</button>
<div id="map">This is where the map will be displayed.</div>
<!--Interface end-->
<script>
var lat, lng;
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, fail, options);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
lat = position.coords.latitude;
lng = position.coords.longitude;
var mylocation = {lat: lat, lng: lng};
var map = new google.maps.Map(document.getElementById("map"), {
zoom: 12,
center: mylocation
});
var marker = new google.maps.Marker({
position: mylocation,
map: map
});
}
function fail(error) {
var errorType = {
0:"Unknown Error",
1:"Permission denied by the user",
2:"Position of the user not available",
3:"Request timed out"
};
var errMsg = errorType[error.code];
if(error.code == 0 || error.code == 2) {
errMsg = errMsg + " - " + error.message;
}
alert(errMsg);
}
var options = {
enableHighAccuracy: true,
timeout: Infinity,
maximumAge: 0
};
</script>
<style>
#map {
height: 500px;
width: 100%;
}
</style>
<!--API Reference start-->
<!--Note that this is Geolocation API Reference without key-->
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<!--API Reference end-->
<!--Interface start-->
<button onclick="getLocation()">Get My Location on Google Maps</button>
<div id="map">This is where the map will be displayed.</div>
<!--Interface end-->
<script>
var lat, lng;
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, fail, options);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
lat = position.coords.latitude;
lng = position.coords.longitude;
var mylocation = {lat: lat, lng: lng};
var map = new google.maps.Map(document.getElementById("map"), {
zoom: 12,
center: mylocation
});
var marker = new google.maps.Marker({
position: mylocation,
map: map
});
}
function fail(error) {
var errorType = {
0:"Unknown Error",
1:"Permission denied by the user",
2:"Position of the user not available",
3:"Request timed out"
};
var errMsg = errorType[error.code];
if(error.code == 0 || error.code == 2) {
errMsg = errMsg + " - " + error.message;
}
alert(errMsg);
}
var options = {
enableHighAccuracy: true,
timeout: Infinity,
maximumAge: 0
};
</script>