0

如何收集我们正在通过谷歌绘图 API 绘制的多边形形状的每个边缘的纬度和经度。请帮我把我放在收集多边形边缘的完美路径上。

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title> Region Drawing System</title>
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=drawing,geometry&sensor=false"></script>
    <script>
    var infoWindow;
    var contentString;
    function initialize() {
        var mapOptions = {
            center : new google.maps.LatLng(42.397, -100.000),
            zoom: 4,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

        var drawingManager = new google.maps.drawing.DrawingManager({
            drawingMode: google.maps.drawing.OverlayType.POLYGON,
            drawingControl: true,
            drawingControlOptions: {
                position: google.maps.ControlPosition.TOP_CENTER,
                drawingModes: [
                    google.maps.drawing.OverlayType.POLYGON
            ]
        },
            polygonOptions :{
                editable: true,
                draggable: true
        }
    });
     drawingManager.setMap(map);
     google.maps.event.addListener(drawingManager, 'click', showNewPath); // Passing drawingManager object is a good idea to collect edges of Polygon ?
     infoWindow =   new google.maps.InfoWindow();
    }
    function showNewPath() {
            var contentString = " " //How to collect the edges of Polygon ?
            infoWindow.setContent(contentString);
            infoWindow.setPosition();  // How to specify the position for each edge ?
            infowindow.open(map);
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
    <center> 
        <h1> Region Drawing System</h1>
        <div id="map-canvas" style="height:400px ; width:800px"></div>
    <hr/>
        <div id="panel">
            <input id="address" type="textbox" value="">
            <input type="button" value="Look UP" >
        </div>
    </center>
  </body>
</html>
4

1 回答 1

0

By The following code. Anyone can find the Lattitude and Longitude of the Polygon which we have drawn.

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title> Region Drawing System</title>
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=drawing,geometry&sensor=false"></script>
    <script>
    var polygonShape = [];
    var infoWindow = new google.maps.InfoWindow({maxWidth : 450});
    var contentString;
    function initialize() {
        var mapOptions = {
            center : new google.maps.LatLng(42.397, -100.000),
            zoom: 4,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

        var drawingManager = new google.maps.drawing.DrawingManager({
            drawingMode: google.maps.drawing.OverlayType.POLYGON,
            drawingControl: true,
            drawingControlOptions: {
                position: google.maps.ControlPosition.TOP_CENTER,
                drawingModes: [
                    google.maps.drawing.OverlayType.POLYGON
            ]
        },
            polygonOptions :{
                editable: true,
                draggable: true
        }
    });
     drawingManager.setMap(map);
     google.maps.event.addListener(drawingManager, 'polygoncomplete', function (e) {
          var vert = e.getPath(); //In the method we could get the vertices of the polygon which we have drawn.
          var clat = 0; // To display the infowindow.
      var clng = 0; // To display the infowindow.
      var contents = '</br><label><b>Region Name : </b></label><input type="text" size="20" id="region_name"/></br><label align="top"><b>Description : </b></label><textarea id="region_desc" cols="20" rows="3"></textarea><br/><input type="button" value="Save Region" onclick="getPointInfo(polygonShape);"/><br/>';
      vert.forEach(function(xy, i) {
          clat = xy.lat();
          clng = xy.lng();
          });
          infoWindow.setContent(contents);
          infoWindow.setPosition(new google.maps.LatLng(clat,clng));
          infoWindow.open(map);
    });});
     infoWindow =   new google.maps.InfoWindow();
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
    <center> 
        <h1> Region Drawing System</h1>
        <div id="map-canvas" style="height:400px ; width:800px"></div>
    <hr/>
    </center>
  </body>
</html>

Have fun with the above code.

于 2014-03-27T11:57:45.040 回答