通过单击html链接在谷歌地图上显示infowindow

时间:2013-09-30 09:01:24

标签: javascript jquery google-maps

有没有办法从外部链接激活谷歌地图上的信息窗口? html和js代码如下。

我想有一个简单的外部链接来激活标记的infowindow。

感谢任何帮助

Sajja:)

<!DOCTYPE html>
<html lang="en">
<head>
<title>Map</title>
<style>
#map{
    width:640px;
    height: 480px;
    border:6px solid #6f5f5e;
    margin:20px auto 30px auto;
}


</style>
</head>

<body>


    <div class='container'>

  <div id="map">&nbsp;</div>

</div>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=true" type="text/javascript"></script>

<script>
var markers = [
    ['Marker 1', 13.988719,100.617909],
    ['Marker 2.', 13.662811,100.43758],
    ['Marker 3', 13.744961,100.535073],
    ['Marker 4', 13.801981,100.613864],
    ['Marker 5', 13.767507,100.644024],
];

$(document).ready(function()  {
    var myOptions = {
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        mapTypeControl: false
    };
    var map = new google.maps.Map(document.getElementById("map"),myOptions);
    var image = new google.maps.MarkerImage('img/marker.png',
        new google.maps.Size(65, 32),
        new google.maps.Point(0,0),
        new google.maps.Point(18, 42)
    );
    var infowindow = new google.maps.InfoWindow();
    var marker, i;
    var bounds = new google.maps.LatLngBounds();

    for (i = 0; i < markers.length; i++) {
        var pos = new google.maps.LatLng(markers[i][1], markers[i][2]);
        bounds.extend(pos);
        marker = new google.maps.Marker({
            position: pos,
            map: map,
            icon: image
        });

        google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
                infowindow.setContent(markers[i][0]);
                infowindow.open(map, marker);
            }
        })(marker, i));
    }
    map.fitBounds(bounds);
});
</script>
</body>

2 个答案:

答案 0 :(得分:4)

你可以使用,

function infoOpen(i)
{
    google.maps.event.trigger(markers[i],'click');
}

将此功能用于元素的onclick

例如

<a href="javascript:void(0);" onclick="infoOpen('1');">open it</a>

我在这里发布完整的代码

<html>
    <head>
        <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&language=en"></script>
        <script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
        <script>
            function infoOpen(i)
            {

                google.maps.event.trigger(gmarkers[i], 'click');
            }
            var gmarkers = [];
            var markers = [];
            markers = [
                ['0', 'Marker 1', 13.988719, 100.617909],
                ['1', 'Marker 2', 13.662811, 100.43758],
                ['2', 'Marker 3', 13.744961, 100.535073],
                ['3','Marker 4', 13.801981, 100.613864],
                ['4', 'Marker 5', 13.767507, 100.644024]];

            $(document).ready(function () {
                var myOptions = {
                    mapTypeId: google.maps.MapTypeId.ROADMAP,
                    mapTypeControl: false
                };

                var map = new google.maps.Map(document.getElementById("map"), myOptions);
                var image = new google.maps.MarkerImage('img/marker.png',
                new google.maps.Size(65, 32),
                new google.maps.Point(0, 0),
                new google.maps.Point(18, 42));
                var infowindow = new google.maps.InfoWindow();
                var marker, i;
                var bounds = new google.maps.LatLngBounds();

                for (i = 0; i < markers.length; i++) {
                    var pos = new google.maps.LatLng(markers[i][2], markers[i][3]);
                    var content = markers[i][1];
                    bounds.extend(pos);
                    marker = new google.maps.Marker({
                        position: pos,
                        map: map
                    });
                    gmarkers.push(marker);
                    google.maps.event.addListener(marker, 'click', (function(marker, content) {
                        return function() {
                            infowindow.setContent(content);
                            infowindow.open(map, marker);
                        }
                    })(marker, content));
                }


                map.fitBounds(bounds);
            });
        </script>
        <style>
            body {
                text-align: center
            }
            #map {
                width:640px;
                height: 480px;
                border:6px solid #6f5f5e;
                margin:20px auto 30px auto;
            }
        </style>
    </head>
    <body>
        <div>
            <a href="javascript:void(0);" onclick="infoOpen('0');">mark 1</a> <a href="javascript:void(0);" onclick="infoOpen('1');">mark 2</a> <a href="javascript:void(0);" onclick="infoOpen('2');">mark 3</a> <a href="javascript:void(0);" onclick="infoOpen('3');">mark 4</a> <a href="javascript:void(0);" onclick="infoOpen('4');">mark 5</a>

            <div id="map">&nbsp;</div>
        </div>
    </body>
</html>

我添加了gmarkers.push(marker)

答案 1 :(得分:0)

是的,它有效! http://jsfiddle.net/tinsajja/nXkXA/43/

好吧,假设我在地图上有很多标记。我如何管理制造商,我试图把它放在但它没有激活信息框。

由于我的编码技巧已被剪切和粘贴,因此我搜索并使用此http://jsfiddle.net/tinsajja/ezSUF/67/

代码

$('#listdiv').append('<p><a href="javascript:show(' + i + ')" id="link' + i + '">' + locations[i][0] + '</a></p>');

我尝试添加

<select><option><a href="javascript:show(' + i + ')"... 

但结果产生了5个选择框。请教我如何制作它。

谢谢, Sajja