0

我正在从 mysql 数据库中获取我的坐标,如下所示

(<?php echo $_Mylat?>, <?php echo $_Mylon?>)
(<?php echo $_Mylat2?>, <?php echo $_Mylon2?>)

当我绘制地图时,它显示了两个点和方向,如下面的代码所示

    function initialize() {
    var mapOptions = {
    zoom: 13,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: center
    };

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

    for (var i=0; i<a.length;i++)
    {
        marker = new google.maps.Marker({
        map:map,
        draggable:true,
        animation: google.maps.Animation.DROP,
        position: a[i]
       });

    }   
     var flightPlanCoordinates = [
         new google.maps.LatLng(<?php echo $_Mylat?>, <?php echo $_Mylon?>),
         new google.maps.LatLng(<?php echo $_Mylat2?>, <?php echo $_Mylon2?>)
    ];

例如,当我单击或将鼠标悬停在标记上时,我希望出现一个消息框,但它不起作用,有人可以帮我更正代码吗

     var contentstring = 'hey,this is my location'
     var infowindow = new google.maps.InfoWindow ({
    content:contentstring
    })

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

2 回答 2

1

您必须为每个标记(在循环内)添加一个侦听器和信息窗口,所以它应该是这样的(未经测试):

for (var i=0; i<a.length;i++) {

    var marker = new google.maps.Marker({
        map:map,
        draggable:true,
        animation: google.maps.Animation.DROP,
        position: a[i]
    });

    var contentstring = 'hey,this is my location'

    var infowindow = new google.maps.InfoWindow({
        content: contentString
    });

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

} 

如果您一次只想拥有一个信息窗口,您应该重用信息窗口,如此所述。

于 2013-03-06T13:46:17.567 回答
1

通常,信息窗口带有标记。设置标记时,还要设置信息窗口和点击事件。

有关信息窗口使用的示例,请参阅google 文档。

不知道你从哪里得到 a.length,但我假设那是你飞行计划的两点。

for (var i=0; i<a.length;i++)
    {
        marker = new google.maps.Marker({
        map:map,
        draggable:true,
        animation: google.maps.Animation.DROP,
        position: a[i]
       });

    }

    var contentstring = 'hey,this is my location'
    var infowindow = new google.maps.InfoWindow ({
        content:contentstring
    })

    google.maps.event.addListener(marker,'click',function(){
        infowindow.open(map,marker);
    });
}
于 2013-03-06T13:48:33.177 回答