2

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

我想要一个简单的外部链接来激活标记的信息窗口。

任何帮助将不胜感激

萨贾:)

<!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>

4

2 回答 2

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)

于 2013-09-30T09:07:04.630 回答
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 个选择框。请教我怎么做。

谢谢, 萨贾

于 2013-10-01T14:17:34.450 回答