谷歌地图 - 功能地理编码和标记

时间:2014-06-12 11:31:52

标签: javascript google-maps google-maps-api-3 google-maps-markers

我需要传递变量标记[x]和infoWindowContent [x]来代替字符串“Title Markers”和“Text Window”。

使用标记[x]或infoWindowContent [x]值未定义。

我该怎么做?

提前致谢!

function initialize(addresses, zoom_s) {

    // Multiple Markers
    var markers = [
        ['London Eye, London'],
        ['Palace of Westminster, London']
    ];

    // Info Window Content
    var infoWindowContent = [
        ['<div class="info_content"><h3>London Eye</h3><p>The London Eye is a giant Ferris wheel situated on the banks of the River Thames. The entire structure is 135 metres (443 ft) tall and the wheel has a diameter of 120 metres (394 ft).</p></div>'],
        ['<div class="info_content"><h3>Palace of Westminster</h3><p>The Palace of Westminster is the meeting place of the House of Commons and the House of Lords, the two houses of the Parliament of the United Kingdom. Commonly known as the Houses of Parliament after its tenants.</p></div>']
    ];

    var bounds = new google.maps.LatLngBounds();
    var myOptions = {
        mapTypeId: 'roadmap'
    };

    geocoder = new google.maps.Geocoder();
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    if (geocoder) {
        // Display multiple markers on a map
        var infoWindow = new google.maps.InfoWindow(),
            marker, i;
        var marker_testo = "";
        var info_testo = "";

        for (var x = 0; x < addresses.length; x++) {

            var marker_testo = addresses[x];

            geocoder.geocode({
                'address': addresses[x]
            }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
                        marker = new google.maps.Marker({
                            position: results[0].geometry.location,
                            map: map,
                            title: "Title Markers"

                        });

                        // Allow each marker to have an info window    
                        google.maps.event.addListener(marker, 'click', (function (marker, x) {
                            return function () {
                                infoWindow.setContent("Text Window");
                                infoWindow.open(map, marker);
                            }
                        })(marker, x));

                        // Automatically center the map fitting all markers on the screen
                        bounds.extend(results[0].geometry.location);

                        map.fitBounds(bounds);
                    }
                }
            });
        }

        // Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
        var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function (event) {
            this.setZoom(14);
            google.maps.event.removeListener(boundsListener);
        });
    }
}

1 个答案:

答案 0 :(得分:1)

var markers = [
    'London Eye, London',
    'Palace of Westminster, London'
];

console.log(markers[0]);

您的阵列内部无需使用挡板。请检查https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

如何构建数组,您需要使用markers[0][0]markers[1][0]等。

修改

要完成我的回答,这是实现你想要的方法。创建一个计数器并在 的地理编码器结果中增加它,并创建一个新变量来保存infoWindow的内容。

var iwContent = infoWindowContent[counter];

var marker = new google.maps.Marker({
    position: results[0].geometry.location,
    map: map,
    title: addresses[counter]
});

google.maps.event.addListener(marker, 'click', function () {

    infoWindow.setContent(iwContent);
    infoWindow.open(map, marker);
});

counter++;

查看我的working example