1

我有一个谷歌地图设置,其中数据来自 mySql 后端,我以编码字符串的形式存储。我可以正确显示地图中的数据,但无法提供外部链接。

    for (x = 0; x < stringArray.length; x = x + 1)
    {
        var addressDetails = [];
        var marker;
        //Separate each field
        addressDetails = stringArray[x].split("&&&");
        //Load the lat, long data
        var lat = new google.maps.LatLng(addressDetails[1], addressDetails[2]);
        //Create a new marker and info window
        var y = x+1;
        marker = new google.maps.Marker({
            map: map,
            position: lat,
            content: addressDetails[0],
            icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld='+y+'|FF0000|000000'
        });
        //Pushing the markers into an array so that it's easier to manage them
        markersArray.push(marker);

        google.maps.event.addListener( marker, 'click', function () {
            closeInfos();
            var info = new google.maps.InfoWindow({content: this.content});
            //On click the map will load the info window
            info.open(map,this);
            infos[0]=info;
        });
       //Extends the boundaries of the map to include this new location
       bounds.extend(lat);
    }
    //Takes all the lat, longs in the bounds variable and autosizes the map
    map.fitBounds(bounds);

此代码用于外部点击。

    function myclick(i) {
      google.maps.event.trigger(markersArray[i], "click");
    }

我得到的错误是:“Uncaught ReferenceError: myclick is not defined”

请指导我哪里出错了?

谢谢,

4

1 回答 1

1

鉴于您尝试调用的方式myclick,您需要将其附加到您的window对象:

window.myclick = function(i) {
    console.log('I am defined and available in the global scope via window!');
}

这是一个演示

于 2013-07-15T12:22:18.357 回答