-2

我需要在每个缩放级别上单击 Google 地图(阿姆斯特丹,纬度 52,lng 4)的任何地方绘制一个 100x100 屏幕像素的方形多边形,并在多边形的中心使用 e.latlng。我尝试使用 fromLatLngToPoint、fromPointToLatLng、比例和 worldCoordinates 来计算它,但我无法绘制多边形。如果有人喜欢这个谜题,我会非常感谢这个解决方案。(我想以此作为一个简单的开始,将多边形编辑为更复杂的形状,而不是使用 DrawingManager)

我试过了:

google.maps.event.addListener(map, 'click', function(e) {
var scale = Math.pow(2, map.getZoom());
var nw = new google.maps.LatLng(map.getBounds().getNorthEast().lat(),map.getBounds().getSouthWest().lng());
var worldCoordinateNW = map.getProjection().fromLatLngToPoint(nw);
var worldCoordinate = map.getProjection().fromLatLngToPoint(e.latLng);
var deX = Math.floor((worldCoordinate.x - worldCoordinateNW.x) * scale);
var deY = Math.floor((worldCoordinate.y - worldCoordinateNW.y) * scale);

// so far so good, deX and deY give the centerpixel

var deNW = map.getProjection().fromPointToLatLng(new google.maps.Point(deX-50,deY-50));         
var deNO = map.getProjection().fromPointToLatLng(new google.maps.Point(deX+50,deY-50));
var deZO = map.getProjection().fromPointToLatLng(new google.maps.Point(deX+50,deY+50));
var deZW = map.getProjection().fromPointToLatLng(new google.maps.Point(deX-50,deY+50));

var dePathArray = [deNW, deNO, deZO, deZW]; 
deObjectNew = new google.maps.Polygon({
    paths: dePathArray,                         
    strokeColor: '#000000',
    strokeWeight: 1,
    fillColor: "#FF0000",
    fillOpacity: 0.3,
}); 
deObjectNew.setMap(map);

});

4

1 回答 1

0

知道了:

        var deNW = deKaart.getProjection().fromPointToLatLng(new google.maps.Point((deX-50)/scale+worldCoordinateNW.x,(deY-50)/scale+worldCoordinateNW.y));         
        var deNO = deKaart.getProjection().fromPointToLatLng(new google.maps.Point((deX+50)/scale+worldCoordinateNW.x,(deY-50)/scale+worldCoordinateNW.y));
        var deZO = deKaart.getProjection().fromPointToLatLng(new google.maps.Point((deX+50)/scale+worldCoordinateNW.x,(deY+50)/scale+worldCoordinateNW.y));
        var deZW = deKaart.getProjection().fromPointToLatLng(new google.maps.Point((deX-50)/scale+worldCoordinateNW.x,(deY+50)/scale+worldCoordinateNW.y));
于 2013-02-16T15:24:48.670 回答