【问题标题】:Geolocation Maps with markers带标记的地理位置地图
【发布时间】:2014-01-08 15:20:01
【问题描述】:

我想在我的网站中添加地图 API。我已经让地理定位工作了,所以它以我的位置为中心。但我也想添加标记。我正在制作一个网站来定位您所在位置附近的咖啡店,所以我只希望我在谷歌地图中搜索“咖啡店”时出现的所有标记在加载时显示。我在 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>

【问题讨论】:

    标签: api geolocation maps


    【解决方案1】:

    您需要使用 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

    【讨论】:

    • 感谢您的回复。不能预定义搜索查询吗?谢谢。
    猜你喜欢
    • 1970-01-01
    • 2017-06-30
    • 2020-06-02
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 2012-05-30
    相关资源
    最近更新 更多