Google Map v3标记点击功能问题并触发外部链接

时间:2011-02-20 16:18:05

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

我正在使用谷歌地图v3 api我想显示信息泡泡。例如,当我单击标记任何标记时,该功能将打开相关的气泡。另一方面,我想通过外部链接触发所有泡沫。我认为marker, 'click', function 应该是每个功能。我已经尝试了很多例子,但我没有工作。

这是我的测试地址: http://www.gercekustu.com/test/

这是我的代码:

function getGoogleMap(Altitude, Latitude, Address) {
            //var myLatlng = new google.maps.LatLng(Altitude, Latitude, Address);
            var image = 'icon.png';

            var myOptions = {
                zoom: 10,
                center: new google.maps.LatLng(-33.9, 151.2),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            }

            var map = new google.maps.Map(document.getElementById("googleMap"), myOptions);

            var locations = [
                ['Test içeriği5', -33.890542, 151.274856, 5],
                ['Test içeriği4', -33.923036, 151.259052, 4],
                ['Test içeriği3', -34.028249, 151.157507, 3],
                ['Test içeriği2', -33.80010128657071, 151.28747820854187, 2],
                ['Test içeriği1', -33.950198, 151.259302, 1]
            ];

            for (var i = 0; i < locations.length; i++) {
                //var image = new google.maps.MarkerImage('icon' + i + '.png',
                var image = new google.maps.MarkerImage('icon.png',
                new google.maps.Size(40, 34),
                new google.maps.Point(0, 0),
                new google.maps.Point(10, 34));

                var location = locations[i];
                //alert(location[3]);
                var myLatLng = new google.maps.LatLng(location[1], location[2]);
                var marker = new google.maps.Marker({
                    position: myLatLng,
                    map: map,
                    icon: image,
                    title: location[0],
                    zIndex: location[3]
                });

                var infowindow = new google.maps.InfoWindow({
                    content: location[0],
                    position: myLatLng
                });

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

            }
            infowindow.open(map);
        }

感谢您的帮助

2 个答案:

答案 0 :(得分:4)

我修改了你的代码,这个适用于我: -

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Google Map</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">

    $(document).ready(function () {
        var map;
        var infowindow;

        getGoogleMap('41.033245', '29.110191', '<div class="mapContainer"><div class="mapContentLeft"><h1>Hotel Name <i>Information/Suggestion</i></h1><div>Hotel Image</div></div><div class="mapContentRight"><div class="mapHotelStars">*****</div><div class="mapHotelAdress">Hotel Adress</div><div class="mapHotelPrice"> Currency + Integer </div></div></div>');

        function getGoogleMap(Altitude, Latitude, Address) {
            var myOptions = {
                zoom: 10,
                center: new google.maps.LatLng(-33.9, 151.2),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            }

            map = new google.maps.Map(document.getElementById("googleMap"), myOptions);

            infowindow = new google.maps.InfoWindow();

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

            var locations = [
                ['Test 5', -33.890542, 151.274856, 5],
                ['Test 4', -33.923036, 151.259052, 4],
                ['Test 3', -34.028249, 151.157507, 3],
                ['Test 2', -33.80010128657071, 151.28747820854187, 2],
                ['Test 1', -33.950198, 151.259302, 1]
            ];

            for (var i = 0; i < locations.length; i++) {
                var location = locations[i];
                var title = location[0];
                var latitude = location[1];
                var longitude = location[2];
                displayMarker(title, latitude, longitude);
            }
        }

        function displayMarker(title, latitude, longitude) {

            var latlong = new google.maps.LatLng(latitude, longitude);

            map.setCenter(latlong);

            var marker = new google.maps.Marker({
                map: map,
                title: title,
                icon: "icon.png",
                position: latlong
            });

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

    });
</script>
<style type="text/css">
 .googleMapContainer {width:700px; height:500px;}
 .mapContainer {border:1px solid red;}
</style>
</head>
<body>
    <form id="form1">
      <div class="googleMapContainer" id="googleMap"></div>
   </form>
</body>
</html>

答案 1 :(得分:1)

由于某种原因,所有事件侦听器都从for循环中获取最后一个标记。我认为这是一个JavaScript的东西。 您需要use closures in event listeners喜欢这样:

更新:修改了以下代码,以便在adding a DOM event listener点击链接时打开所有信息窗口。该链接必须具有id="link_id"属性。

function attachInfoWindow(map, marker) {
  var infowindow = new google.maps.InfoWindow(
      { content: marker.title
      });
  google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map,marker);
  });
  google.maps.event.addDomListener(document.getElementById('link_id'),'click', function() {
   infowindow.open(map,marker);
  });
}

function getGoogleMap(Altitude, Latitude, Address) {
    //var myLatlng = new google.maps.LatLng(Altitude, Latitude, Address);
    var image = 'icon.png';

    var myOptions = {
        zoom: 10,
        center: new google.maps.LatLng(-33.9, 151.2),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    var map = new google.maps.Map(document.getElementById("googleMap"), myOptions);

    var locations = [
        ['Test içeriği5', -33.890542, 151.274856, 5],
        ['Test içeriği4', -33.923036, 151.259052, 4],
        ['Test içeriği3', -34.028249, 151.157507, 3],
        ['Test içeriği2', -33.80010128657071, 151.28747820854187, 2],
        ['Test içeriği1', -33.950198, 151.259302, 1]
    ];

    for (var i = 0; i < locations.length; i++) {
        //var image = new google.maps.MarkerImage('icon' + i + '.png',
        var image = new google.maps.MarkerImage('icon.png',
        new google.maps.Size(40, 34),
        new google.maps.Point(0, 0),
        new google.maps.Point(10, 34));

        var location = locations[i];

        //alert(location[3]);
        var myLatLng = new google.maps.LatLng(location[1], location[2]);
        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            icon: image,
            title: location[0],
            zIndex: location[3]
        });
        attachInfoWindow(map, marker);

    }
    //infowindow.open(map);
}
相关问题