2016-09-10 85 views
-1

我的谷歌地圖上有一個搜索框,在打字時似乎沒有顯示任何自動完成結果。地圖本身顯示,所以我相信我的api證書沒問題。我似乎失去了一些東西,但無法弄清楚什麼!在搜索框中鍵入目前什麼都不做谷歌地圖搜索框不工作

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

 
function initializeMap() { 
 

 
      var mapOptions = { 
 
       center: { lat: 41.8369, lng: -87.6847 }, 
 
       disableDefaultUI: true, // a way to quickly hide all controls 
 
       zoom: 8 
 
      }; 
 
      var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
 

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

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

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

 
       if (places.length == 0) { 
 
        return; 
 
       } 
 

 
       else { 
 
        place = places[0]; 
 
       } 
 

 
       var lat = place.geometry.location.lat(); 
 
       var lon = place.geometry.location.lng(); 
 

 
       
 
       // Clear out the old markers. 
 
       markers.forEach(function (marker) { 
 
        marker.setMap(null); 
 
       }); 
 
       markers = []; 
 

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

 

 
       var marker = new google.maps.Marker({ 
 
        map: map, 
 
        title: place.name, 
 
        draggable: true, 
 
        position: place.geometry.location 
 
       }); 
 

 
       
 

 

 
       markers.push(marker); 
 

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

 

 
       map.fitBounds(bounds); 
 
      }); 
 

 
     }

+1

請提供一個演示此問題的[mcve]。 [當我添加缺少的東西(小提琴)](http://jsfiddle.net/geocodezip/9Lexv0xr/),這意味着問題是你缺少你需要的東西,或者你的鑰匙不是已啓用Place API。 – geocodezip

+0

你是對的!原來我沒有啓用Place API。謝謝 – user1145998

回答

1

您需要啓用您的關鍵地方API。