2012-06-01 95 views
1

我有这个功能,点击标记后,在与某个项目相关的点之间绘制一条线。Google Maps API V3 - 管理多段线

function showDetails(itemId) 
{ 
    var newlatlng = itemId.position; 
    var xmlhttp; 

    if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp=new XMLHttpRequest(); 
    } 
    else 
    {// code for IE6, IE5 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 

    xmlhttp.open("GET", "index.php?r=items/ajaxdetails&id="+itemId.indice, 
       false); 
    xmlhttp.send(); 




    var checkins = JSON.parse(xmlhttp.responseText); 
    var numeroCheckins = checkins.length; 

    var polylineCheckins = []; 

    var bounds = new google.maps.LatLngBounds(); 

    for (counter = 0; counter< numeroCheckins; counter++) 
    { 
     var posizione = new google.maps.LatLng(checkins[counter].lat, 
               checkins[counter].long); 
     polylineCheckins[counter] = posizione; 
     bounds.extend(posizione); 
    } 

    var polyline = new google.maps.Polyline({ 
     path: polylineCheckins, 
     strokeColor: "#FF0000", 
     strokeOpacity: 0.5, 
     strokeWeight: 5 
     }); 

    polyline.setMap(map); 

    map.fitBounds(bounds); 
} 

一切正常,但如果多次调用此函数,则始终显示上一行。我试图使用方法setMap(null)但没有成功,试图重置折线。

我想在绘制一个新的之前实现删除以前的折线的结果。

感谢您的支持

回答

2

最简单的方法,只保留一个折线showDetails在地图上是做一个全球性的折线变量。那样,每调用一次showDetails就会调用全局变量。

现在,每次showDetails运行一个新的Polyline被创建并且没有对它的引用被返回,所以我没有看到一种方法来将上一行的映射设置为null。

// GLOBAL 
    var detailsPolyline = new google.maps.Polyline({ 
    strokeColor: "#FF0000", 
    strokeOpacity: 0.5, 
    strokeWeight: 5 
    }); 

showDetails

detailsPolyline.setPath(polylineCheckins); 
detailsPolyline.setMap(map); 

map.fitBounds(bounds); 

下面是我用了整个测试的情况下,因为我没有我创建了自己的对象

var map; 
    var mapOptions = { center: new google.maps.LatLng(0.0, 0.0), zoom: 2, 
    mapTypeId: google.maps.MapTypeId.ROADMAP }; 

    function initialize() { 
    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); 
    showDetails([ {lat: 20, long: 0}, 
        {lat: 20, long: 10}, 
        {lat: 30, long: 20}]); 

    showDetails([ {lat: 10, long: 0}, 
        {lat: 10, long: 10}, 
        {lat: 20, long: 20}]); 
    } 

    var detailsPolyline = new google.maps.Polyline({ 
    strokeColor: "#FF0000", 
    strokeOpacity: 0.5, 
    strokeWeight: 5 
    }); 

function showDetails(checkins) 
{ 
    var numeroCheckins = checkins.length; 
    var polylineCheckins = []; 
    var bounds = new google.maps.LatLngBounds(); 

    for (counter = 0; counter< numeroCheckins; counter++) 
     { 
     var posizione = new google.maps.LatLng(checkins[counter].lat, checkins[counter].long); 
     polylineCheckins[counter] = posizione; 
     bounds.extend(posizione); 
     } 

    detailsPolyline.setPath(polylineCheckins); 
    detailsPolyline.setMap(map); 

    map.fitBounds(bounds); 
} 
+0

感谢您的支持! – maxdangelo

1

你定义的函数内部的变量polyline,所以一旦函数结束,变量超出范围的任何其他方法(如setMap(null))。

有几种方法可以做到这一点。该simplist方法是定义函数外折线作为一个全局变量:

var polyline = null; 

function showDetails(itemId) 
{ 
    if (polyline != null) 
    { 
    polyline.setMap(null); 
    polyline = null; 
    } 

    /* more code */ 

    polyline = new google.maps.Polyline({ 
    path: polylineCheckins, 
    strokeColor: "#FF0000", 
    strokeOpacity: 0.5, 
    strokeWeight: 5 
    }); 

    polyline.setMap(map); 

    map.fitBounds(bounds); 
} 
+0

感谢您的支持php的文件! – maxdangelo