Google Maps v3 Listener(单击) - 更改URL

时间:2012-08-28 14:54:29

标签: javascript google-maps

我有一个带有markercluster的谷歌地图地图和各种要点。使用事件监听器生成如下所示的点,它会在页面加载的每一个点上消失。

   
function mappyfuntime() {
    var mapOptions = {
        center: new google.maps.LatLng(51.481581,-3.17909),
        zoom: 4,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("bigmap"), mapOptions);
    <?php echo prepare_javascript(); ?>
    var markers = [];
    var marker = [];
    linkto = new Array();
    for(g=0; g<LocationsArray.length; g++) {
        var latlng = new google.maps.LatLng(LocationsArray[g][0], LocationsArray[g][1]);
        marker[g] = new google.maps.Marker({'position': latlng});
        markers.push(marker[g]);
        google.maps.event.addListener(marker[g], 'click', click_handler(PermalinksArray[g]));
    }
    var markerCluster = new MarkerClusterer(map, markers);
}

mappyfuntime();
function click_handler(link) {
    alert(link);
}

但是,如果我将google.maps.event.addListener更改为以下内容,则会返回未定义的警报。

   
google.maps.event.addListener(marker[g], 'click', function() {
    alert(PermalinksArray[g]);
});

如果我将PermalinksArray [g]传递给函数,同样的事情就会发生。当在地图上点击相应的点时,有没有办法从数组(全局定义)中获取一条信息?

2 个答案:

答案 0 :(得分:0)

我猜Permalinks数组来自php插入?那么它不在全球范围内。尝试将php-insert放在你的javascript脚本顶部。

   
// set globals
var map;
var markers = [];
var PermalinksArray = ['...'];
var LocationsArray = ['...'];

function mappyfuntime() {

    // init map
    var mapOptions = {
        center: new google.maps.LatLng(51.481581,-3.17909),
        zoom: 4,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("bigmap"), mapOptions);

    // create markers        
    for(g=0; g<LocationsArray.length; g++) {

        // create new Position
        var latlng = new google.maps.LatLng(LocationsArray[g][0], LocationsArray[g][1]);
        // create new marker with position
        var marker = new google.maps.Marker({'position': latlng});
        // add event to marker
        google.maps.event.addListener(marker, 'click', function() {
            click_handler(marker, PermalinksArray[g]);
        });
        // push marker into markers array
        markers.push(marker);
    }
    var markerCluster = new MarkerClusterer(map, markers);
}

function click_handler(marker, link) {
    alert(link);
}

mappyfuntime();

答案 1 :(得分:0)

我创建了一个包含2个标记的Google地图,当点击标记时,该区域的名称会显示在信息窗口中。标记中的信息通过名为cityList的全局定义数组加载。你可以在下面找到我的代码。

<!doctype html>
<html lang="en">
   <head>
        <title>jQuery mobile with Google maps - Google maps jQuery plugin</title>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
        <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3&sensor=false&language=en"> </script>
        <script type="text/javascript">

            var cityList = [
                ['Chicago', 41.850033, -87.6500523, 1],
                ['Illinois', 40.797177,-89.406738, 2]
            ],
            demoCenter = new google.maps.LatLng(41,-87),
            map;

            function initialize()
            {
                map = new google.maps.Map(document.getElementById('map_canvas'), {
                   zoom: 7,
                   center: demoCenter,
                   mapTypeId: google.maps.MapTypeId.ROADMAP
                 });
            }

            function addMarkers()
            {
                var marker, i;
                var infowindow = new google.maps.InfoWindow();
                for (i = 0; i < cityList.length; i++) 
                {  
                    marker = new google.maps.Marker({
                        position: new google.maps.LatLng(cityList[i][1], cityList[i][2]),
                        map: map,
                        title: cityList[i][0]
                    });

                    google.maps.event.addListener(marker, 'click', (function(marker, i) {
                        return function() {
                            infowindow.setContent(cityList[i][0]);
                            infowindow.open(map, marker);
                        }
                    })(marker, i));
                }
            }

            $(document).on("pageinit", "#basic-map", function() {
                initialize();
            });

            $(document).on('click', '.add-markers', function(e) {
                e.preventDefault();
                addMarkers();
            });

        </script>
    </head>
    <body>
        <div id="basic-map" data-role="page">
            <div data-role="header">
                <h1><a data-ajax="false" href="/">jQuery mobile with Google maps v3</a> examples</h1>
                <a data-rel="back">Back</a>
            </div>
            <div data-role="content">   
                <div class="ui-bar-c ui-corner-all ui-shadow" style="padding:1em;">
                    <div id="map_canvas" style="height:350px;"></div>
                </div>
                <a href="#" class="add-markers" data-role="button" data-theme="b">Add Some More Markers</a>
            </div>
        </div>      
    </body>
</html>

我希望这会有所帮助。