Javascript 在多个边界中搜索地图坐标
我有一些代码要求用户输入地址,并使用GoogleMaps API返回该地址的地理代码Lat/Long。我已经将其设置为查询这些坐标是否位于预先确定的边界内。我想做的是有一个多个边界,能够通过它们搜索并返回一个结果。例如,我会有一些JSON数据或其他类型的数组,其中包含SW_LatLng、NE_LatLng和答案。因此,我的查询将使用以下函数,如果为true,则返回答案。我的问题是如何循环通过多个边界并返回结果Javascript 在多个边界中搜索地图坐标,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我有一些代码要求用户输入地址,并使用GoogleMaps API返回该地址的地理代码Lat/Long。我已经将其设置为查询这些坐标是否位于预先确定的边界内。我想做的是有一个多个边界,能够通过它们搜索并返回一个结果。例如,我会有一些JSON数据或其他类型的数组,其中包含SW_LatLng、NE_LatLng和答案。因此,我的查询将使用以下函数,如果为true,则返回答案。我的问题是如何循环通过多个边界并返回结果 bounds.contains(new google.maps.LatLng(lati
bounds.contains(new google.maps.LatLng(latitude,longitude));
以下是我已有的代码:
function codeAddress() {
var bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(37,-109),
new google.maps.LatLng(41, -102)
);
var geocoder = new google.maps.Geocoder();
var address = document.getElementById('address').value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
}
var center = bounds.getCenter();
var x = bounds.contains(new google.maps.LatLng(latitude,longitude));
if (x) { alert('In Colorado')} else { alert('Outside Colorado')};
});
}
<h3>Is your Address in Colorado?</h3>
<input id="address" type="textbox" style="width:60%">
<input type="button" value="Find" onclick="codeAddress()">
函数代码地址(){
var bounds=new google.maps.LatLngBounds(
新google.maps.LatLng(37,-109),
新google.maps.LatLng(41,-102)
);
var geocoder=new google.maps.geocoder();
var address=document.getElementById('address')。值;
geocoder.geocode({'address':address},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
var latitude=results[0]。geometry.location.lat();
var longitude=results[0]。geometry.location.lng();
}
var center=bounds.getCenter();
var x=bounds.contains(新的google.maps.LatLng(纬度、经度));
if(x){alert('In Colorado')}else{alert('out Colorado')};
});
}
你的地址在科罗拉多州吗?
我在一个代码笔(比如JSFIDLE)中设置了这个,供您使用
感谢您的帮助。另外,隐藏数据的最佳方法是什么
这是JSFIDLE链接您是否考虑过使用多边形定义边界?然后使用几何体库中的google maps api containsLocation方法()确定点是否位于边界内 您似乎仅限于矩形区域(这对于您的目的可能已经足够好了,我不知道我只是在提出建议) 无论哪种方式,正如您所说的,您都需要在纬度/经度边界对数组中循环(称之为
latLngArray
)
函数代码地址(){
var geocoder=new google.maps.geocoder();
var address=document.getElementById('address')。值;
geocoder.geocode({'address':address},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
var latitude=results[0]。geometry.location.lat();
var longitude=results[0]。geometry.location.lng();
}
var latlngaraylength=latLngArray.getLength();
对于(i=0;i您在fiddle和codepen中的代码已损坏:Uncaught ReferenceError:initialize未定义
我修复了JSFIDLE,因此它现在可以工作。codepen对我来说工作正常。
function codeAddress() {
var geocoder = new google.maps.Geocoder();
var address = document.getElementById('address').value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
}
var latLngArrayLength = latLngArray.getLength();
for (i=0; i<latLngArrayLength; i++) {
var bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(latLngArray[i].latSW, latLngArray[i].lngSW)
new google.maps.LatLng(latLngArray[i].latNE, latLngArray[i].lngNE)
);
var center = bounds.getCenter(); //what is this for??
var x = bounds.contains(new google.maps.LatLng(latitude,longitude));
if (x) {
alert('In ' + latLngArray[i].boundaryName); //not sure what you want to return exactly
return latLngArray[i].boundaryName;
}
}
});
}