Click here to Skip to main content
15,885,886 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HTML
<!DOCTYPE html>
<html dir="ltr" lang="en-gb">
<head>
<meta charset="utf-8" />
<title>Geolocation API getCurrentPosition example</title>

#map { width:100%; height:800px; }

<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
</head>
<body>
Click on the marker for position information.

<script>
if (navigator.geolocation) {
var timeoutVal = 10 * 1000 * 1000;
navigator.geolocation.getCurrentPosition(
displayPosition,
displayError,
{ enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 }
);
}
else {
alert("Geolocation is not supported by this browser");
}
function displayPosition(position) {
var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var options = {
zoom: 10,
center: pos,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"), options);
var marker = new google.maps.Marker({
position: pos,
map: map,
title: "User location"
});
var contentString = "Timestamp: " + parseTimestamp(position.timestamp) + "User location: lat " + position.coords.latitude + ", long " + position.coords.longitude + ", accuracy " + position.coords.accuracy;
var infowindow = new google.maps.InfoWindow({
content: contentString
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
}
function displayError(error) {
var errors = {
1: 'Permission denied',
2: 'Position unavailable',
3: 'Request timeout'
};
alert("Error: " + errors[error.code]);
}
function parseTimestamp(timestamp) {
var d = new Date(timestamp);
var day = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
var hour = d.getHours();
var mins = d.getMinutes();
var secs = d.getSeconds();
var msec = d.getMilliseconds();
return day + "." + month + "." + year + " " + hour + ":" + mins + ":" + secs + "," + msec;
}
</script>
</body>
</html>



after i run code it get error 1
Quote:
Permission denied
Posted
Updated 7-Oct-14 16:05pm
v2
Comments
[no name] 8-Oct-14 0:18am    
Is that working in all the other browsers?

1 solution

There are some steps you must follow,

1. You must accept the geolocation permission
2.try clearing browser data and restarting Chrome
3.Set permission for geo location read here https://support.google.com/chrome/answer/142065?hl=en[^]

4.You can read some here http://stackoverflow.com/questions/13370203/allow-to-use-geolocation-for-a-website-for-which-i-previously-refused-it[^]

5. If you have python installed rad here http://stackoverflow.com/questions/5423938/html-5-geo-location-prompt-in-chrome[^]
 
Share this answer
 
Comments
anssary2010 8-Oct-14 17:08pm    
i am not block any thing and google map site run correctly plz tell me if code run in your computer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900