从谷歌地图折线获取坐标

6
这是我的代码 http://jsfiddle.net/6KacA/ 一切正常,我可以在地图上绘图,但是我想获取我在地图上绘制的坐标。在API v3中,我该如何做呢?
附注:对于我的英语表示抱歉。
3个回答

12

绘图管理器(drawingManager)有一个polylinecomplete事件,观察它以访问折线(polyline),它将是提供给回调函数的第一个参数。

然后使用polyline.getPath()方法访问路径,并使用MVCArray方法操作它。

示例:

google.maps.event.addListener(drawingManager, 'polylinecomplete', function(line) {
  alert(line.getPath().getArray().toString());
});

我如何通过 Node.js 后端实现这个功能?我有一堆相隔 300 米的坐标,我想知道它们是否在折线中。 - nr5

6
您可以使用以下函数获取所有折线的坐标。
function getPathVariableCode(line){
var codeStr = '  var linePath = [\n';
var pathArr = line.getPath();
for (var i = 0; i < pathArr.length; i++){
    codeStr += '    {lat: ' + pathArr.getAt(i).lat() + ', lng: ' + pathArr.getAt(i).lng() + '}' ;
    if (i !== pathArr.length-1) {
        codeStr += ',\n';
    };

};

codeStr += '\n  ];';

//the coordinates path it´s print on the console of the browser

console.log (codeStr);
console.log(pathArr.length);

};

这是如何调用该函数的方法。
 line.addListener('click', function () {
 getPathVariableCode(line);
 });

然后,您只需单击某个点以在浏览器控制台上生成坐标。

------------ HERE IS THE COMPLETE CODE ---------

var map;
function initialize() {

  

    //Map options
   var mapOptions = {
 zoom: 7,
    center: new google.maps.LatLng(18.075464, -94.012622),
    mapTypeId: google.maps.MapTypeId.TERRAIN,
    scaleControl: false,
    mapTypeControl: false,
    zoomControl: false,
    draggable: true,
    disableDoubleClickZoom: true,
    keyboardShortcuts: false,
 
  }
    //map canvas
  map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions );
   //coordinates por the polyline
  var linePath = [
    {lat: 18.068652, lng: -94.25055299999997},
    {lat: 16.766951, lng: -93.31531000000001}
  ];
    //Polyline Options
  var line = new google.maps.Polyline({
    path: linePath,
    geodesic: true,
    strokeColor: '#ff0000',
    strokeOpacity: 0.4,
    strokeWeight: 8,
 editable: true // if you dont want to see the editable point change it to false
  });

     //call to the path coordinates function

  line.addListener('click', function () {
     getPathVariableCode(line);
 });
    //set map
 line.setMap(map);
 };

//here we call the initialize function which load the map
google.maps.event.addDomListener(window, 'load', initialize);


//function to get all the coordinates of the polyline
function getPathVariableCode(line){
 var codeStr = '  var linePath = [\n';
 var pathArr = line.getPath();
 for (var i = 0; i < pathArr.length; i++){
  codeStr += '    {lat: ' + pathArr.getAt(i).lat() + ', lng: ' + pathArr.getAt(i).lng() + '}' ;
        if (i !== pathArr.length-1) {
   codeStr += ',\n';
  };
       
 };
    
 codeStr += '\n  ];';
    
    //the coordinates path it´s print on the console of the browser

    console.log (codeStr);
    console.log(pathArr.length);
 
};



  
#map_canvas {
    width: 90%;
    height: 300px;
    margin: 0 auto;
    border: 1px solid grey;
    border-radius: 5px;
    box-shadow: 0px 0px 8px #999;
    color: black;
    text-align: center;
}
<!DOCTYPE html>
<html lang="en">

<head>
 



</head>

<body>
    <div class="container">
        <!-- Push Wrapper -->
        <div class="mp-pusher" id="mp-pusher">


           
            <!-- /scroller-inner -->

            <div id="map_canvas"></div>

        </div>

        <!-- /pusher -->
    </div>
    <!-- /container -->

    <script type="text/javascript"
      src="http://maps.google.com/maps/api/js?sensor=false&libraries=drawing"></script>
    

</body>

</html>


0

试试这个

    myfile.csv:

    -6.9906775;38.0667982
    -6.9906474;38.0666109
    -6.9904324;38.0663124
    ...

    var coordsList= [];
    $.ajax({
            type: "GET",
            url: "myfile.csv",
            dataType: "text",
            success: function(data) {                    
                var allTextLines = data.split(/\r\n|\n/);
                allTextLines.forEach(function(element) {
                    var entry = element.split(';');
                    coordsList.push({lat: entry[0]*1, lng: entry[1]*1});
                });
                console.log(coordsList);
            }
        });

    var myArea = new google.maps.Polygon({paths: coordsList});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接