我无法获得Google的Places服务输出

时间:2011-10-20 19:33:05

标签: javascript google-maps google-maps-api-3 geolocation

在我的代码中,首先,我试图通过Google地图上的GeoLocation服务找到用户的位置。

其次,我试图通过谷歌的位置服务附近的用户位置显示'酒吧',就像你去喝酒的​​地方一样。它应该在用户位置附近的“Bars”上放置标记。

然而,这不起作用,我遇到了一个问题。但我不知道它是什么。它在我的位置附近打开没有“Bars”的地图。

我的css代码:

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}

#map_canvas {
  height: 100%;
}

@media print {
  html, body {
    height: auto;
  }



  #map_canvas {
    height: 650px;
  }
}

这是我的javascript和我的HTML代码=

    <!DOCTYPE html>
<html>
  <head>
    <title>Google Maps JavaScript API v3 Example: Map Geolocation</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="UTF-8">
    <link href="mapcss.css" rel="stylesheet" type="text/css">
    <!--
    Include the maps javascript with sensor=true because this code is using a
    sensor (a GPS locator) to determine the user's location.
    See: http://code.google.com/apis/maps/documentation/javascript/basics.html#SpecifyingSensor
    -->
    <script type="text/javascript"
        src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>

    <script type="text/javascript">
      var map;

      function initialize() {
        var myOptions = {
          zoom: 6,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById('map_canvas'),
            myOptions);

        // 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);

            var request = {
                location: pos,
                radius: 500,
                types: ['bar']
            };

            infowindow = new google.maps.InfoWindow();
            var service = new google.maps.places.PlacesService(map);
            service.search(request, callback);

          },

      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(45.4643898,9.1883469),
          content: content
        };

        var infowindow = new google.maps.InfoWindow(options);
        map.setCenter(options.position);
      }
      function callback(results, status) {
        if (status == google.maps.places.PlacesServiceStatus.OK) {
          for (var i = 0; i < results.length; i++) {
            createMarker(results[i]);
          }
        }
      }

      function createMarker(place) {
        var placeLoc = place.geometry.location;
        var marker = new google.maps.Marker({
          map: map,
          position: place.geometry.location
        });

        google.maps.event.addListener(marker, 'click', function() {
          infowindow.setContent(place.name);
          infowindow.open(map, this);
        });
      }

      google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
    <div id="map_canvas"></div>
  </body>
</html>

1 个答案:

答案 0 :(得分:1)

Google地方信息搜索通常是空的,特别是考虑到当前时间Google地方数据库的稀疏程度(或者是我最后一次尝试它)。有关其他详细信息和建议,请参阅Google Maps Javascript API V3 : Search Requests