0

我无法在 Google Maps V3 中显示多个标记,我得到了正确的坐标但没有显示在地图上。控制台中也没有错误

  1. map_items[0] = 标题
  2. map_items[1] = 55.153766, 11.909180
  3. map_items[2] = 链接
  4. map_items[3] = 文本

如果我发出警报,它们都会正确显示。例子

“标题”、“51.00150763193481、-2.5659284999999272”、“链接”、“文本”

 function initialize() {
            var map = new google.maps.Map(document.getElementById('map'), {
                zoom: 7,
                center: new google.maps.LatLng(55.153766, 11.909180),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });
            for (var x = 0; x < temp.length; x++) {
                if(temp[x][1]){
                    var map_items = temp[x];
                    var myLatlng = new google.maps.LatLng(map_items[1]);
                    var marker = new google.maps.Marker({
                        map: map,
                        position: myLatlng,
                        title: map_items[0]
                    });
                    google.maps.event.addListener(marker, 'click', function() {
                        infowindow.setContent('<div class="google_marker"><a href="'+map_items[2]+'">'+map_items[0]+'</a><br /><p>'+map_items[3]+'</p></div>');
                        infowindow.open(map, marker);
                    });
                }
            }
        }
4

3 回答 3

7

google.maps.LatLng() 接受两个参数,而不是一个,所以:

var latlon = map_items[1].split(',');
var myLatlng = new google.maps.LatLng(parseFloat(latlon[0]), parseFloat(latlon[1]));

虽然实际上,最好使用对象而不是数组,其中每个项目都包含不同的数据类型,例如:

marker_data[0] = {
  "lat": 55.153766,
  "lon": 11.909180,
  "title": "My Title",
  "link": "http://stackoverflow.com"
}
//etc...

然后你会像这样访问它:

var myLatlng = new google.maps.LatLng(marker_data[0].lat, marker_data[0].lon);
于 2012-09-10T07:23:28.850 回答
1

Assuming map_items[1] is string

if (temp[x][1]) {
    var map_items = temp[x];
    var latlng = map_items[1].split(",");
    var myLatlng = new google.maps.LatLng(parseFloat(latlng[0]), parseFloat(latlng[1]));
    var marker = new google.maps.Marker({
        map: map,
        position: myLatlng,
        title: map_items[0]
    });
    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent('<div class="google_marker"><a href="' + map_items[2] + '">' + map_items[0] + '</a><br /><p>' + map_items[3] + '</p></div>');
        infowindow.open(map, marker);
    });
}
于 2012-09-10T07:27:19.593 回答
0

我认为当您单击任何标记时,只有最后一个 map_items 可用。创建不同的上下文可能会解决您的问题。

function initialize() {
                var map = new google.maps.Map(document.getElementById('map'), {
                    zoom: 7,
                    center: new google.maps.LatLng(55.153766, 11.909180),
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                });
                function AttachEventToMarker(marker, map_items){

                    google.maps.event.addListener(marker, 'click', function() {
                       infowindow.setContent('<div class="google_marker"><a href="'+map_items[2]+'">'+map_items[0]+'</a><br /><p>'+map_items[3]+'</p></div>');
                       infowindow.open(map, marker);
                    });
                }

                for (var x = 0; x < temp.length; x++) {
                    if(temp[x][1]){
                        var map_items = temp[x];
                        var myLatlng = new google.maps.LatLng(map_items[1]);
                        var marker = new google.maps.Marker({
                            map: map,
                            position: myLatlng,
                            title: map_items[0]
                        });
                         AttachEventToMarker(marker, map_items);
                    }
                }
            }
于 2012-09-10T07:36:17.930 回答