谷歌地图和多个标记

时间:2012-10-17 01:46:44

标签: javascript google-maps-api-3

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

  

[{“name”:“McChord Field   DGC “ ”LAT“: ”47.128257“, ”长“: ” - 122.488847“},{ ”名“:” 环境   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“:”White   河”, “LAT”: “47.278501”, “长”: “ - 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));
                    };

                }

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:1)

您应该使用调试器。首先是:

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

latlongARR[i][1]latlongARR[i][2]未定义,应为latlongARR[i].latlatlongARR[i].long