map-geolocation.html
author sl
Wed, 09 Apr 2014 13:41:29 +0200
changeset 1 a1483545da35
permissions -rw-r--r--
Rectangle overlay params:
Fixing duplicate first rectangle entry due to our guery strings parsing.
Adding 'a' URL parameter to control rectangle fill opacity.
Adding support to pass in color as hexa using %23 URL encoding.
     1 
     2 
     3 <!DOCTYPE html>
     4 <html>
     5   <head>
     6     <title>Geolocation</title>
     7     <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
     8     <meta charset="utf-8">
     9     <link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet">
    10     <!--
    11     Include the maps javascript with sensor=true because this code is using a
    12     sensor (a GPS locator) to determine the user's location.
    13     See: https://developers.google.com/apis/maps/documentation/javascript/basics#SpecifyingSensor
    14     -->
    15     <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script>
    16 
    17     <script>
    18 var map;
    19 
    20 function initialize() {
    21   var mapOptions = {
    22     zoom: 6,
    23     mapTypeId: google.maps.MapTypeId.ROADMAP
    24   };
    25   map = new google.maps.Map(document.getElementById('map-canvas'),
    26       mapOptions);
    27 
    28   // Try HTML5 geolocation
    29   if(navigator.geolocation) {
    30     navigator.geolocation.getCurrentPosition(function(position) {
    31       var pos = new google.maps.LatLng(position.coords.latitude,
    32                                        position.coords.longitude);
    33 
    34       var infowindow = new google.maps.InfoWindow({
    35         map: map,
    36         position: pos,
    37         content: 'Location found using HTML5.'
    38       });
    39 
    40       map.setCenter(pos);
    41     }, function() {
    42       handleNoGeolocation(true);
    43     });
    44   } else {
    45     // Browser doesn't support Geolocation
    46     handleNoGeolocation(false);
    47   }
    48 }
    49 
    50 function handleNoGeolocation(errorFlag) {
    51   if (errorFlag) {
    52     var content = 'Error: The Geolocation service failed.';
    53   } else {
    54     var content = 'Error: Your browser doesn\'t support geolocation.';
    55   }
    56 
    57   var options = {
    58     map: map,
    59     position: new google.maps.LatLng(60, 105),
    60     content: content
    61   };
    62 
    63   var infowindow = new google.maps.InfoWindow(options);
    64   map.setCenter(options.position);
    65 }
    66 
    67 google.maps.event.addDomListener(window, 'load', initialize);
    68 
    69     </script>
    70   </head>
    71   <body>
    72     <div id="map-canvas"></div>
    73   </body>
    74 </html>
    75