【问题标题】:Add existing markers to new Polyline overlay将现有标记添加到新的折线叠加层
【发布时间】:2012-06-08 02:58:10
【问题描述】:

鉴于 Google 地图视图上的一组现有标记位置,我如何将这些点添加到折线叠加层?

我正在构建一个网络游览应用程序,用户可以在其中根据通过 AJAX 调用检索到的一组 GPS 坐标来选择停靠点。这部分工作正常,因为所有点都显示在地图上。

我的问题是我只想将标记位置添加到折线。目前,选定的点被添加到 tourList 数组中,该数组被转换为 JSON 数组并通过 jquery ajax post 调用发布。所以我知道点击事件处理程序正在为一个部分工作。

This question 几乎可以满足我的需求,但它是为 Maps API v2 设计的,而我正在使用 V3。

到目前为止我得到了什么:

//page-specific global variables
var visitPoints = new google.maps.MVCArray();
var polyLine;
var map;

window.onload= function(){
  //set initial map location
  map = new google.maps.Map(
    document.getElementById("map"), mapOptions);    

  //set up polyline capability
  var polyOptions = new google.maps.Polyline({
    path: visitPoints,
    map: map
  });

  polyLine = new google.maps.Polyline(polyOptions);
  polyLine.setMap(map); 

  //get all the GPS locations in the database  This function and makeMarkers works 
  //as designed
  var request = $.ajax({
    type:"GET",
    url: "includes/phpscripts.php?action=cords",
    dataType:"json",
    success: makeMarkers
  });

  //Populate the map view with the locations and save tour stops in array
  function makeMarkers(response){
    console.log("Response Length: "+response.length)
    for (var i=0; i< response.length; i++){
      var marker= new google.maps.Marker({
        position: new google.maps.LatLng(response[i].lat, response[i].lon),
        map: map,
        title: response[i].fileName
      });

      //anonymous function wrapper to create distinct markers
      (function(marker){
        google.maps.event.addListener(marker, 'click', function(){

          tourList.push(marker); //add marker to tour list
          visitPoints.push(marker.latlng); //add location to polyline array
          console.log("Tour List length- # stops: "+tourList.length);

        });
      })(marker);    
    }
  }

  //listener for poline click
  google.maps.event.addListener(map, 'click', updatePolyline)

} //end onload function

//updates the polyline user selections
function updatePolyline(event){
  var path = polyLine.getPath();
  path.push(event.latlng);
} //end updatePolyline

目前,我在 Firebug 中没有收到任何脚本警告,但从未调用 updatePolyline 函数。

是否需要在标记侦听器中添加侦听器来更新折线?

