google maps api 3 - Draw polygon of 100 pixels on mouseclick -
i need draw square polygon of 100x100 screenpixels whereever click on google map (amsterdam, lat 52, lng 4), on every zoomlevel, e.latlng @ center of polygon. tried figure out using fromlatlngtopoint, frompointtolatlng, scale , worldcoordinates, can't polygon drawn. if likes puzzle appreciate solution much. (i want utilize simple start edit polygon more complex shape, not using drawingmanager)
i tried:
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); // far good, dex , dey give 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);
});
got it:
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));
google-maps-api-3
No comments:
Post a Comment