0

我有一个带有标记集群和各种点的谷歌地图地图。使用事件侦听器生成如下点,它会在页面加载的每个点上消失。

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] 传递给函数,也会发生同样的事情。当在地图上单击其对应点时,有什么方法可以从数组(全局定义)中获取一条信息?

4

2 回答 2

0

我猜永久链接数组来自 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();
于 2012-08-28T15:13:29.100 回答
0

我创建了一个带有 2 个标记的谷歌地图,当单击一个标记时,该区域的名称会出现在信息窗口中。标记中的信息通过名为 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>

我希望这有帮助。

于 2012-08-28T17:04:39.437 回答