Javascript 数组项 == “未定义”在谷歌地图功能中

发布于 2024-11-01 17:52:08 字数 1318 浏览 1 评论 0原文

你好 我正在循环一个 JSON 对象并为每个项目创建一个新的地图标记。创建标记后,我将其推送到一个数组(varmarkers)以供将来操作。问题是,当我尝试访问数组项(例如:console.log(markers[3]))时,我得到一个“未定义”值。

下面的相关代码:

var map;
var markers = [];
var activeFilters = [];

function initializeMap() {
    var myLatlng = new google.maps.LatLng( 51.506873, -0.125141);
    var myOptions = {
        zoom: 14,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        disableDefaultUI: true
    }
    map = new google.maps.Map(document.getElementById("location_map"), myOptions);


    $.getJSON('../js/hotspots.json', function(data) {
        var hotspots = data.scenes[0].hotspots;
        $.each(hotspots, function(i) {

            var lat = hotspots[i].latitude;
            var lon = hotspots[i].longitude;
            var point = new google.maps.LatLng(lat,lon);

            var cat = hotspots[i].hotspottype;
            var weburl = hotspots[i].weburl;

            var marker = addMarker(point,cat,weburl);
        });

    });


}

function addMarker(point, cat, weburl) {
    var markerUrl = weburl;
    var marker = new google.maps.Marker({
        position: point,
        map: map
    });

    marker.category = cat;
    markers.push(marker);
};

initializeMap();
console.log(markers[3]);

任何帮助将不胜感激。

Hello
I'm looping over a JSON object and creating a new map marker for each item. After the marker is created, I push it to an array (var markers) for future manipulation. Problem is, when I try to access the array items (eg: console.log(markers[3])), I am given an "undefined" value.

Relevant code below:

var map;
var markers = [];
var activeFilters = [];

function initializeMap() {
    var myLatlng = new google.maps.LatLng( 51.506873, -0.125141);
    var myOptions = {
        zoom: 14,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        disableDefaultUI: true
    }
    map = new google.maps.Map(document.getElementById("location_map"), myOptions);


    $.getJSON('../js/hotspots.json', function(data) {
        var hotspots = data.scenes[0].hotspots;
        $.each(hotspots, function(i) {

            var lat = hotspots[i].latitude;
            var lon = hotspots[i].longitude;
            var point = new google.maps.LatLng(lat,lon);

            var cat = hotspots[i].hotspottype;
            var weburl = hotspots[i].weburl;

            var marker = addMarker(point,cat,weburl);
        });

    });


}

function addMarker(point, cat, weburl) {
    var markerUrl = weburl;
    var marker = new google.maps.Marker({
        position: point,
        map: map
    });

    marker.category = cat;
    markers.push(marker);
};

initializeMap();
console.log(markers[3]);

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忘东忘西忘不掉你 2024-11-08 17:52:08

get json 异步运行。试试这个:

$.getJSON('../js/hotspots.json', function(data) {
    var hotspots = data.scenes[0].hotspots;
    $.each(hotspots, function(i) {
        var lat = hotspots[i].latitude;
        var lon = hotspots[i].longitude;
        var point = new google.maps.LatLng(lat,lon);
        var cat = hotspots[i].hotspottype;
        var weburl = hotspots[i].weburl;
        var marker = addMarker(point,cat,weburl);
    });
    console.log(markers);
});

get json runs asynchronously. try this:

$.getJSON('../js/hotspots.json', function(data) {
    var hotspots = data.scenes[0].hotspots;
    $.each(hotspots, function(i) {
        var lat = hotspots[i].latitude;
        var lon = hotspots[i].longitude;
        var point = new google.maps.LatLng(lat,lon);
        var cat = hotspots[i].hotspottype;
        var weburl = hotspots[i].weburl;
        var marker = addMarker(point,cat,weburl);
    });
    console.log(markers);
});
梦途 2024-11-08 17:52:08

尝试一下,

console.log(markers[0]);

因为查看您的代码,markers 数组中只有一个元素。

如果您要在该数组中放入 4 个元素,那么使用 markers[3] 您将访问第四个元素。

Try to

console.log(markers[0]);

because looking at your code, there is just one element in markers array.

If you will put into that array 4 elements, then using markers[3] you will access fourth element.

对风讲故事 2024-11-08 17:52:08

尝试:

initializeMap();
setTimeout(function(){
    console.log(markers[3]);
}, 10000);

try:

initializeMap();
setTimeout(function(){
    console.log(markers[3]);
}, 10000);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文