我有一个在我的网站上绘制多边形地图的功能。
通过错误控制它 TypeError:k [bd] .maps.Load不是函数
我的代码
<script src="http://maps.google.com/maps/api/js?sensor=false&libraries=drawing" type="text/javascript"></script>
<script src="http://maps.gstatic.com/maps-api-v3/api/js/21/4/main.js"></script>
<script src="http://maps.gstatic.com/maps-api-v3/api/js/21/4/drawing.js"></script>
<script type="text/javascript">
var drawingManager;
var selectedShape;
var colors = ['#1E90FF', '#FF1493', '#32CD32', '#FF8C00', '#4B0082'];
var selectedColor;
var colorButtons = {};
function clearSelection() {
if (selectedShape) {
selectedShape.setEditable(false);
selectedShape = null;
}
document.getElementById('delete-button').disabled = true;
}
function setSelection(shape) {
clearSelection();
selectedShape = shape;
shape.setEditable(true);
selectColor(shape.get('fillColor') || shape.get('strokeColor'));
document.getElementById('delete-button').disabled = false;
}
function deleteSelectedShape() {
if (selectedShape) {
selectedShape.setMap(null);
}
// To show:
drawingManager.setOptions({
drawingControl: true
});
document.getElementById('delete-button').disabled = true;
}
function selectColor(color) {
selectedColor = color;
for (var i = 0; i < colors.length; ++i) {
var currColor = colors[i];
colorButtons[currColor].style.border = currColor == color ? '2px solid #789' : '2px solid #fff';
}
// Retrieves the current options from the drawing manager and replaces the
// stroke or fill color as appropriate.
var polylineOptions = drawingManager.get('polylineOptions');
polylineOptions.strokeColor = color;
drawingManager.set('polylineOptions', polylineOptions);
var rectangleOptions = drawingManager.get('rectangleOptions');
rectangleOptions.fillColor = color;
drawingManager.set('rectangleOptions', rectangleOptions);
var circleOptions = drawingManager.get('circleOptions');
circleOptions.fillColor = color;
drawingManager.set('circleOptions', circleOptions);
var polygonOptions = drawingManager.get('polygonOptions');
polygonOptions.fillColor = color;
drawingManager.set('polygonOptions', polygonOptions);
}
function setSelectedShapeColor(color) {
if (selectedShape) {
if (selectedShape.type == google.maps.drawing.OverlayType.POLYLINE) {
selectedShape.set('strokeColor', color);
} else {
selectedShape.set('fillColor', color);
}
}
}
function makeColorButton(color) {
var button = document.createElement('span');
button.className = 'color-button';
button.style.backgroundColor = color;
google.maps.event.addDomListener(button, 'click', function() {
selectColor(color);
setSelectedShapeColor(color);
});
return button;
}
function buildColorPalette() {
var colorPalette = document.getElementById('color-palette');
for (var i = 0; i < colors.length; ++i) {
var currColor = colors[i];
var colorButton = makeColorButton(currColor);
colorPalette.appendChild(colorButton);
colorButtons[currColor] = colorButton;
}
selectColor(colors[0]);
}
function initialize() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: new google.maps.LatLng(12.886436490787712, 80.2685546875),
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true,
zoomControl: true
});
var polyOptions = {
strokeWeight: 0,
fillOpacity: 0.45,
editable: true
};
// Creates a drawing manager attached to the map that allows the user to draw
// markers, lines, and shapes.
drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.POLYGON,
drawingControlOptions: {
drawingModes: [
google.maps.drawing.OverlayType.POLYGON
]
},
markerOptions: {
draggable: true
},
polylineOptions: {
editable: true
},
rectangleOptions: polyOptions,
circleOptions: polyOptions,
polygonOptions: polyOptions,
map: map
});
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(event) {
if (event.type == google.maps.drawing.OverlayType.CIRCLE) {
var radius = event.overlay.getRadius();
}
if (event.type == google.maps.drawing.OverlayType.POLYGON) {
var coordinates=[];
var polygonBounds=event.overlay.getPath();
for(var i = 0 ; i < polygonBounds.length ; i++)
coordinates.push([polygonBounds.getAt(i).lat(), polygonBounds.getAt(i).lng()]);
document.getElementById("paths").value = coordinates;
//document.forms["frm_crds"].submit();
}
});
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(e) {
if (e.type != google.maps.drawing.OverlayType.MARKER) {
// Switch back to non-drawing mode after drawing a shape.
drawingManager.setDrawingMode(null);
// To hide:
drawingManager.setOptions({
drawingControl: false
});
// Add an event listener that selects the newly-drawn shape when the user
// mouses down on it.
var newShape = e.overlay;
newShape.type = e.type;
google.maps.event.addListener(newShape, 'click', function() {
setSelection(newShape);
});
google.maps.event.addListener(newShape.getPath(), 'set_at', function(event) {
var coordinates1=[];
var polygonBounds1=newShape.getPath();
for(var j = 0 ; j < polygonBounds1.length ; j++)
coordinates1.push([polygonBounds1.getAt(j).lat(), polygonBounds1.getAt(j).lng()]);
document.getElementById("paths").value = coordinates1;
});
google.maps.event.addListener(newShape.getPath(), 'insert_at', function(event) {
var coordinates1=[];
var polygonBounds1=newShape.getPath();
for(var j = 0 ; j < polygonBounds1.length ; j++)
coordinates1.push([polygonBounds1.getAt(j).lat(), polygonBounds1.getAt(j).lng()]);
document.getElementById("paths").value = coordinates1;
});
setSelection(newShape);
}
});
// Clear the current selection when the drawing mode is changed, or when the
// map is clicked.
google.maps.event.addListener(drawingManager, 'drawingmode_changed', clearSelection);
google.maps.event.addListener(map, 'click', clearSelection);
google.maps.event.addDomListener(document.getElementById('delete-button'), 'click', deleteSelectedShape);
buildColorPalette();
var coords =<?php echo json_encode($coords);?>
var newCoords = [];
var newPolys;
for (var c = 0, cc = coords.length; c < cc; c++) {
var point = coords[c];
newCoords.push(new google.maps.LatLng(point.lat, point.lng));
}
//console.log(newCoords);
newPolys = new google.maps.Polygon({
path: newCoords,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35
});
newPolys.setMap(map);
google.maps.event.addListener(newPolys , 'click', isWithinPoly);
function isWithinPoly(event){
var isWithinPolygon = google.maps.geometry.poly.containsLocation(event.latLng, this);
//console.log(isWithinPolygon);
}
addNewPolys(newPolys);
function addNewPolys(newPoly) {
drawingManager.setDrawingMode(null);
drawingManager.setOptions({
drawingControl: false
});
google.maps.event.addListener(newPoly, 'click', function(event) {
var coordinates1=[];
var polygonBounds1=this.getPath();
for(var j = 0 ; j < polygonBounds1.length ; j++)
coordinates1.push([polygonBounds1.getAt(j).lat(), polygonBounds1.getAt(j).lng()]);
document.getElementById("paths").value = coordinates1;
setSelection(newPoly);
});
google.maps.event.addListener(newPoly.getPath(), 'set_at', function(event) {
var coordinates1=[];
var polygonBounds1=newPoly.getPath();
for(var j = 0 ; j < polygonBounds1.length ; j++)
coordinates1.push([polygonBounds1.getAt(j).lat(), polygonBounds1.getAt(j).lng()]);
document.getElementById("paths").value = coordinates1;
});
google.maps.event.addListener(newPoly.getPath(), 'insert_at', function(event) {
var coordinates1=[];
var polygonBounds1=newPoly.getPath();
for(var j = 0 ; j < polygonBounds1.length ; j++)
coordinates1.push([polygonBounds1.getAt(j).lat(), polygonBounds1.getAt(j).lng()]);
document.getElementById("paths").value = coordinates1;
});
}
setMarkers(map, beaches);
}
var beaches = <?php echo json_encode($exst);?>;
//console.log(beaches);
function setMarkers(map, locations) {
var image = {
url: 'images/beachflag.png',
// This marker is 20 pixels wide by 32 pixels tall.
size: new google.maps.Size(20, 32),
// The origin for this image is 0,0.
origin: new google.maps.Point(0,0),
// The anchor for this image is the base of the flagpole at 0,32.
anchor: new google.maps.Point(0, 32)
};
// Shapes define the clickable region of the icon.
// The type defines an HTML <area> element 'poly' which
// traces out a polygon as a series of X,Y points. The final
// coordinate closes the poly by connecting to the first
// coordinate.
var shape = {
coords: [1, 1, 1, 20, 18, 20, 18 , 1],
type: 'poly'
};
var marker = null;
var infowindow = new google.maps.InfoWindow();
for (var i = 0; i < locations.length; i++) {
var beach = locations[i];
var myLatLng = new google.maps.LatLng(beach[1], beach[2]);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: beach[0],
info: "<strong>"+beach[0]+"</strong><hr>"+beach[3],
zIndex: beach[4]
});
google.maps.event.addListener(marker, 'click', function () {
infowindow.setContent(this.info);
infowindow.open(map, this);
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<script type="text/javascript" charset="UTF-8" src="http://maps.gstatic.com/maps-api-v3/api/js/21/4/common.js"></script>
<script type="text/javascript" charset="UTF-8" src="http://maps.gstatic.com/maps-api-v3/api/js/21/4/map.js"></script>
<script type="text/javascript" charset="UTF-8" src="http://maps.gstatic.com/maps-api-v3/api/js/21/4/util.js"></script>
<script type="text/javascript" charset="UTF-8" src="http://maps.gstatic.com/maps-api-v3/api/js/21/4/controls.js"></script>
<script type="text/javascript" charset="UTF-8" src="http://maps.gstatic.com/maps-api-v3/api/js/21/4/drawing_impl.js"></script>
<script type="text/javascript" charset="UTF-8" src="http://maps.gstatic.com/maps-api-v3/api/js/21/4/onion.js"></script>
<script type="text/javascript" charset="UTF-8" src="http://maps.gstatic.com/maps-api-v3/api/js/21/4/stats.js"></script>
<script type="text/javascript" charset="UTF-8" src="http://maps.gstatic.com/maps-api-v3/api/js/21/4/geometry.js"></script>
<script type="text/javascript" charset="UTF-8" src="http://maps.gstatic.com/maps-api-v3/api/js/21/4/poly.js"></script>
<form name="frm_crds" method="post">
<input id="paths" type="hidden" name="coord[]">
<div id="panel">
<div>
<div id="color-palette" style="display:none">
</div>
<button type="submit">Submit</button>
<button id="delete-button" type="button" disabled>Clear Map Draw</button>
</div>
</div>
</form>
<div id="map" style="position: relative; background-color: rgb(229, 227, 223); overflow: hidden;"></div>
在帖子中我发现原因是“js的重复”。我在脚本中找不到哪个js文件重复。
注意:如果我删除main.js,它可以在firefox中运行。它在chrome中不起作用
答案 0 :(得分:1)
删除所有 <script>...</script>
行但这一行:
<script src="http://maps.google.com/maps/api/js?sensor=false&libraries=drawing" type="text/javascript"></script>
因为它将加载所有必需的脚本文件,包括main.js
- 这是复制的来源。