【问题标题】:Google Map API to find nearby restaurant around my placeGoogle Map API 可在我附近查找附近的餐厅
【发布时间】:2016-07-14 03:41:21
【问题描述】:

我正在研究 Google map api [这是我在餐厅附近找到的。 . .当我运行它时,我只得到当前位置。我没有收到任何与餐厅数据相关的信息。

<script>

      function initMap() {
          var map = new google.maps.Map(document.getElementById('map'), {
              center: { lat: 25.276987, lng: 55.296249 },
              zoom: 15
          });

          var infoWindow = new google.maps.InfoWindow({ map: map });

          if (navigator.geolocation) {
              navigator.geolocation.getCurrentPosition(function (position) {

                  var pos = {
                      lat: position.coords.latitude,
                      lng: position.coords.longitude     
                  };
                  infoWindow.setPosition(pos);
                  infoWindow.setContent('Location found.');
                  map.setCenter(pos);
                  var marker = new google.maps.Marker({
                      map: map,
                      position: pos.getPlace().location
                  });
                  var service = new google.maps.places.PlacesService(map);
                  service.nearbySearch({
                      location: rak,
                      radius: 5500,
                      type: ['restaurant'] // not resturant
                  }, callback);

              }, function () {
                  handleLocationError(true, infoWindow, map.getCenter());
              });
          }
          else {            
              handleLocationError(false, infoWindow, map.getCenter());
          }
      }

【问题讨论】:

    标签: javascript asp.net api google-maps


    【解决方案1】:

    也许您遇到了有关附近搜索位置标记的错误。您没有启动变量rak,它将返回一个空值。

    设置脚本

    <script
        src="https://maps.googleapis.com/maps/api/js?key=API_KEY&libraries=places&callback=initMap"
        async defer></script>
    

    初始化变量

    var map;
    var infowindow;
    var myPlace = {lat: 25.276987, lng: 55.296249 };
    

    致电您的附近搜索

    var service = new google.maps.places.PlacesService(map);
                service.nearbySearch({
                    location : myPlace,
                    radius : 5500,
                    type : [ 'restaurant' ]
                }, callback);
    

    检查搜索结果,然后为每个找到的位置创建一个标记

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

    您可以在document 中找到此代码,它将很好地解释正确的编码以及如何使用 api。这里还有list of supported location type

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多