简体   繁体   English

如何按城市(位置)Google地图过滤位置数组

[英]How do I filter location array by city (location) Google Maps

I can't find any articles in Google Maps documentation hope somebody here did or know how to achieve that what I need. 我在Google Maps文档中找不到任何文章希望这里的人有所作为或知道如何实现我所需要的。

So basically I have array of locations 所以基本上我有array of locations

example of array 数组的例子

const array = [
  {
    latitude: 37.783476,
    longitude: -122.425412,
  }
]

Now I need to filter this array based on city (eg San Francisco). 现在,我需要根据城市(例如,旧金山)过滤此数组。

How can I achieve this? 我该如何实现?
Any links to documentation or any ideas much appreciated. 任何文档链接或任何想法都值得赞赏。

You can achieve it using reverse geocoding. 您可以使用反向地理编码来实现。

js fiddle: https://jsfiddle.net/fatty/bdartuph/12/ js小提琴: https : //jsfiddle.net/fatty/bdartuph/12/

Here i have taken two coordinates in array - san francisco and new york. 在这里,我采用了两个坐标数组-旧金山和纽约。 So the array will get filtered by the city you write in input box and will get store in filteredArray. 因此,该数组将根据您在输入框中写入的城市进行过滤,并将存储在filteredArray中。

 var coordArray = [{ latitude: 37.783476, longitude: -122.425412, }, { latitude: 40.730885, longitude: -73.997383, } ]; $("#btn").click(function() { var filteredArray = []; var cityName = $("#cityName").val(); var geocoder = new google.maps.Geocoder(); for (var i = 0; i < coordArray.length; i++) { var latlng = new google.maps.LatLng(coordArray[i].latitude, coordArray[i].longitude); geocoder.geocode({ 'latLng': latlng }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { var address = results[0].formatted_address.split(','); alert("" + address[1]); if (address[1].toLocaleLowerCase().trim() == cityName.toLocaleLowerCase()) { filteredArray.push(coordArray[i]); } } else { alert("Geocoder failed due to: " + status); } }); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script> <input id="cityName" type="text" placeholder="write city name"> <input id="btn" type="button" value="filter coordinates" /> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM