close close infoWindow / Hide Polyline Google maps api v3

时间:2011-03-31 10:20:12

标签: javascript google-maps google-maps-api-3

我需要能够显示隐藏单个折线及其相关的infoWindow + Marker。

这是我的代码,目前clearOverlays()函数隐藏了所有标记,我需要能够使用底部链接中的hideRoutePath()函数隐藏特定标记..任何有助于此的帮助因为我的想法已经用完了......谢谢!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8;" /> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script>
<title>test</title> 

<script type='text/javascript'> 

var map = null;
var markersArray = [];

//default load position
function initialize() {
    var latlng = new google.maps.LatLng(51.41859298,0.089179345);

    var settings = {
        zoom: 11,
        center: latlng,
        mapTypeControl: true,
        scaleControl: true,
        mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
        navigationControl: true,
        navigationControlOptions: {style: google.maps.NavigationControlStyle.DEFAULT},
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        backgroundColor: 'white'
    };

    map = new google.maps.Map(document.getElementById('map_canvas'), settings);

}
// End initialize function

function clearOverlays() {

  if (markersArray) {
    for (i in markersArray) {
      markersArray[i].setMap(null);

    }
  }
}


// Mapping variables
var global_strokeColor = "#ED00FF";
var global_strokeOpacity = 0.7;
var global_strokeWeight = 6;

//Route 1
function showRoutePath_1() {

    position = new google.maps.LatLng(51.41859298,0.089179345);
    var infowindow = new google.maps.InfoWindow({content: "Beaverwood School"});
    var marker = new google.maps.Marker({position: position,map: map});

    google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map,marker);
    });

    markersArray.push(marker);

    //open infowWindow with marker
    infowindow.open(map, marker);
    //setTimeout(function () { infowindow.close(); }, 5000);

    bromley_route638 = new google.maps.Polyline({
      path: [
      new google.maps.LatLng(51.408664,0.114405),
      new google.maps.LatLng(51.412973,0.114973),
      new google.maps.LatLng(51.417979,0.097195),
      new google.maps.LatLng(51.421214,0.023720)],
      strokeColor: global_strokeColor,
      strokeOpacity: global_strokeOpacity,
      strokeWeight: global_strokeWeight
    });

    bromley_route638.setMap(map);

}
// End Route 1

//Route 2
function showRoutePath_2() {

    position = new google.maps.LatLng(51.382522,0.045018);

    var infowindow = new google.maps.InfoWindow({content: "Bishops Justus School"});
    var marker = new google.maps.Marker({position: position,map: map});

    google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map,marker);
    });

    markersArray.push(marker);

    //open infowWindow with marker
    infowindow.open(map, marker);
    //setTimeout(function () { infowindow.close(); }, 5000);

    bromley_route638Run2 = new google.maps.Polyline({
      path: [
      new google.maps.LatLng(51.369530,0.002027),
      new google.maps.LatLng(51.375388,0.010733),
      new google.maps.LatLng(51.377991,0.009467),
      new google.maps.LatLng(51.401988,0.017248)],
      strokeColor: global_strokeColor,
      strokeOpacity: global_strokeOpacity,
      strokeWeight: global_strokeWeight
    });

    bromley_route638Run2.setMap(map);

}
//End Route 2


//Hide Routes
function hideRoutePath(number) {
    if(number == 1) {
    clearOverlays();
    bromley_route638.setMap(null);
    }
    else if(number == 2) {
    clearOverlays();
    bromley_route638Run2.setMap(null);}
}

</script> 

</head> 

<body onload='initialize()'> 

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

    <a class="exp" id="myMenu1">View schools in Hackney</a>

    <div class="expandable_box" id="myDiv1">
        <p>Select a school to view its bus route and location.</p>
        <a href="#" onClick="showRoutePath_1();"> Beaverwood School +</a> / <a href="#" onClick="hideRoutePath(1);">-</a><br/>
        <a href="#" onClick="showRoutePath_2();"> Bishop Justus School +</a> / <a href="#" onClick="hideRoutePath(2);">-</a>
    </div>

</body> 
</html>

2 个答案:

答案 0 :(得分:0)

看起来你只需要将路由声明为全局变量。 将以下内容添加到脚本顶部

var bromley_route638, bromley_route638Run2; 

答案 1 :(得分:0)

以下是完整的JSFiddle Demo:

这是一种方法,但可以优化代码。首先,我添加了两个新功能removeMarker和checkMarker。 removeMarker,从地图和数组中删除标记,这样您就不会在同一位置多次使用延迟标记。 checkMarker,检查标记是否已经在地图上和全局markersArray中。如果else返回false,则返回true。我们使用checkMarker来确保我们不会在同一个latlng中创建多个标记实例。在你的情况下,它是标记路线1和标记路线2。

//remove specific marker from map
function removeMarker(myMark) {
    if (markersArray) {
        for (var i in markersArray) {
            if (myMark == markersArray[i]['number']) {
                console.log(markersArray[i]['number']);
                markersArray[i].setMap(null);
                markersArray.splice(i,1); //removes marker from array
                break;
            }
        }
    }
}

//check if marker already exist on map
function checkMarker(number){
    if(markersArray){
        for(var i in markersArray){
            if(markersArray[i]['number'] == number)
                return true;
        }
    } else 
        return false;
}

然后我使用checkMarker添加一个标记检查器,以确保我们不会使用它的关联路径创建双标记,并且我还为您的制造商提供了一个唯一的“数字”标识符。 1为marker1,2为marker2。这两个mod应该在函数showRoutePath_1和showRoutePath_2:

//check if marker1 already on map if true do nothing
if(checkMarker(1))  //check if marker already in global marker array/map if marker 2 replace 1 with 2
    return;
position = new google.maps.LatLng(51.41859298, 0.089179345);
var infowindow = new google.maps.InfoWindow({
    content: "Beaverwood School"
});
var marker = new google.maps.Marker({
    position: position,
    map: map
});
marker['number'] = 1; //Here is the unique identifier we assign.  if marker 2 replace 1 with 2

最后但并非最不重要的是,我通过添加标识符参数“number”来更改hideRoutePath,并使用removeMarker函数隐藏/删除关联路径和标记:

function hideRoutePath(number) {
    if (number == 1) {
        //clearOverlays();
        bromley_route638.setMap(null);
    }
    else if (number == 2) {
        //clearOverlays();
        bromley_route638Run2.setMap(null);
    }
    removeMarker(number);
}