gpt4 book ai didi

google-maps - HTML Canvas 作为 Google Maps 中的 Overlayview,相对于 Map Canvas 是固定的

转载 作者:行者123 更新时间:2023-12-04 08:29:19 26 4
gpt4 key购买 nike

我正在尝试创建一个 HTML5 Canvas 作为 map 大小的 OverlayView,将其定位到 top:0; left:0; ,在上面画一些东西,然后将其添加到 map 中。每本地图缩放或平移时,我想从 map 中删除旧 Canvas 并在其上创建一个新 Canvas ,将其定位到 0,0 并将其添加到 map 中。但是 map 永远不会重新定位到顶部: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 Framework 调用了“ ”。画 ' 方法经常在 Canvas 上绘制一些需要一秒钟左右才能完成的东西。在这种情况下,我定义了 ' ' 原型(prototype)函数只是一个空函数,而将我的真实绘图函数命名为 'canvasDraw' - 然后为“ zoomend ”和“ dragend ”添加事件监听器。你在这里得到的是一个只有在用户更改缩放级别或 map 拖动操作结束时才会更新的 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 - HTML Canvas 作为 Google Maps 中的 Overlayview,相对于 Map Canvas 是固定的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5960524/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com