使用侧边栏点击触发google地图事件v3

发布于 2024-12-01 10:59:12 字数 2787 浏览 0 评论 0原文

仍然坚持这个问题,有人可以帮助我们取得一些进展,只是现在信息窗口出现问题。

<div id="map"></div>
<div id="sidebar">
  <h2>Sidebar</h2>
</div>


<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script> 
<script type="text/javascript">
  var locations = [
      ['Bondi Beach', -33.890542, 151.274856, 4],
      ['Coogee Beach', -33.923036, 151.259052, 5],
      ['Cronulla Beach', -34.028249, 151.157507, 3],
      ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
      ['Maroubra Beach', -33.950198, 151.259302, 1]
    ];

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      center: new google.maps.LatLng(-33.890542, 151.274856),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    //console.log(infowindow);

    var markers = [];

    var marker = [];


     var html = "<ul>";

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

         //console.info(marker.position());


      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map,
        icon: "http://lindsay-and-dan.gettingmarried.co.uk/images/maps/icon-music.gif"
      });

      //console.log(marker);

       var center = marker.position;
       markers.push(marker);
       console.log(markers[i]);

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
           //console.log(marker.position);
          map.panTo(center);

        }

      })(marker, i));

      //console.log(marker);
      //console.log(locations[i][0]);


      html += '<li><a href="#" class="move" map="' +  markers[i].Oa + ',' + markers[i].Pa  + ',' + locations[i][0] + '">' +  locations[i][0] + '</a></li>';


    }

    html += "</ul>";
    $('#sidebar').html(html).fadeIn('slow');

    $('.move').click(function(){ 

        //infowindow.close();

        var contents = $(this).attr('map');

        var array = contents.split(',');

        var lon = array[0];
        var lat = array[1];
        var content = array[2]; 

        console.log(content);


        infowindow.open(map, marker);
        infowindow.setContent(content);
        map.panTo(new google.maps.LatLng(lon,lat));


        return false;
    });



</script>

这是测试示例的链接。

http://extwit.users35.interdns.co.uk/gmap/

任何建议???

Still stuck with this one can someone please help made some progress just having issues with the info windows now.

<div id="map"></div>
<div id="sidebar">
  <h2>Sidebar</h2>
</div>


<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script> 
<script type="text/javascript">
  var locations = [
      ['Bondi Beach', -33.890542, 151.274856, 4],
      ['Coogee Beach', -33.923036, 151.259052, 5],
      ['Cronulla Beach', -34.028249, 151.157507, 3],
      ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
      ['Maroubra Beach', -33.950198, 151.259302, 1]
    ];

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      center: new google.maps.LatLng(-33.890542, 151.274856),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    //console.log(infowindow);

    var markers = [];

    var marker = [];


     var html = "<ul>";

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

         //console.info(marker.position());


      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map,
        icon: "http://lindsay-and-dan.gettingmarried.co.uk/images/maps/icon-music.gif"
      });

      //console.log(marker);

       var center = marker.position;
       markers.push(marker);
       console.log(markers[i]);

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
           //console.log(marker.position);
          map.panTo(center);

        }

      })(marker, i));

      //console.log(marker);
      //console.log(locations[i][0]);


      html += '<li><a href="#" class="move" map="' +  markers[i].Oa + ',' + markers[i].Pa  + ',' + locations[i][0] + '">' +  locations[i][0] + '</a></li>';


    }

    html += "</ul>";
    $('#sidebar').html(html).fadeIn('slow');

    $('.move').click(function(){ 

        //infowindow.close();

        var contents = $(this).attr('map');

        var array = contents.split(',');

        var lon = array[0];
        var lat = array[1];
        var content = array[2]; 

        console.log(content);


        infowindow.open(map, marker);
        infowindow.setContent(content);
        map.panTo(new google.maps.LatLng(lon,lat));


        return false;
    });



</script>

Here is a link to the test example.

http://extwit.users35.interdns.co.uk/gmap/

any suggestions???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北渚 2024-12-08 10:59:12

好的,所以帮助你绝对帮助了我。您的标记应如下所示:

marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    Oa: locations[i][1],
    Pa: locations[i][2],
    map: map,
    icon: "http://lindsay-and-dan.gettingmarried.co.uk/images/maps/icon-music.gif"
  });

已添加 OaPa

希望这会有所帮助

P.S:我刚刚注意到您的信息窗口本身的其他问题。添加 infowindow.open(map,marker); 并且信息窗口的定位已关闭......对此不太确定。

PPS:您的 $('.move') 代码块需要位于标记循环内;在此块中,将您的 marker 引用替换为 markers[i]。这应该适合你!

Ok, so helping you DEFINITELY helped me. Your marker should look something like this:

marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    Oa: locations[i][1],
    Pa: locations[i][2],
    map: map,
    icon: "http://lindsay-and-dan.gettingmarried.co.uk/images/maps/icon-music.gif"
  });

Oa and Pa were added.

Hope this helps

P.S: I just noticed your other issue with the info window itself. Add infowindow.open(map,marker); and also the positioning of the infowindow is off...not really sure about that one.

P.P.S.: Your $('.move') block of code needs to be within the markers loop; within this block, replace your marker reference with markers[i]. This should do it for you!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文