javascript - Google.maps.api、JavaScript;同时移动多个多边形的顶点

标签 javascript google-maps polygon

我有一堆多边形 (google.maps.Polygon),已导入到 map 中。它们边对边放置并共享顶点,因此之间没有“空白区域”(您可以想象国家边界或邮政编码区域),所以它有点像网格。

现在,我希望我的用户能够编辑此网格: 当将多边形的顶点从位置 (x1, y1) 拍摄到 (x2, y2) 时,同一位置 (x1, y1) 上的(其他多边形的)所有其他顶点应移动到新位置 (x2, y2)。

关于如何实现这一目标有什么想法吗?

这是我尝试过的: 我可以使多边形editable: true,这将允许用户拖动顶点,但并非所有多边形顶点都会跟随。

我尝试为多边形添加一个“set_at”监听器,一旦移动了一个顶点,循环就会抛出其他多边形并检查它们是否有一个与起始坐标相同的顶点,如果是,则将该坐标移动到新位置。问题是我不知道如何获取移动坐标的起始坐标,只知道结束坐标。

有没有比使用多边形(例如网格)更好的方法来解决这个问题?

非常感谢您的帮助!

最佳答案

很难移动所有其他坐标。为什么不制作可编辑的多边形。让用户自己在谷歌地图上添加/编辑多边形。我正在分享我的代码,其中我创建了一堆多边形,用户也可以对其进行编辑

<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>

希望它对您有用。

谢谢

关于javascript - Google.maps.api、JavaScript;同时移动多个多边形的顶点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16712285/

相关文章:

javascript - 在javascript中访问函数参数的最佳方法是什么?

javascript - 引导问题

android - 如何在android中的mapview中获取四个角的long&lat值?

android - IllegalArgumentException : No view found for id , 从另一个 fragment 返回并调用 onResume 时

c++ - 如何将单调多边形分解为两个单调链

r - R中点要素到最近多边形的距离

javascript - react VX 图表 : React does not recognize the `xScale` prop on a DOM element

android - 谷歌地图标记被缩放时的边界矩形所取代

python - 检查地理点是在多边形内部还是外部

javascript - 重置经纬度不会创建 map