0

我正在使用这样的代码从谷歌的地图中加载一个 kml:

var myOptions = {
    mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
var kmlLayer = new google.maps.KmlLayer('https://maps.google.com/maps/ms?authuser=0&vps=2&ie=UTF8&msa=0&output=kml&msid=209879288832578501414.0004c52d678cf88f70685');
kmlLayer.setMap(map);

问题是弹出窗口的链接,kml 添加 target="_blank",我不想要它。

如何使用 javascript 将其删除?我找到了这个答案,但它在 API 3 中不起作用。我不喜欢使用 jQuery。

4

2 回答 2

0

通过 jQuery,

加载地图后执行此操作,

$('a[target="_blank"]').prop('target','_self'); 
于 2012-07-19T14:22:28.863 回答
0

通过@geocodezip的评论,我得到了解决方案。它并不完美,InfoWindow始于标记的脚下而不是头部,但它是某种东西。

var infoWindow;
var map;

function initialize() {
    var myOptions = {
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);

    var opts = {suppressInfoWindows: true};
    var kmlLayer = new google.maps.KmlLayer('https://maps.google.com/maps/ms?authuser=0&vps=2&ie=UTF8&msa=0&output=kml&msid=209879288832578501414.0004c52d678cf88f70685', opts);
    kmlLayer.setMap(map);

    infoWindow = new google.maps.InfoWindow();

    google.maps.event.addListener(kmlLayer, 'click', function(kmlMouseEvent) {
        var name = kmlMouseEvent.featureData.name;
        var descr = kmlMouseEvent.featureData.description.replace(/ target="_blank"/ig, '');
        var dom = '<div style="font-family: Arial, sans-serif; font-size: small">' +
            '<div style="font-weight: bold; font-size: medium; margin-bottom: 0em">' +
            name +
            '</div>' +
            '<div>' +
            descr +
            '</div>' +
            '</div>';
        infoWindow.setContent(dom);
        infoWindow.setPosition(kmlMouseEvent.latLng);
        infoWindow.open(map);
    });
}
于 2012-07-21T16:05:46.500 回答