Javascript 编辑原始多边形后获取新坐标
我正在尝试找出如何获得已编辑多边形的新坐标。因此,首先在地图上绘制多边形,该多边形的坐标显示在文本区域“Javascript 编辑原始多边形后获取新坐标,javascript,html,google-maps-api-3,Javascript,Html,Google Maps Api 3,我正在尝试找出如何获得已编辑多边形的新坐标。因此,首先在地图上绘制多边形,该多边形的坐标显示在文本区域“coordinaren”中。坐标被处理为JSON字符串。 您需要将侦听器绑定到多边形路径,而不是多边形本身 函数初始化(){ 变量映射选项={ 缩放:4, 中心:新google.maps.LatLng(40,9), mapTypeId:google.maps.mapTypeId.ROADMAP }; var map=new google.maps.map(document.getElemen
coordinaren
”中。坐标被处理为JSON字符串。
您需要将侦听器绑定到多边形路径,而不是多边形本身
函数初始化(){
变量映射选项={
缩放:4,
中心:新google.maps.LatLng(40,9),
mapTypeId:google.maps.mapTypeId.ROADMAP
};
var map=new google.maps.map(document.getElementById(“地图画布”),mapOptions);
var polygon=新建google.maps.polygon({
是的,
笔划不透明度:0,
冲程重量:0,
填充颜色:'#00FF00',
不透明度:.6,
路径:[
新的google.maps.LatLng(39,4),
新的google.maps.LatLng(34,24),
新的google.maps.LatLng(43,24),
新的google.maps.LatLng(39,4)],
地图:地图
});
//从多边形中获取路径,并分别为每个路径设置事件侦听器
polygon.getPaths().forEach(函数(路径,索引){
google.maps.event.addListener(路径'insert_at',函数(){
console.log('insert_at event');
});
google.maps.event.addListener(路径'remove_at',函数(){
console.log('remove_at event');
});
google.maps.event.addListener(路径'set_at',函数(){
console.log('set_at event');
});
});
}
#地图画布{
高度:200px;
}
还有我的新功能,我现在是如何让它工作的 仅供参考:我现在不使用删除或拖动选项,这就是为什么我在评论中同时使用这两个选项
function initialize()
{
//fill array with coordinates
var path = [[51.14920179999362,3.706512451171875],[50.99042122689005,3.475799560546875],[50.93852713736125,3.73809814453125]];
//Options for the map
var mapOptions = {
zoom: 10,
center: new google.maps.LatLng(51.0108706, 3.7264613),
}
//generate map
var map = new google.maps.Map(document.getElementById('map'), mapOptions);
//translate array of arrays to LatLng
var fixedPath = [];
for (var i=0; i < path.length; i++){
fixedPath.push({lat:path[i][0],lng:path[i][1]});
}
//options for the polygon
var polyOptions = {
paths: fixedPath,
strokeColor: '#FF0000',
strokeWeight: 2,
strokeOpacity: 0.8,
fillColor: '#FF0000',
fillOpacity: 0.35,
editable: true, //editeren van de polygon
draggable: false //verplaatsen van de polygon
};
//create the polygon
var polygon = new google.maps.Polygon(polyOptions);
polygon.setMap(map);
//google.maps.event.addListener(polygon, "dragend", getPolygonCoords);
google.maps.event.addListener(polygon.getPath(), "insert_at", getPolygonCoords);
//google.maps.event.addListener(polygon.getPath(), "remove_at", getPolygonCoords);
google.maps.event.addListener(polygon.getPath(), "set_at", getPolygonCoords);
function getPolygonCoords() {
var coordinates_poly = polygon.getPath().getArray();
var newCoordinates_poly = [];
for (var i = 0; i < coordinates_poly.length; i++){
lat_poly = coordinates_poly[i].lat();
lng_poly = coordinates_poly[i].lng();
latlng_poly = [lat_poly, lng_poly];
newCoordinates_poly.push(latlng_poly);
}
var str_coordinates_poly = JSON.stringify(newCoordinates_poly);
document.getElementById('new_coordinaten').value = str_coordinates_poly;
}
}
google.maps.event.addDomListener(window, 'load', initialize);
函数初始化()
{
//用坐标填充数组
var路径=[[51.1490179999362,3.706512451171875],[50.9904122689005,3.475799560546875],[50.93852713736125,3.73809814453125];
//地图选项
变量映射选项={
缩放:10,
中心:新google.maps.LatLng(51.0108706,3.7264613),
}
//生成地图
var map=new google.maps.map(document.getElementById('map'),mapOptions);
//将阵列的阵列转换为LatLng
var fixedPath=[];
对于(变量i=0;i
我只想让set\u at做点什么,remove\u at不会被使用,至少现在不会,而insert\u at我想禁用。您是否知道如何隐藏/删除触发insert_at listener的多边形上中间不太可见的点?
function initialize()
{
//fill array with coordinates
var path = [[51.14920179999362,3.706512451171875],[50.99042122689005,3.475799560546875],[50.93852713736125,3.73809814453125]];
//Options for the map
var mapOptions = {
zoom: 10,
center: new google.maps.LatLng(51.0108706, 3.7264613),
}
//generate map
var map = new google.maps.Map(document.getElementById('map'), mapOptions);
//translate array of arrays to LatLng
var fixedPath = [];
for (var i=0; i < path.length; i++){
fixedPath.push({lat:path[i][0],lng:path[i][1]});
}
//options for the polygon
var polyOptions = {
paths: fixedPath,
strokeColor: '#FF0000',
strokeWeight: 2,
strokeOpacity: 0.8,
fillColor: '#FF0000',
fillOpacity: 0.35,
editable: true, //editeren van de polygon
draggable: false //verplaatsen van de polygon
};
//create the polygon
var polygon = new google.maps.Polygon(polyOptions);
polygon.setMap(map);
//google.maps.event.addListener(polygon, "dragend", getPolygonCoords);
google.maps.event.addListener(polygon.getPath(), "insert_at", getPolygonCoords);
//google.maps.event.addListener(polygon.getPath(), "remove_at", getPolygonCoords);
google.maps.event.addListener(polygon.getPath(), "set_at", getPolygonCoords);
function getPolygonCoords() {
var coordinates_poly = polygon.getPath().getArray();
var newCoordinates_poly = [];
for (var i = 0; i < coordinates_poly.length; i++){
lat_poly = coordinates_poly[i].lat();
lng_poly = coordinates_poly[i].lng();
latlng_poly = [lat_poly, lng_poly];
newCoordinates_poly.push(latlng_poly);
}
var str_coordinates_poly = JSON.stringify(newCoordinates_poly);
document.getElementById('new_coordinaten').value = str_coordinates_poly;
}
}
google.maps.event.addDomListener(window, 'load', initialize);