【问题讨论】:

    标签: google-maps-api-3 event-handling google-polyline


    【解决方案1】:

    您写了 “我只想将标记位置添加到折线” 尽管代码的设置方式是,折线每次单击标记时都会添加一个新顶点,或者是空白部分点击地图的。对于要自行更新的折线,它必须在侦听器之外。

    有两个地方需要改,

     visitPoints.push(marker.latlng);
    

    marker.getPosition()

    event.latlng 接近结尾,event.latLng(大写 L 表示 Lng)。

    我在测试时遇到了一些困难。文档描述了将 MVCArray 分配给折线路径时的自动更新;但是,折线似乎不喜欢未初始化的 MVCArray(空访问点)。当我点击标记时,自动更新没有发生。

    只有当我使用初始 LatLng 设置它时,它才能按预期工作,但我最终放弃了 visitPoints,因为不知道第一个点在哪里。我想最好的方法是在 Ajax 返回时使用 MVCArray visitPoints 初始化折线。下面的演示使用 getPath, push 而不是 visitPoints。

    DEMO http://jsfiddle.net/yV6xv/8/(点击标记或地图查看线路更新)

    初始化visitPoints 以使其余代码按预期运行,以及对marker.latlng 和event.latlng 的一些小改动。第一次单击将定义线的第一个点,随后的单击将延长线。

    一定有更好的办法

    演示 http://jsfiddle.net/yV6xv/9/

    //page-specific global variables
    var visitPoints = new google.maps.MVCArray();
    var polyLine;
    var map;
    
    // REDEFINING mapOptions and tourList
    var mapOptions = { center: new google.maps.LatLng(0.0, 0.0), zoom: 2,
            mapTypeId: google.maps.MapTypeId.ROADMAP};
    var tourList = [];
    
    window.onload= function(){
      //set initial map location
      map = new google.maps.Map(
        document.getElementById("map"), mapOptions);    
    
      //initialization hack
      visitPoints.push(new google.maps.LatLng(0,0));
    
      //set up polyline capability
      var polyOptions = new google.maps.Polyline({
        path: visitPoints,
        map: map
      });
    
      //complete initialization hack
      visitPoints.pop();
    
      polyLine = new google.maps.Polyline(polyOptions);
      polyLine.setMap(map); 
    
      //get all the GPS locations in the database  This function and makeMarkers works 
    
    /* TEMPORARY COMMENTING OUT
      //as designed
      var request = $.ajax({
        type:"GET",
        url: "includes/phpscripts.php?action=cords",
        dataType:"json",
        success: makeMarkers
      });
    */
    
    makeMarkers([{lat:0, lon:0},{lat:10, lon:10},{lat:15, lon:20},{lat:20, lon:30}]);
    
    
      //Populate the map view with the locations and save tour stops in array
      function makeMarkers(response){
        console.log("Response Length: "+response.length)
        for (var i=0; i< response.length; i++){
          var marker= new google.maps.Marker({
            position: new google.maps.LatLng(response[i].lat, response[i].lon),
            map: map,
            title: response[i].fileName
          });
    
          //anonymous function wrapper to create distinct markers
          (function(marker){
            google.maps.event.addListener(marker, 'click', function(){
    
              tourList.push(marker); //add marker to tour list
              visitPoints.push(marker.getPosition()); //add location to polyline array
              console.log("Tour List length- # stops: "+tourList.length);
    
            });
          })(marker);    
        }
      }
    
      //listener for poline click
      google.maps.event.addListener(map, 'click', updatePolyline)
    
    } //end onload function
    
    //updates the polyline user selections
    function updatePolyline(event){
      var path = polyLine.getPath();
      path.push(event.latLng);
    } //end updatePolyline
    

    【讨论】:

    • 这是解决方案,感谢您的帮助!为什么折线点不需要 MVCArray 呢?从给出的示例中,需要有一些地方来存储行中的位置。
    • 我总是使用一个普通的 LatLngs 数组来存储线和多边形的坐标,这是我第一次使用 MVCArray。我想我并不是所有可用工具都跟上进度 :( 我确实建议使用 MVCArray,因为更新线路所需的编码更少。因为我缺乏经验,所以我得到线路的唯一方法是使用MVCArray 是让它在分配给折线之前至少包含一个 LatLng。也许有一个很好的例子来说明它应该如何正确初始化。
    • 我不太高兴这样离开 MVCArray,所以我想出了一个技巧。 (上面更新的答案中的详细信息)。事实证明,您可以使用任意点初始化折线,然后弹出(删除)该点,使其充当空的,并与您的其余代码一起使用。我认为这是允许的,因为我在空折线上调用 getPath()。我怀疑在内部 MVCArray 会跟踪内容类型是什么,但是在调用 new MVCArray 时它什么都不知道。这是我最好的猜测。
    • 此解决方案有效,但一个问题是任何点击事件都会导致折线延伸到点击点。因此,我从响应对象创建了一个点列表,并将点击位置与 updatePolyline 函数中的现有标记进行了比较。这样,只有有效的标记位置会添加到折线路径中。
    • 知道了,我不知道该怎么做,并打开了选项。我知道地图监听器是为了扩展折线。
    猜你喜欢
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多