【问题标题】:Using NearbySearch in Google Maps API Places Library not working在 Google Maps API 地点库中使用 NearbySearch 不起作用
【发布时间】:2017-06-23 13:03:28
【问题描述】:

在 Google Maps API Places Library 中,我使用以下代码。但是,当用户单击 id="find" 的按钮时,虽然地图以用户位置为中心,但不会返回附近的地点。我正在混合来自位置库和地理定位 API 的代码,但我看不出我做错了什么。

任何帮助将不胜感激!

<script>

  var map, infoWindow;
  function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
      center: {lat: 51.516401, lng: -0.152218},
      zoom: 17
    });
    infoWindow = new google.maps.InfoWindow;

    $("#find").click(function(){

    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.');
        infoWindow.open(map);
        map.setCenter(pos);

        var pyrmont = new google.maps.LatLng(pos.lat,pos.lng);  

        var request = {
            location: pyrmont,
            radius: '1000',
            query: 'restaurant'
        };

        service = new google.maps.places.PlacesService(map)

        service.textSearch(request, callback);


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

      handleLocationError(false, infoWindow, map.getCenter());
    }
  });

}

  function handleLocationError(browserHasGeolocation, infoWindow, pos) {
    infoWindow.setPosition(pos);
    infoWindow.setContent(browserHasGeolocation ?
                          'Error: The Geolocation service failed.' :
                          'Error: Your browser doesn\'t support geolocation.');
    infoWindow.open(map);
  }

    function callback(results, status) {
        if (status == google.maps.places.PlacesServiceStatus.OK) {
        for (var i = 0; i < results.length; i++) {
        var place = results[i];
        createMarker(results[i]);
            }
        }
    }


</script>

【问题讨论】:

  • 我在发布代码Uncaught ReferenceError: createMarker is not defined时收到一个javascript错误@
  • 你没有定义 createMarker 函数

标签: javascript google-maps google-places-api


【解决方案1】:

添加createMarker函数

function createMarker(place) {

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-25
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 2013-11-04
    • 2013-11-16
    相关资源
    最近更新 更多