0

所以这是我的代码。问题是地图显示,但地图上没有放置标记。latlongARR 是一个看起来像这样的数组

[{"name":"McChord Field DGC","lat":"47.128257","long":"-122.488847"},{"name":"环境 DGC","lat":"47.184732","long ":"-122.509232"},{"name":"Riverside Disc Golf Park","lat":"47.185683","long":"-122.212600"},{"name":"East Tacoma DGC"," lat":"47.227344","long":"-122.398491"},{"name":"白河","lat":"47.278501","long":"-122.197390"}]

似乎一切都应该工作?有什么明显的错误吗?

                var mapOptions = {
                    zoom:10,
                    center: currentLocation,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                    };
                var map = new google.maps.Map(document.getElementById('dgmap'), mapOptions);
                var infowindow = null;

                var infowindow = new google.maps.InfoWindow({content: 'loading...'}); 

                var marker, i;

                for(i=0; i < latlongARR.length; i++){
                    var markers;
                    marker = new google.maps.Marker({
                        position: new google.maps.LatLng(latlongARR[i][1], latlongARR[i][2]),
                        map:map
                        });
                        console.log(latlongARR[i][0]);
                    google.maps.event.addListener(marker, 'click', (function(marker, i){
                        return function(){
                            infowindow.setContent(latlongARR[i][0]);
                            infowindow.open(map, marker);
                        }
                        })(marker,i));
                    };

                }
4

2 回答 2

2
position: new google.maps.LatLng(latlongARR[i][1], latlongARR[i][2]),

latlongARR[i]是一个对象,你需要使用属性名而不是数字索引来获取值,上面的代码应该是:

position: new google.maps.LatLng(latlongARR[i].lat, latlongARR[i].long),

或者

position: new google.maps.LatLng(latlongARR[i]['lat'], latlongARR[i]['long']),

以下相同的问题:

infowindow.setContent(latlongARR[i][0]);

这应该是infowindow.setContent(latlongARR[i].name);

于 2012-10-17T02:02:59.320 回答
1

您应该使用调试器。第一件事是这样的:

                   marker = new google.maps.Marker({
                    position: new google.maps.LatLng(latlongARR[i][1], latlongARR[i][2]),
                    map:map
                    });

latlongARR[i][1]andlatlongARR[i][2]是未定义的,它们应该是latlongARR[i].latand latlongARR[i].long

于 2012-10-17T02:23:21.343 回答