简体   繁体   English

如何从Google Maps API获取城市和州名?

[英]How can I get just city and state from the Google Maps API?

I want to remove the country from the Google API map search.For example I don't want Atlanta,GA,USA but I want something like Atlanta,GA. 我想从Google API地图搜索中删除国家/地区。例如,我不希望美国佐治亚州亚特兰大,但我想要佐治亚州亚特兰大。 Is this possible? 这可能吗?

Here is my code. 这是我的代码。 The form in Laravel blade: Laravel刀片中的表格:

{!!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() !!}

The JavaScript code: JavaScript代码:

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

I am trying to do the exact same thing... (not show the ' , USA ' at the end of every city, State entry) 我正在尝试做完全相同的事情...(在每个城市的末尾都不要显示“ ,USA ”,请输入州名)

It's funny because I believe it used to work this way out of the box... see this old screenshot in the developer docs here: https://developers.google.com/maps/documentation/javascript/places-autocomplete#map_controls 这很有趣,因为我认为它曾经可以开箱即用...在开发人员文档中查看以下旧屏幕截图: https//developers.google.com/maps/documentation/javascript/places-autocomplete#map_controls 在此处输入图片说明

However if you go to their example it does not work this way: https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-hotelsearch 但是,如果您转到他们的示例,则该方法将无法正常运行: https//developers.google.com/maps/documentation/javascript/examples/places-autocomplete-hotelsearch 在此处输入图片说明

Look you thought that Input field was long enough for any city right? 您以为输入字段足够长,适合任何城市吗? wrong: 错误: 在此处输入图片说明

Here is my approach using MutationObserver to remove the Country every time! 这是我每次使用MutationObserver删除国家/地区的方法! This approach is inspired by this other stackoverflow post: JS: event listener for when element becomes visible? 这种方法的灵感来自另一个stackoverflow帖子: JS:何时元素可见的事件侦听器?

Update: this now works with IE (I have tested IE, Edge, Firefox, Chrome) 更新:现在可以在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> 

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

相关问题 JavaScript Google Maps Geolocation如何从中获取Zipcode,City,State? - JavaScript Google Maps Geolocation How can I get the Zipcode, City, State from this? 使用谷歌地图 api,如何从纬度和经度获取国家、state 和城市? - using google maps api, how to get country, state, and city from lat and long? 如何从谷歌地图上的标记获取城市名称和州/省 - How to get the city name and state/province from marker on google maps 如何从谷歌地图API获取城市和邮政编码? - How to get city and postal code from google maps API? 从谷歌地图请求中解析城市/州 - Parsing city/state from Google Maps request 如何通过选定的城市Google Maps API获取地址? - How to get address by selected city google maps api? 如何在Google Maps API上按城市+邮政编码获取距离 - How to get distance by city + zipCode on google maps API 如何在Google Maps API中使用Geocoder获取城市的Latlng价值? - How to get latlng value of a city using geocoder in google maps api? 我可以从 Google 地图自动填充中获取唯一的迪拜城市地址吗? - Can I get a only Dubai city address from Google Maps autocomplete? 如何在仅具有城市名称的Google Maps API上获取城市的Lat,Alt - How to get a city's Lat,Alt on google maps API having only the name of the city
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM