我正在尝试创建HTML5 Canvas 作为 map 大小的OverlayView,将其放置在top:0; left:0;
上,在其上绘制一些内容,然后将其添加到 map 中。每本地图缩放或平移时,我都希望从 map 上删除旧 Canvas ,并在其上创建一个新的 Canvas 绘制,将其定位为0,0并将其添加到 map 中。但是,映射永远不会重新定位到top:0;左:0。有人可以帮忙吗?
function CustomLayer(map){
this.latlngs = new Array();
this.map_ = map;
this.addMarker = function(position){
this.latlngs.push(position);
}
this.drawCanvas = function(){
this.setMap(this.map_);
//google.maps.event.addListener(this.map_, 'bounds_changed',this.reDraw());
}
}
function defineOverlay() {
CustomLayer.prototype = new google.maps.OverlayView();
CustomLayer.prototype.onAdd = function() {
console.log("onAdd()");
if(this.canvas){
var panes = this.getPanes();
panes.overlayLayer.appendChild(this.canvas);
}
}
CustomLayer.prototype.remove = function() {
console.log("onRemove()");
if(this.canvas)
this.canvas.parentNode.removeChild(this.canvas);
}
CustomLayer.prototype.draw = function() {
console.log("draw()");
this.remove();
this.canvas = document.createElement("canvas");
this.canvas.setAttribute('width', '800px');
this.canvas.setAttribute('height', '480px');
this.canvas.setAttribute('top', '30px');
this.canvas.setAttribute('left', '30px');
this.canvas.setAttribute('position', 'absolute');
this.canvas.setAttribute('border', '1px solid red');
this.canvas.style.border = '1px solid red';
//using this way for some reason scale up the images and mess up the positions of the markers
/*this.canvas.style.position = 'absolute';
this.canvas.style.top = '0px';
this.canvas.style.left = '0px';
this.canvas.style.width = '800px';
this.canvas.style.height = '480px';
this.canvas.style.border = '1px solid red';*/
//get the projection from this overlay
overlayProjection = this.getProjection();
//var mapproj = this.map_.getProjection();
if(this.canvas.getContext) {
var context = this.canvas.getContext('2d');
context.clearRect(0,0,800,480);
for(i=0; i<this.latlngs.length; i++){
p = overlayProjection.fromLatLngToDivPixel(this.latlngs[i]);
//p = mapproj.fromLatLngToPoint(this.latlngs[i]);
img = new Image();
img.src = "standardtick.png";
console.log(Math.floor(p.x)+","+Math.floor(p.y));
context.drawImage(img,p.x,p.y);
}
}
this.onAdd();
console.log("canvas width:"+this.canvas.width+" canvas height: "+this.canvas.height);
console.log("canvas top:"+this.canvas.getAttribute("top")+" left: "+this.canvas.getAttribute("left"));
}
}
最佳答案
在此示例中-我认为重要的是要引起人们注意projection.fromLatLngToDivPixel和projection.fromLatLngToContainerPixel之间的区别。在这种情况下,DivPixel用于使 Canvas 的位置在 map View 上居中-而ContainerPixel用于查找要绘制到 Canvas 上的形状的位置。
接下来是一个完整的工作示例,我自己解决了这个问题。
叠加层所需的CSS属性:
.GMAPS_OVERLAY
{
border-width: 0px;
border: none;
position:absolute;
padding:0px 0px 0px 0px;
margin:0px 0px 0px 0px;
}
初始化 map 并基于Google标记创建测试
var mapsize = { width: 500, height: 500 };
var mapElement = document.getElementById("MAP");
mapElement.style.height = mapsize.width + "px";
mapElement.style.width = mapsize.width + "px";
var map = new google.maps.Map(document.getElementById("MAP"), {
mapTypeId: google.maps.MapTypeId.TERRAIN,
center: new google.maps.LatLng(0, 0),
zoom: 2
});
// Render G-Markers to Test Proper Canvas-Grid Alignment
for (var lng = -180; lng < 180; lng += 10)
{
var marker = new google.maps.Marker({
position: new google.maps.LatLng(0, lng),
map: map
});
}
定义自定义叠加层
var CanvasOverlay = function(map) {
this.canvas = document.createElement("CANVAS");
this.canvas.className = "GMAPS_OVERLAY";
this.canvas.height = mapsize.height;
this.canvas.width = mapsize.width;
this.ctx = null;
this.map = map;
this.setMap(map);
};
CanvasOverlay.prototype = new google.maps.OverlayView();
CanvasOverlay.prototype.onAdd = function() {
this.getPanes().overlayLayer.appendChild(this.canvas);
this.ctx = this.canvas.getContext("2d");
this.draw();
};
CanvasOverlay.prototype.drawLine = function(p1, p2) {
this.ctx.beginPath();
this.ctx.moveTo( p1.x, p1.y );
this.ctx.lineTo( p2.x, p2.y );
this.ctx.closePath();
this.ctx.stroke();
};
CanvasOverlay.prototype.draw = function() {
var projection = this.getProjection();
// Shift the Canvas
var centerPoint = projection.fromLatLngToDivPixel(this.map.getCenter());
this.canvas.style.left = (centerPoint.x - mapsize.width / 2) + "px";
this.canvas.style.top = (centerPoint.y - mapsize.height / 2) + "px";
// Clear the Canvas
this.ctx.clearRect(0, 0, mapsize.width, mapsize.height);
// Draw Grid with Canvas
this.ctx.strokeStyle = "#000000";
for (var lng = -180; lng < 180; lng += 10)
{
this.drawLine(
projection.fromLatLngToContainerPixel(new google.maps.LatLng(-90, lng)),
projection.fromLatLngToContainerPixel(new google.maps.LatLng( 90, lng))
);
}
};
初始化 Canvas
我发现我想添加一个额外的调用来吸引“dragend”事件-但可以对其进行测试,以查看您对自己的需求的想法。
var customMapCanvas = new CanvasOverlay(map);
google.maps.event.addListener(map, "drawend", function() {
customMapCanvas.draw();
};
如果“ Canvas 绘图”正在放慢 map 的速度
在我使用的应用程序上,我发现Map框架在 Canvas 上调用' draw '方法的频率很高,而绘制 Canvas 需要花费大约一秒钟的时间。在这种情况下,我将' draw '原型(prototype)函数定义为简单的空函数,同时将我的真实绘制函数命名为'canvasDraw'-然后为“ zoomend ”和“ dragend ”添加事件监听器。您得到的是一个仅在用户更改缩放级别后或在 map 拖动 Action 结束时才更新的 Canvas 。
CanvasOverlay.prototype.draw = function() { };
...
google.maps.event.addListener(map, "dragend", function() {
customMapCanvas.canvasDraw();
});
google.maps.event.addListener(map, "zoom_changed", function() {
customMapCanvas.canvasDraw();
});
现场演示:Complete Example - All Inline Source
关于google-maps - 相对于 map Canvas 固定的HTML Canvas 作为Google map 中的覆盖 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5960524/