【问题标题】:google.maps.InfoWindowgoogle.maps.InfoWindow
【发布时间】:2013-02-21 07:33:33
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

    有关信息窗口使用示例,请参阅 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);
        });
    }
    

    【讨论】:

      【解决方案2】:

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

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

      如果您一次只想拥有一个信息窗口,您应该按照here 的描述重复使用信息窗口。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-21
        • 1970-01-01
        • 1970-01-01
        • 2014-01-15
        • 1970-01-01
        相关资源
        最近更新 更多