我正在尝试为谷歌地图脚本创建一个包装对象,但由于某种原因它似乎不起作用 - 有人可以看看下面的代码并告诉我我做错了什么吗?
var cmdMapObject = {
canvas : 'map-canvas',
latLng : '-25.344, 131.036',
zoom : 14,
myLocation : '',
poly : '',
map : '',
markers : [],
path : '',
addPoint : function(event) {
"use strict";
// add the latitude and longitute to array after the last item
cmdMapObject.path.insertAt(cmdMapObject.path.length, event.latLng);
// create new marker
var marker = new google.maps.Marker({
position: event.latLng,
map: cmdMapObject.map,
draggable: true
});
// add new marker to the array
cmdMapObject.markers.push(marker);
// set tooltip title to the marker
marker.setTitle("#" + cmdMapObject.path.length);
// remove marker on click
google.maps.event.addListener(marker, 'click', function() {
// remove marker from the map
marker.setMap(null);
// remove location from array and path
for (var i = 0, ml = cmdMapObject.markers.length; i < ml && cmdMapObject.markers[i] != marker; ++i) {
cmdMapObject.markers.splice(i, 1);
cmdMapObject.path.removeAt(i);
}
}
);
// add listener to draggend event to change the position of the marker
// when dragging has ended
google.maps.event.addListener(marker, 'dragend', function() {
for (var i = 0, ml = cmdMapObject.markers.length; i < ml && cmdMapObject.markers[i] != marker; ++i) {
var thisNewPosition = marker.getPosition();
cmdMapObject.markers[i]['position'] = thisNewPosition;
cmdMapObject.path.setAt(i, thisNewPosition);
}
});
},
initialize : function() {
"use strict";
cmdMapObject.path = new google.maps.MVCArray;
cmdMapObject.myLocation = new google.maps.LatLng(cmdMapObject.latLng);
cmdMapObject.map = new google.maps.Map(document.getElementById(cmdMapObject.canvas), {
zoom: cmdMapObject.zoom,
center: cmdMapObject.myLocation,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
cmdMapObject.poly = new google.maps.Polygon({
strokeWeight: 3,
fillColor: '#5555FF'
});
cmdMapObject.poly.setMap(cmdMapObject.map);
cmdMapObject.poly.setPaths(new google.maps.MVCArray(cmdMapObject.path));
google.maps.event.addListener(cmdMapObject.map, 'click', cmdMapObject.addPoint);
},
mapInit : function() {
"use strict";
google.maps.event.addDomListener(window, 'load', cmdMapObject.initialize);
}
};
cmdMapObject.mapInit();
我得到的是左侧的缩放滑块、地图/卫星按钮和底部的版权声明,但没有地图。