开发者

Using sidebar click to trigger google maps event v3

开发者 https://www.devze.com 2023-03-30 05:01 出处:网络
Still stuck with this one can someone please help made some progress just hav开发者_如何转开发ing issues with the info windows now.

Still stuck with this one can someone please help made some progress just hav开发者_如何转开发ing 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???


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号