2016-09-10 83 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。