Harshit Laddha
Harshit Laddha

Reputation: 2124

Google map api usage for geolocation I dont know whats wrong?

I have copied exact code from google's tutorial and added my api key So there can't be any mistake from code point of view. However my browser gives no geolocation allowed so there must be some mistake there. Can anyone tell me what am i missing. Here are some screen shots i thing that might be useful for understanding my problem.

My location setting allowed by default to this page

My location setting allowed by default to this page

My Google Project showing that all google maps and geolocation services are On

My Google Project showing that all google maps and geolocation services are On

Error dialog giving me when i load the page

Error dialog giving me when i load the page

On clicking that manage settings also i checked that i turned ON location for all

On clicking that manage settings also i checked that i turned ON location for all

I really dont know whats wrong ? here's the code if of any use - from

https://developers.google.com/maps/documentation/javascript/examples/map-geolocation

<!DOCTYPE html>
<html>
  <head>
    <title>Geolocation</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <!--
    Include the maps javascript with sensor=true because this code is using a
    sensor (a GPS locator) to determine the user's location.
    See: https://developers.google.com/maps/documentation/javascript/tutorial#Loading_the_Maps_API
    -->
    <script src="https://maps.googleapis.com/maps/api/js?key={My_API_KEY}&v=3.exp&sensor=true"></script>

    <script>
// Note: This example requires that you consent to location sharing when
// prompted by your browser. If you see a blank space instead of the map, this
// is probably because you have denied permission for location sharing.

var map;

function initialize() {
  var mapOptions = {
    zoom: 6
  };
  map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);

  // Try HTML5 geolocation
  if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var pos = new google.maps.LatLng(position.coords.latitude,
                                       position.coords.longitude);

      var infowindow = new google.maps.InfoWindow({
        map: map,
        position: pos,
        content: 'Location found using HTML5.'
      });

      map.setCenter(pos);
    }, function() {
      handleNoGeolocation(true);
    });
  } else {
    // Browser doesn't support Geolocation
    handleNoGeolocation(false);
  }
}

function handleNoGeolocation(errorFlag) {
  if (errorFlag) {
    var content = 'Error: The Geolocation service failed.';
  } else {
    var content = 'Error: Your browser doesn\'t support geolocation.';
  }

  var options = {
    map: map,
    position: new google.maps.LatLng(60, 105),
    content: content
  };

  var infowindow = new google.maps.InfoWindow(options);
  map.setCenter(options.position);
}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

Please note that It is working in Mozilla Firefox Correctly, but not in chrome

Upvotes: 1

Views: 383

Answers (1)

Leo
Leo

Reputation: 14830

For security reasons Chrome disables GeoLocation for resources that come from the file system with the file:\\ scheme. If you host your page in IIS you should be good to go.

Upvotes: 2

Related Questions