很难移动所有其他坐标。你为什么不制作可编辑的多边形。让用户自己在谷歌地图上添加/编辑多边形。我正在分享我的代码,我在其中创建了一堆多边形,用户也可以对其进行编辑
<pre>
var COLORS = [["red", "#ff0000"], ["orange", "#ff8800"], ["green","#008000"], ["blue", "#000080"], ["purple", "#800080"]];
var options = {};
var lineCounter_ = 0;
var shapeCounter_ = 0;
var markerCounter_ = 0;
var colorIndex_ = 0;
var featureTable_;
var map;
//Active selective controller
function select(buttonId)
{
document.getElementById("hand_b").className="unselected";
document.getElementById("shape_b").className="unselected";
//document.getElementById("line_b").className="unselected";
//document.getElementById("placemark_b").className="unselected";
document.getElementById(buttonId).className="selected";
}
//Active tab after editing completion
function stopEditing()
{
select("hand_b");
}
function getColor(named)
{
return COLORS[(colorIndex_++) % COLORS.length][named ? 0 : 1];
}
function getIcon(color)
{
var icon = new GIcon();
icon.image = "http://google.com/mapfiles/ms/micons/" + color + ".png";
icon.iconSize = new GSize(32, 32);
icon.iconAnchor = new GPoint(15, 32);
return icon;
}
function startShape()
{
select("shape_b");
var color = getColor(false);
var polygon = new GPolygon([], color, 2, 0.7, color, 0.2);
startDrawing(polygon, "Shape " + (++shapeCounter_), function()
{
var cell = this;
var area = polygon.getArea();
cell.innerHTML = (Math.round(area / 10000) / 100) + "km<sup>2</sup>";
}, color);
}
function addFeatureEntry(name, color)
{
currentRow_ = document.createElement("tr");
var colorCell = document.createElement("td");
currentRow_.appendChild(colorCell);
colorCell.style.backgroundColor = color;
colorCell.style.width = "1em";
var nameCell = document.createElement("td");
currentRow_.appendChild(nameCell);
nameCell.innerHTML = name;
var descriptionCell = document.createElement("td");
currentRow_.appendChild(descriptionCell);
featureTable_.appendChild(currentRow_);
return {desc: descriptionCell, color: colorCell};
}
function startDrawing(poly, name, onUpdate, color)
{
map.addOverlay(poly);
poly.enableDrawing(options);
poly.enableEditing({onEvent: "mouseover"});
poly.disableEditing({onEvent: "mouseout"});
GEvent.addListener(poly, "endline", function()
{
select("hand_b");
//SANI: getting all vertix of polygon
var str = '{"zones":[';
for (var i = 0, I = poly.getVertexCount(); i < I; ++i)
{
var xy = poly.getVertex(i);
str += '{"lat":"'+xy.lat() + '", "long":"' + xy.lng() + '"}';
if(i != Number(I-1))
str += ',';
}
str += ']}';
//SANI: getting polygon name
var n = prompt("Enter polygon name:", "");
frmFeilds = "latlng="+str+"&name="+n;
//SANI: storing lat,long and name into database
$(document).ready(
function()
{
$.ajax({
url: 'save_polygon.php',
type:'POST',
data:'action=yes&'+frmFeilds,
success: function(sani)
{
featureTable_.innerHTML = sani;
}
});
}
);
//select("hand_b");
// var cells = addFeatureEntry(name, color);
GEvent.bind(poly, "lineupdated", cells.desc, onUpdate);
GEvent.addListener(poly, "click", function(latlng, index)
{
if (typeof index == "number")
{
poly.deleteVertex(index);
} else {
var newColor = getColor(false);
cells.color.style.backgroundColor = newColor
poly.setStrokeStyle({color: newColor, weight: 4});
}
});
});
}
function initialize()
{
if (GBrowserIsCompatible())
{
map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(25.18622, 55.23866), 11);
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.clearOverlays();
featureTable_ = document.getElementById("featuretbody");
select("hand_b");
/* S@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@NI */
<?php
$zoneQry = mysql_query("SELECT * FROM tbl_zones where zon_status = '1'");
if(mysql_num_rows($zoneQry) > 0)
{
while($zoneRec = mysql_fetch_assoc($zoneQry))
{
$json = (string)$zoneRec['zon_vertix'];
$obj = json_decode($json, true);
?>
var polygon<?php echo $zoneRec['zon_id']; ?> = new GPolygon([
<?php
foreach($obj['zones'] as $zones)
{
?>
new GLatLng(<?php echo $zones['lat']; ?>, <?php echo $zones['long']; ?>),
<?php } ?>
], "#f06ca8", 2, 1, "#89cff1", 0.5);
drawShapBySani(polygon<?php echo $zoneRec['zon_id']; ?>);
updatezones(<?php echo $zoneRec['zon_id']; ?>, polygon<?php echo $zoneRec['zon_id']; ?>);
<?php
}
}
?>
/* S@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@NI */
}
}
/* S@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ SANI: Update all Zones @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@NI */
function updatezones(zonid, zoneObj)
{
//Update zones on click
$("#updatezone").click(function() {
//alert(zonid+', '+zoneObj);
var str = '{"zones":[';
for(var i = 0, I = zoneObj.getVertexCount(); i < I; ++i)
{
var xy = zoneObj.getVertex(i);
str += '{"lat":"'+xy.lat() + '", "long":"' + xy.lng() + '"}';
if(i != Number(I-1))
str += ',';
}
str += ']}';
//SANI: getting polygon name
frmFeilds = "latlng="+str+"&id="+zonid;
//SANI: storing lat,long and name into database
$(document).ready(
function()
{
$.ajax({
url: 'save_polygon.php',
type:'POST',
data:'action=yes&'+frmFeilds,
success: function(sani)
{
featureTable_.innerHTML = sani;
}
});
}
);
});
}
/* S@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ SANI: Draw Editable Shap @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@NI */
function drawShapBySani(polygon)
{
map.addOverlay(polygon);
startDrawing(polygon, 'sani', function() {
var cell = this;
var area = polygon.getArea();
cell.innerHTML = (Math.round(area / 10000) / 100) + "km<sup>2</sup>";
}, '#ff0000');
select("hand_b");
polygon.disableEditing();
}
</pre>
希望它对你有用。
谢谢