0

我尝试在搜索后打开信息窗口并平移到谷歌地图上的位置。有人能帮我吗?谢谢你。

var availableTags = [];
for (i=0;i<mapLocationsJSON.length;i++){
    availableTags[i]=mapLocationsJSON[i].title;
}

//This is the autocomplete text field
$(function() {
    $( "#search" ).autocomplete({
      source: availableTags
    });
});

//Insert the function here for what you want it to do when you search
function myFunc(searchTerm){
    var validSearch=false;
    var markerNumber;

    //Check to see if the term they searched for matches any of the locations in the map data
    for (i=0;i<mapLocationsJSON.length;i++){
        if(searchTerm==mapLocationsJSON[i].title){
            validSearch=true;
            markerNumber=i;
            break;
        }
    }
    if(validSearch){
        //Pan to the position and zoom in.
        map.panTo(markers[markerNumber].getPosition());
        map.setZoom(7);

        //I'm not sure how to open the info window.... ?
        //infowindow.open(map,markers[markerNumber]);
    }else{
        //if the data doesn't match a term, ask them to search again. 
        alert("Please try again")
    };
}

搜索完位置后我应该怎么做才能打开信息窗口

4

1 回答 1

1

假设您在标记上有一个预先存在的点击侦听器(您没有包含定义标记的代码),最简单的方法是:

if(validSearch){
        //Pan to the position and zoom in.
        map.panTo(markers[markerNumber].getPosition());
        map.setZoom(7);
        // click on the marker
        google.maps.event.trigger(markers[markerNumber],'click');
}else{
        //if the data doesn't match a term, ask them to search again. 
        alert("Please try again")
}
于 2013-03-26T20:38:24.923 回答