簡體   English   中英

javascript變量是未定義的甚至定義?

[英]javascript variable is undefined even defined?

var markers;
function initListMarkers() {
        var markers;

    var mpMarkers = [
        ['place 1', 13.784420, 100.684456, 1],
        ['', 13.744571, 100.436233, 7],
        ['', 13.807593, 100.510734, 8],
        ['', 13.783086, 100.493740, 9],
        ['', 13.806426, 100.578541, 10],
        ['', 13.660516, 100.605148, 11],
        ['', 13.761079, 100.710033, 12],
        ['', 13.691707, 100.750974, 13],
        ['', 13.680032, 100.476874, 14],
        ['', 13.678364, 100.747069, 15],
        ['swb', 13.676029, 100.734709, 16],
    ];

    var infoWindowContent = [
        ['<div class="myplace-info-content"><h3>place 1</h3><p></p></div>'],
        ['<div class="myplace-info-content"><h3></h3><p></p></div>'],
        ['<div class="myplace-info-content"><h3></h3><p></p></div>'],
        ['<div class="myplace-info-content"><h3></h3><p></p></div>'],
        ['<div class="myplace-info-content"><h3></h3><p></p></div>'],
        ['<div class="myplace-info-content"><h3></h3><p></p></div>'],
        ['<div class="myplace-info-content"><h3></h3><p></p></div>'],
        ['<div class="myplace-info-content"><h3></h3><p></p></div>'],
        ['<div class="myplace-info-content"><h3></h3><p></p></div>'],
        ['<div class="myplace-info-content"><h3></h3><p></p></div>'],
        ['<div class="myplace-info-content"><h3>swb</h3><p>suwarnnabhumi airport</p></div>'],
    ];

    var infoWindow = new google.maps.InfoWindow(), marker, i;

    for( i = 0; i < mpMarkers.length; i++ ) {
        var position = new google.maps.LatLng(mpMarkers[i][1], mpMarkers[i][2]);

        bounds.extend(position);

        marker = new google.maps.Marker({
            position: position,
            map: map,
            title: mpMarkers[i][0]
        });

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

        var mp_id = mpMarkers[i][3];
        markers[mp_id] = marker;

        map.fitBounds(bounds);
    }

    var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) {
        this.setZoom(12);
        google.maps.event.removeListener(boundsListener);
    });
}

在螢火蟲說

TypeError:標記未定義

markers [mp_id] =標記;

但標記已定義。 首先是外部功能。 函數內部的第二位ID。

下面的功能工作正常。

function addMarkerDraggable(group_id) {
    // get current view center location
    var current_view = map.getCenter();
    var current_lat = current_view.lat();
    var current_lng = current_view.lng();

    /**
     * set variable from ajax fixed by http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call
     */
    var mp_id = get_from_ajax_as_json();

    mp_id = mp_id.responseJSON;
    mp_id = mp_id.mp_id;

    if (mp_id != 'undefined' && mp_id != '') {
        // myplace id was set, set marker.
        marker = new google.maps.Marker({
            draggable: true,
            position: current_view,
            icon: default_marker_icon,
            map: map,
            title: '',
            animation: google.maps.Animation.DROP
        });

        // set markers array for easy to remove.
        markers[mp_id] = marker;
    }
}// addMarkerDraggable

為什么它的工作方式不同而應該相同?

如何從函數initListMarkers()設置標記數組?

您已經聲明了markers但尚未為其分配任何值。 如果要使其成為數組,只需將其值初始化為一個新數組,如下所示:

var markers = [];

當您具有按特定順序排列的元素序列(索引編號為0… n)時,數組是理想的選擇。 但是,從粗略地看代碼的情況來看,似乎更像是將其用作關聯數組或映射,其中您有許多項目(無特定順序)由字符串索引。 在這種情況下,通常更適合使用對象:

var markers = {};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM