【问题标题】:How to set bounds for Google Places Autocomplete using drop down如何使用下拉菜单设置 Google Places Autocomplete 的范围
【发布时间】:2018-02-18 13:54:48
【问题描述】:

我在我的应用程序中使用了 Google Places API,并且能够为自动完成设置边界,并且只有我的首选(纽约市)的下拉菜单。我希望能够在下拉列表中为我的其他城市设置它,以便当用户从纽约市更改为芝加哥时 - 自动完成仅注册来自芝加哥的结果。现在,当我选择 Chicago - console.log 注册 Chicago LatLng 但自动完成只显示 NYC 结果。我在下面列出了我的代码,非常感谢任何帮助。我感觉它需要一个事件监听器来传递更改,但我所有的努力都还没有成功。

Autocomplete.js

 var map, places, infoWindow;
  var markers = [];
  var autocomplete;      
  ;

   function cafeInitMap(lat, lng,latnorth, lngeast, store) {
    map = new google.maps.Map(document.getElementById('map'), {
      center: new google.maps.LatLng(40.751,-73.99),
      zoom: 11,          
      mapTypeControl: false,
      panControl: false,
      zoomControl: false,
      streetViewControl: false
    });

    infoWindow = new google.maps.InfoWindow({
      content: document.getElementById('info-content')
    });

    var defaultBounds = new google.maps.LatLngBounds(
              new google.maps.LatLng(lat, lng),
              new google.maps.LatLng(latnorth, lngeast));

    // Create the autocomplete object and associate it with the UI input control.
    // Restrict the search to the default country, and to place type "cities".
    autocomplete = new google.maps.places.Autocomplete(
        /** @type {!HTMLInputElement} */ (
            document.getElementById('autocomplete')), {
                bounds: defaultBounds,
                strictBounds: true
        });
    places = new google.maps.places.PlacesService(map);
}
$(document).ready(function() {
  $("#cafe_city_id").on('change', hanldeGoogleMapDraw);
  function hanldeGoogleMapDraw(e) {
var $place = $("#cafe_city_id"),

    lat,
    latnorth,
    searchType = '',
    lng,
    lngeast;

if($place.find(':selected').length === 0) return;   

lat = $place.find(':selected').data('south');
lng = $place.find(':selected').data('west');    
latnorth = $place.find(':selected').data('north');
lngeast = $place.find(':selected').data('east');
$('#search-results-dropdown').html('');
cafeInitMap(lat, lng,latnorth, lngeast);
}
});

Automcomplete.html.erb

  <div class="col-md-6">
<div class="field">
<%= f.label :city %><br>
<select class="form-control" name="cafe[city_id]" id="cafe_city_id">
    <option></option>
    <% City.all.each do |city| %>
        <option value="<%= city.id %>" data-south="<%= city.latitude %>" data-west="<%= city.longitude %>" data-north="<%= city.latitudenorth  %>" data-east="<%= city.longitudeeast %>" >    <%= city.name %></option>
    <% end %>
</select>
</div>
</div>
<div class="col-md-6">   
   <div id="locationField">
  <input id="autocomplete" placeholder="Type a Location" type="text" />
</div>

Seeds.rb

City.destroy_all
nyc = City.create!(name:"New York City", latitude: 40.495992, longitude: -74.029988, latitudenorth: 40.915568, longitudeeast: -73.699215)
sf = City.create!(name:"San Francisco", latitude: 37.7749295, longitude: -122.4194, latitudenorth: 37.7152613, longitudeeast: -122.5206928)
austin = City.create!(name:"Austin", latitude: 30.267153, longitude: -97.7430608, latitudenorth: 30.3074624, longitudeeast: -98.0335911)
la = City.create!(name:"Los Angeles", latitude: 34.0522342, longitude: -118.2436849, latitudenorth: 34.0201613, longitudeeast: -118.6919205)

【问题讨论】:

  • 您应该监听place_changed 事件。 developers.google.com/maps/documentation/javascript/…
  • 感谢@max 的指导。那么应该是这样吗google.maps.event.addListener(autocomplete, 'place_changed', function () {map.set.defaultBounds(new google.maps.LatLng(lat, lng, latnorth, lngeast)); });
  • 我对你到底想做什么有点困惑。我想我的答案已经弄清楚了。

标签: javascript jquery ruby-on-rails google-maps google-places-api


【解决方案1】:
function cafeInitMap(){
  var map = new google.maps.Map(document.getElementById('map'), {
    center: new google.maps.LatLng(40.751,-73.99),
    zoom: 11,          
    mapTypeControl: false,
    panControl: false,
    zoomControl: false,
    streetViewControl: false
  });
  var infoWindow = new google.maps.InfoWindow({
    content: document.getElementById('info-content')
  });
  var defaultBounds = function(){
    // Use the first empty options tag for the default location or provide 
    // a lat lng literal
    return $(this).find('option:selected').data() || $(this).find('option:first').data();
  }
  var autocomplete = new google.maps.places.Autocomplete(document.getElementById('autocomplete'), {
    bounds: defaultBounds(),
    strictBounds: true
  });
  var places = google.maps.places.PlacesService(map);

  // Set an event listener on the select and
  // use it to update the bounds of the autocomplete
  $(document).on('change','#cafe_city_id', function(){
    var lat_lng = $(this).find('option:selected').data();
    autocomplete.setBounds(lat_lng);
    // you may need to trigger a change event as well 
    // for google.maps.places.Autocomplete to fetch new results
    // $('#autocomplete').trigger('change');
  });
  // If you want to update the map as well then you can set a listener so 
  // that the map bounds conform to the autocomplete when the data is loaded:
  google.maps.event.addListener(autocomplete, 'place_changed', function () {
    map.setBounds( autocomplete.getBounds() );
  });
}

【讨论】:

  • 我认为它几乎就在那里,我似乎根本无法显示地图或任何结果。我认为您的答案没有初始化地图?因为我原来的仍然显示地图
猜你喜欢
  • 2011-12-15
  • 2017-01-30
  • 1970-01-01
  • 1970-01-01
  • 2012-04-11
  • 2015-11-12
  • 2019-04-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多