01.
<script>
02.
03.
04.
05.
06.
07.
function
initMap() {
08.
var
map =
new
google.maps.Map(document.getElementById(
'map'
), {
09.
center: {lat: -34.397, lng: 150.644},
10.
zoom: 6
11.
});
12.
var
infoWindow =
new
google.maps.InfoWindow({map: map});
13.
14.
15.
if
(navigator.geolocation) {
16.
navigator.geolocation.getCurrentPosition(
function
(position) {
17.
var
pos = {
18.
lat: position.coords.latitude,
19.
lng: position.coords.longitude
20.
};
21.
22.
infoWindow.setPosition(pos);
23.
infoWindow.setContent(
'Location found.'
);
24.
map.setCenter(pos);
25.
},
function
() {
26.
handleLocationError(
true
, infoWindow, map.getCenter());
27.
});
28.
}
else
{
29.
30.
handleLocationError(
false
, infoWindow, map.getCenter());
31.
}
32.
}
33.
34.
function
handleLocationError(browserHasGeolocation, infoWindow, pos) {
35.
infoWindow.setPosition(pos);
36.
infoWindow.setContent(browserHasGeolocation ?
37.
'Error: The Geolocation service failed.'
:
38.
'Error: Your browser doesn\'t support geolocation.'
);
39.
}
40.
</script>