我试图看看输入的lat和long是否与两种类型的多边形中的一种对应,一种是带有Live的键,另一种是带有Soon的键,但是我似乎无法写入if语句。
因此,如果纬度+长点位于多边形类型中,则结果应该是,找到位置并且是实时的。如果纬度+长点是多边形类型很快结果应该是,找到位置并即将到来。如果lat + long不在多边形中,则结果应该是,找不到位置。
我的if语句出了什么问题?
initGeocompleteControl(map, function (result) {
map.data.forEach(function (f) {
if (f.getProperty("key") == "live") {
var bounds = new google.maps.LatLngBounds();
calcBounds(f.getGeometry(), bounds);
if (bounds.contains(result.geometry.location)) {
$( "#result" ).empty();
$("#result").html('Location is found and is live');
} else if (f.getProperty("key") == "soon") {
var bounds = new google.maps.LatLngBounds();
calcBounds(f.getGeometry(), bounds);
if (bounds.contains(result.geometry.location)) {
$( "#result" ).empty();
$("#result").html('Location is found and is coming soon');
} else {
$( "#result" ).empty();
$("#result").html('Location is not found');
}
//w11 2ed
}
// w10 5nr
}
});
});
答案 0 :(得分:2)
把你的其他条件放在外面即。在if(f.getProperty("key"...
之外
initGeocompleteControl(map, function (result) {
map.data.forEach(function (f) {
if (f.getProperty("key") == "live") {
var bounds = new google.maps.LatLngBounds();
calcBounds(f.getGeometry(), bounds);
if (bounds.contains(result.geometry.location)) {
$( "#result" ).empty();
$("#result").html('Location is found and is live');
}
//the else if moved from here
}
else if
(f.getProperty("key") == "soon") {
var bounds = new google.maps.LatLngBounds();
calcBounds(f.getGeometry(), bounds);
if (bounds.contains(result.geometry.location)) {
$( "#result" ).empty();
$("#result").html('Location is found and is coming soon');
}
//else condition moved from here
} //and else condition after the else if condtion
else {
$( "#result" ).empty();
$("#result").html('Location is not found');
}
//w11 2ed
// w10 5nr
});
});