我将地图功能包含在脚本行
中<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=geometry,drawing&sensor=false"></script>
完整的代码就是这个
var map, currentInfoWindow, drawingManager, selectedShape;
var markers = [];
var selectedMarkers = [];
var shapes = [];
function initialize() {
var mapOptions = {
center: new google.maps.LatLng({0},{1}), // this comes from the db
zoom: 8,
tilt: 30
};
var mapDiv = document.getElementById('mapDiv');
var height = Math.max(0, document.documentElement.clientHeight - 110).toString();
mapDiv.setAttribute('style','height: ' + height + 'px');
map = new google.maps.Map(mapDiv, mapOptions);
// {2}
// here I create the markers with db info, it works fine
// var marker{0} = new google.maps.Marker({{ position: new google.maps.LatLng({1},{2}), map: map, title: '{3}', icon: pinImage{0} }}); markers.push(marker{0});
drawingManager = new google.maps.drawing.DrawingManager({
drawingControl: true,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: [google.maps.drawing.OverlayType.POLYGON]
},
map: map
});
google.maps.event.addListener(drawingManager, 'polygoncomplete', function(polygon) {
shapes.push(polygon);
drawingManager.setDrawingMode(null);
var newShape = polygon.overlay;
google.maps.event.addListener(newShape, 'click', function() {
setSelection(newShape);
});
setSelection(newShape);
});
google.maps.event.addListener(drawingManager, 'drawingmode_changed', clearSelection);
google.maps.event.addListener(map, 'click', clearSelection);
google.maps.event.addDomListener(document.getElementById('btnDeleteSelectedShape'), 'click', deleteSelectedShape);
google.maps.event.addDomListener(document.getElementById('btnDeleteAllShapes'), 'click', deleteAllShapes);
google.maps.event.addDomListener(document.getElementById('btnSelectMarkers'), 'click', setSelectedMarkers);
}
function addInfoWindow(marker, content) {
var infoWindow = new google.maps.InfoWindow({
content: content
});
google.maps.event.addListener(marker, 'click', function () {
if (currentInfoWindow != null) {
currentInfoWindow.close();
}
infoWindow.open(marker.get('map'), marker);
currentInfoWindow = infoWindow;
});
}
function markerSelection(spanTitle, button) {
var hidden = document.getElementById('hiddenTitle');
hidden.value = spanTitle.textContent;
button.disabled = true;
__doPostBack('','');
}
function clearSelection() {
if (selectedShape) {
selectedShape.setEditable(false);
selectedShape = null;
}
}
function setSelection(shape) {
clearSelection();
selectedShape = shape;
shape.setEditable(true);
}
function deleteSelectedShape() {
if (selectedShape) {
selectedShape.setMap(null);
}
}
function deleteAllShapes() {
for (var i = 0; i < shapes.length; i++) {
shapes[i].overlay.setMap(null);
}
shapes = [];
}
function setSelectedMarkers() {
var hidden = document.getElementById('hiddenTitle');
for (var i = 0; i < shapes.length; i++) {
for (var k = 0; i < markers.length; i++) {
var point = new google.maps.LatLng(markers[k].getPosition().lat(), markers[k].getPosition().lng());
console.log(point);
if ($.inArray(markers[k], selectedMarkers) == -1
&& google.maps.geometry.poly.containsLocation(point, shapes[i]))
{
selectedMarkers.push(markers[k]);
alert(selectedMarkers.length);
hidden.value += markers[k].getTitle() + ',';
alert(hidden.value);
}
}
}
}
google.maps.event.addDomListener(window, 'load', initialize);
我怀疑代码中有&#39; polygoncomplete&#39;可能是错的,我也在那个部分得到了javascript错误。
答案 0 :(得分:0)
错误就在这一行
google.maps.geometry.poly.containsLocation(point, shapes[i]))
这样写的时候工作正常
google.maps.geometry.poly.containsLocation(point, shapes[i].overlay))
@ Dr.Molle感谢您发现循环错误