google.maps.InfoWindow

时间:2013-03-06 13:25:01

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

我从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,????????);});

2 个答案:

答案 0 :(得分:1)

你必须为每个标记(在循环内)添加一个监听器和infowindow,所以它应该是这样的(未经测试):

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);
    });

} 

如果您只希望一次拥有一个InfoWindow,则应重用here所述的信息窗口。

答案 1 :(得分:1)

一般情况下,infowindows会使用标记。设置标记时,还要设置信息窗口并单击事件。

有关信息窗使用的示例,请参阅the google docs

不知道你从哪里获得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);
    });
}