【问题标题】:How can I get just city and state from the Google Maps API?如何从 Google Maps API 获取城市和州?
【发布时间】:2018-07-25 07:47:37
【问题描述】:

我想从 Google API 地图搜索中删除国家/地区。例如,我不想要美国佐治亚州亚特兰大,但我想要像佐治亚州亚特兰大这样的国家。这可能吗?

这是我的代码。 Laravel Blade 中的表单:

{!!Form::open(array('url'=>'search','method'=>'get','id'=>'theform')) !!}
{{Form::text('mywhat','',array('placeholder'=>'Search Business or Keywords','class'=>'form-control f1','size'=>'50%','height'=>'auto','id'=>'whatty','autocomplete'=>'off'))}}
{{Form::text('mywhere','',array('placeholder'=>'City State or ZipCode','class'=>'form-control','height'=>'auto','id'=>'location-input','autocomplete'=>'off'))}}
{{Form::submit('Search', array('class'=>'btn btn-warning bbt','id'=>'button'))}}
{!!Form::close() !!}

JavaScript 代码:

<script type="text/javascript"> 
   var searchbox=new google.maps.places.SearchBox(document.getElementById('location-input'));
</script>

【问题讨论】:

  • 展示你的尝试
  • 1) 编辑您的问题并在那里添加您的代码,而不是在 cmets 中。 2) 这段 javascript 在上下文中没有多大意义。你有与此相关的 HTML 吗?
  • 您应该查看 Google 的 API 文档。它们对 API 返回的内容进行了非常全面的细分。我的猜测是您可以在将其渲染到您的表单之前拦截返回并首先对其进行自定义。
  • @Luke 我一直在寻找一整天,我没有看到任何关于仅获取城市和州或在 google api 上删除国家/地区的内容。很抱歉一直打扰你,但我真的很生气。
  • 它没有告诉我任何关于删除国家的信息。我正在尝试获取城市和州,例如乔治亚州亚特兰大而不是美国乔治亚州亚特兰大。

标签: javascript php laravel google-maps


【解决方案1】:

我正在尝试做完全相同的事情...(不在每个城市的末尾显示“, USA”,州条目)

这很有趣,因为我相信它曾经以这种方式开箱即用......在此处的开发人员文档中查看这个旧屏幕截图: https://developers.google.com/maps/documentation/javascript/places-autocomplete#map_controls

但是,如果您查看他们的示例,它不会以这种方式工作: https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-hotelsearch

您认为输入字段对于任何城市都足够长,对吧?错误的:

这是我每次使用 MutationObserver 删除 Country 的方法! 这种方法的灵感来自另一个 stackoverflow 帖子:JS: event listener for when element becomes visible?

更新:这现在适用于 IE(我已经测试过 IE、Edge、Firefox、Chrome)

<!DOCTYPE html>
<html>
  <head>
    <title>Place Autocomplete Test</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
    </style>
  </head>

  <body>

    <div id="findhotels">
      Find City State:
    </div>

    <div id="locationField">
      <input id="autocomplete" placeholder="Enter a city" type="text" />
    </div>

    <script>

      var autocomplete;
	  var addressEle = document.getElementById('autocomplete');
	  var dropdown;
	  var times = 0;
	  
      function initAutocomplete() {
        
        // 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(
            (addressEle), {
              types: ['(cities)'],
              componentRestrictions: {'country': 'us'}
            });
			
        autocomplete.addListener('place_changed', onPlaceChanged);
      }
	  
	  function initAutoObserver(){
		dropdown = document.querySelector('div.pac-container.pac-logo');
		if( dropdown){
			if(times){ console.log( 'IE sucks... recursive called '+times+' times.' ); }
		  //Literally the ONLY listener google gives us is the 'place_changed' Listener
		  //So we are having to use a MutationObserver to detect when the dropdown is shown/hidden 
		  //When Dropdown changes, remove ', USA'
			var observer = new MutationObserver(function(){
			   if(dropdown.style.display !='none' ){
					//console.log('is visible');
					for(var i=0,l=dropdown.children.length; i<l; i++){ 
						var thespan = dropdown.children[i].lastElementChild
						thespan.innerHTML = thespan.innerHTML.replace(', USA','');
					}
			   } else {
					//console.log('is hidden');
					//console.log(addressEle.value);
					addressEle.value = addressEle.value.replace(', USA','');
			   }
			});
			observer.observe(dropdown,  { attributes: true, childList: true });
		}else {
			//IE seems to take longer to add the dropdown element to the dom:
			times++;
			setTimeout( initAutoObserver, 20);
		}
	  }
	  window.addEventListener("load", initAutoObserver );

      // When the user selects a city, get the place details for the city
      function onPlaceChanged() {
        var place = autocomplete.getPlace();
        if (place.geometry) {
			//console.log(place);
        } else {
			addressEle.placeholder = 'Enter a city';
        }
      }
	  
	//Prevent Numbers (Allow Only Letters) from Being entered into the City, State Field:
    addressEle.addEventListener('input', function (event) {
        this.value = this.value.replace(/[^a-zA-Z -,]/g, "");
    });
	
      // <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">
    </script>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCvRwR3-fGr8AsnMdzmQVkgCdlWhqUiCG0&libraries=places&callback=initAutocomplete"
        async defer></script>
  </body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-17
    • 1970-01-01
    • 2015-08-01
    • 2012-11-18
    • 1970-01-01
    • 2012-07-08
    • 2017-09-02
    • 2019-05-27
    相关资源
    最近更新 更多