我试图看看输入的lat和long是否与两种类型的多边形中的一种相对应,一种是带有Live的键,另一种带有一个很快的键,但是我似乎无法使if语句正确。
因此,如果一个纬度+长点位于多边形键实时,结果应该是,找到位置并且是实时的。如果一个纬度+长点在多边形键中很快结果应该是,找到位置并即将到来。如果lat + long不在多边形中,则结果应该是,找不到位置。然而,这最后的其他声明不起作用。
以下是codepen链接Codepen
依赖关系
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/geocomplete/1.6.5/jquery.geocomplete.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
HTML
<div id="location_container"class="jumbotron">
<div class="container">
<p>w11 2ed - soon</p>
<p>w10 5nr - live</p>
<p>sw19 5aw - not live and not coming soon</p>
<input id="location" placeholder="" type="text" class="form-control" autocomplete="off">
<div id="result"></div>
</div>
</div>
<div id="map_div"></div>
Jquery
function initialize() {
var map = new google.maps.Map(document.getElementById('map_div'), {
center: new google.maps.LatLng(51.5072, -0.1275),
zoom: 12,
mapTypeId: google.maps.MapTypeId.TERRAIN
});
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');
console.log('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');
console.log('Location is found and is coming soon')
}
}
else {
$( "#result" ).empty();
$("#result").html('Location is not found');
console.log('Location is not found')
}
});
});
map.data.loadGeoJson('https://gist.githubusercontent.com/SimonFricker/39540c7fb6e332456640/raw/7a9dee5b9ab7f9a04082c65f2378928b8c027dcd/polygon.json');
}
function initGeocompleteControl(map, complete) {
var option = {
//types: ['(cities)']
country: 'GB'
};
$("#location").geocomplete(option).bind("geocode:result", function (event, result) {
complete(result);
});
}
function calcBounds(geometry, bounds) {
if (geometry instanceof google.maps.LatLng) {
bounds.extend(geometry);
}
else if (geometry instanceof google.maps.Data.Point) {
bounds.extend(geometry.get());
}
else {
geometry.getArray().forEach(function (g) {
calcBounds(g, bounds);
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
答案 0 :(得分:1)
您的其他声明仅适用于多边形。如果该点在任何多边形之外,则永远不会执行。你需要添加一个标志来检测你何时在多边形内找到了这个点,如果你已经处理了所有多边形并且没有找到其中一个多边形的点,那么就执行“未找到”代码。
一个选项:
代码段
var marker;
function initialize() {
var map = new google.maps.Map(document.getElementById('map_div'), {
center: new google.maps.LatLng(51.5072, -0.1275),
zoom: 12,
mapTypeId: google.maps.MapTypeId.TERRAIN
});
initGeocompleteControl(map, function(result) {
if (!marker || !marker.setPosition) {
marker = new google.maps.Marker({
position: result.geometry.location,
map: map
});
} else {
marker.setPosition(result.geometry.location);
}
var inPolygon = false;
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');
console.log('Location is found and is live');
inPolygon = true;
}
} 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');
console.log('Location is found and is coming soon');
inPolygon = true;
}
}
});
if (!inPolygon) {
$("#result").empty();
$("#result").html('Location is not found');
console.log('Location is not found')
}
});
map.data.loadGeoJson('https://gist.githubusercontent.com/SimonFricker/39540c7fb6e332456640/raw/7a9dee5b9ab7f9a04082c65f2378928b8c027dcd/polygon.json');
}
function initGeocompleteControl(map, complete) {
var option = {
//types: ['(cities)']
country: 'GB'
};
$("#location").geocomplete(option).bind("geocode:result", function(event, result) {
complete(result);
});
}
function calcBounds(geometry, bounds) {
if (geometry instanceof google.maps.LatLng) {
bounds.extend(geometry);
} else if (geometry instanceof google.maps.Data.Point) {
bounds.extend(geometry.get());
} else {
geometry.getArray().forEach(function(g) {
calcBounds(g, bounds);
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map_div {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/geocomplete/1.6.5/jquery.geocomplete.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places"></script>
<div id="location_container" class="jumbotron">
<div class="container">
<p>w11 2ed - soon</p>
<p>w10 5nr - live</p>
<p>sw19 5aw - not live and not coming soon</p>
<input id="location" placeholder="" type="text" class="form-control" autocomplete="off">
<div id="result"></div>
</div>
</div>
<div id="map_div"></div>