Mr.*_* B. 8 javascript geometry google-maps google-maps-api-3
我试图google.maps.Circle通过使用这里google.maps.geometry.poly.containsLocation推荐的方式获得给定半径()内的所有标记,但是我得到一个错误:.TypeError: e is undefined
片段:
// ...
if (google.maps.geometry.poly.containsLocation(randomMarkers[i].marker.getPosition(), searchArea)) {
console.log('=> is in searchArea');
} else {
console.log('=> is NOT in searchArea');
}
// ...
Run Code Online (Sandbox Code Playgroud)
完整代码:
<!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
<script>
var map,
searchArea,
searchAreaMarker,
searchAreaRadius = 1000, // metres
startLat = 40.782827,
startLng = -73.966167
;
function init() {
var startLatLng = new google.maps.LatLng(startLat, startLng);
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: startLatLng,
zoom: 12
});
searchArea = new google.maps.Circle({
strokeColor: '#FF0000',
strokeOpacity: 0.5,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.2,
map: map,
center: startLatLng,
radius: searchAreaRadius
});
searchAreaMarker = new google.maps.Marker({
position: startLatLng,
map: map,
draggable: true,
animation: google.maps.Animation.DROP,
title: 'searchAreaMarker'
});
var randomMarkers = [
{ title: 'Marker 1', latLng: new google.maps.LatLng(40.770088, -73.971146) },
{ title: 'Marker 2', latLng: new google.maps.LatLng(40.782048, -73.972691) },
{ title: 'Marker 3', latLng: new google.maps.LatLng(40.769048, -73.987797) },
{ title: 'Marker 4', latLng: new google.maps.LatLng(40.773858, -73.956211) },
{ title: 'Marker 5', latLng: new google.maps.LatLng(40.800372, -73.952091) },
{ title: 'Marker 6', latLng: new google.maps.LatLng(40.804661, -73.939388) }
];
for (var i = 0; i < randomMarkers.length; i++) {
randomMarkers[i].marker = new google.maps.Marker({
position: randomMarkers[i].latLng,
map: map,
title: randomMarkers[i].title
});
}
google.maps.event.addListener(searchAreaMarker, 'dragend', function(e) {
startLatLng = e.latLng;
searchArea.setOptions({
center: startLatLng
});
map.panTo(searchAreaMarker.getPosition());
// find markers in area
for (var i = 0; i < randomMarkers.length; i++) {
console.log('Marker: ' + randomMarkers[i].marker.title + ', position: ' + randomMarkers[i].marker.getPosition());
// ---------- Here comes the error:
// TypeError: e is undefined
if (google.maps.geometry.poly.containsLocation(randomMarkers[i].marker.getPosition(), searchArea)) {
console.log('=> is in searchArea');
} else {
console.log('=> is NOT in searchArea');
}
}
});
}
google.maps.event.addDomListener(window, 'load', init);
</script>
Run Code Online (Sandbox Code Playgroud)
geo*_*zip 21
containsLocation是google.maps.Polygon对象上的一种方法,而不是google.maps.Circle对象
要确定标记是否在圆圈内,请使用google.maps.geometry.spherical.computeDistanceBetween
if (google.maps.geometry.spherical.computeDistanceBetween(randomMarkers[i].marker.getPosition(), searchArea.getCenter()) <= searchArea.getRadius()) {
console.log('=> is in searchArea');
} else {
console.log('=> is NOT in searchArea');
}
Run Code Online (Sandbox Code Playgroud)
工作代码片段:
var map,
searchArea,
searchAreaMarker,
searchAreaRadius = 1000, // metres
startLat = 40.782827,
startLng = -73.966167;
function init() {
var startLatLng = new google.maps.LatLng(startLat, startLng);
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: startLatLng,
zoom: 12
});
searchArea = new google.maps.Circle({
strokeColor: '#FF0000',
strokeOpacity: 0.5,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.2,
map: map,
center: startLatLng,
radius: searchAreaRadius
});
searchAreaMarker = new google.maps.Marker({
position: startLatLng,
map: map,
draggable: true,
animation: google.maps.Animation.DROP,
title: 'searchAreaMarker'
});
var randomMarkers = [{
title: 'Marker 1',
latLng: new google.maps.LatLng(40.770088, -73.971146)
}, {
title: 'Marker 2',
latLng: new google.maps.LatLng(40.782048, -73.972691)
}, {
title: 'Marker 3',
latLng: new google.maps.LatLng(40.769048, -73.987797)
}, {
title: 'Marker 4',
latLng: new google.maps.LatLng(40.773858, -73.956211)
}, {
title: 'Marker 5',
latLng: new google.maps.LatLng(40.800372, -73.952091)
}, {
title: 'Marker 6',
latLng: new google.maps.LatLng(40.804661, -73.939388)
}];
for (var i = 0; i < randomMarkers.length; i++) {
randomMarkers[i].marker = new google.maps.Marker({
position: randomMarkers[i].latLng,
map: map,
title: randomMarkers[i].title
});
}
google.maps.event.addListener(searchAreaMarker, 'dragend', function(e) {
startLatLng = e.latLng;
searchArea.setOptions({
center: startLatLng
});
map.panTo(searchAreaMarker.getPosition());
// find markers in area
for (var i = 0; i < randomMarkers.length; i++) {
console.log('Marker: ' + randomMarkers[i].marker.title + ', position: ' + randomMarkers[i].marker.getPosition());
// ---------- Here comes the error:
// TypeError: e is undefined
if (google.maps.geometry.spherical.computeDistanceBetween(randomMarkers[i].marker.getPosition(), searchArea.getCenter()) <= searchArea.getRadius()) {
console.log('=> is in searchArea');
} else {
console.log('=> is NOT in searchArea');
}
}
});
}
google.maps.event.addDomListener(window, 'load', init);Run Code Online (Sandbox Code Playgroud)
html,
body,
#map-canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}Run Code Online (Sandbox Code Playgroud)
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
<div id="map-canvas" style="border: 2px solid #3872ac;"></div>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8678 次 |
| 最近记录: |