简体   繁体   English

Google Map搜索框自动填充Javascript

[英]Google Map Search Box Autocomplete Javascript

I used google maps search box to put markers on the map when i change search box to autocomplete it does't pin to map. 当我将搜索框更改为自动完成时,我使用Google Maps搜索框在地图上放置了标记,它不会固定在地图上。 I need only cities to search. 我只需要搜索城市。

I just changed searchBox to autocomplete with options which is type cities. 我只是将searchBox更改为自动填充,其中包含城市类型的选项。 As you can see on the links first link's search box works but i need autocomplete only cities. 正如您在链接上看到的那样,第一个链接的搜索框有效,但是我只需要自动填充城市。

Search box for all places this works but i need only cities https://jsfiddle.net/tolga748/8qax66ew/ 搜索框适用于所有可行的地方,但我只需要城市https://jsfiddle.net/tolga748/8qax66ew/

Autocomplete search box only cities this is not work https://jsfiddle.net/tolga748/fsxfrf3g/ 仅自动填充搜索框仅适用于无法运行的城市https://jsfiddle.net/tolga748/fsxfrf3g/

var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);


map.addListener('bounds_changed', function() {
  searchBox.setBounds(map.getBounds());
});

var markers = [];
searchBox.addListener('places_changed', function() {

    var places = searchBox.getPlaces();

    var input = document.getElementById('pac-input');
    var searchBox = new google.maps.places.SearchBox(input);
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

    map.addListener('bounds_changed', function() {
      searchBox.setBounds(map.getBounds());
    });

    var markers = [];

    searchBox.addListener('places_changed', function() {

    var places = searchBox.getPlaces();
}

For your autocomplete example, you're listening for an event of places_changed . 对于您的自动完成示例,您正在侦听places_changed事件。 In this case you need to use place_changed , and rather than looping over an array, just act on a click on a single result. 在这种情况下,您需要使用place_changed ,而不是循环遍历数组,只需单击单个结果即可。

So your autocomplete event becomes something like this: 因此,您的自动完成事件会变成这样:

     autocomplete.addListener('place_changed', function() {

          var place = autocomplete.getPlace();

          // For each place, get the icon, name and location.
          var bounds = new google.maps.LatLngBounds();

            if (!place.geometry) {
              console.log("Returned place contains no geometry");
              return;
            }
            var icon = {
              url: place.icon,
              size: new google.maps.Size(71, 71),
              origin: new google.maps.Point(0, 0),
              anchor: new google.maps.Point(17, 34),
              scaledSize: new google.maps.Size(25, 25)
            };

            // Create a marker for each place.
            markers.push(new google.maps.Marker({
              map: map,
              icon: icon,
              title: place.name,
              position: place.geometry.location
            }));



            $(".list-area ul").append("<li><div class='list-area-left'><span> " + place.adr_address + "</span></div><div class='list-area-right'><a href='#''><img src='http://iconshow.me/media/images/ui/ios7-icons/png/512/minus-outline.png' width='25'></a></div></li>");






            if (place.geometry.viewport) {
              // Only geocodes have viewport.
              bounds.union(place.geometry.viewport);
            } else {
              bounds.extend(place.geometry.location);
            }



          map.fitBounds(bounds);
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM