带标记的地理位置地图

时间:2014-01-08 15:20:02

标签: api geolocation maps

我想在我的网站上使用地图api。我已经完成了地理位置工作,所以它集中在我的位置。但我也想添加标记。我正在建立一个网站,找到你所在地附近的咖啡店,所以我只想在谷歌地图中搜索“Coffeeshop”时出现的所有标记出现在onload上。我在te Maps API网站上搜索过但我找不到我想要的内容。

提前致谢!

这是我的代码:

    <!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, {
        height: 100%;
      }

      #map-canvas {
        height: 400px;
        width: 80%;
        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 type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAcITWIS3AIRREEJG7oC_BU7f8jVV9MbfE&sensor=false"></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: 9
  };
  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>

1 个答案:

答案 0 :(得分:0)

您需要使用Google Places API for JS来搜索调用navigator.geolocation.getCurrentPosition()返回的纬度/经度位置附近的位置。

https://developers.google.com/maps/documentation/javascript/places#place_search_requests

places API支持按预定义的地点类型进行搜索。他们没有专门的“咖啡店”,但他们确实有“咖啡馆”:

https://developers.google.com/places/documentation/supported_types