5

我正在使用 KML 在 Google 地图上叠加形状。元素中包含与每个形状对应的信息<description>,以及指向与该形状对应的详细信息页面的链接。

例如,我的 KML 包括以下内容:

<description>
    <![CDATA[
    <div>

     ...

        <p>
            <a href="Concession.20.aspx">View details</a>
        </p>
        &nbsp;
    </div>
]]>

当然,我希望该链接在同一窗口中打开,因为它只是导航到同一站点上的另一个页面。不幸的是,如此处所述,嵌入在<description>KML 文件元素中的链接被重写为target='_blank'.

目标在直接写入 KML 的 HTML 中被忽略;打开所有此类链接,就好像目标设置为 _blank。任何指定的目标都将被忽略。

我的问题:谁能想到一种解决方法来覆盖这种(令人讨厌的,恕我直言)行为并强制这些链接在同一个窗口中打开?

作为一种方法的示例,我目前正在尝试覆盖这些链接上的点击事件(使用 jQuery),但它们是由谷歌地图动态生成的,我似乎无法及早掌握它们。

4

6 回答 6

6

我无法让这些例子起作用。最后,我在 jQuery 中做到了这一点,一旦点击它就会打开链接。

  $('#map_canvas').delegate('a', 'click', function(event) {
    window.location.href=$(this).attr('href');
    return false;
  });
于 2011-12-16T13:16:13.763 回答
3

我想出了一个使用 jQuery 和地图infowindowopen事件的工作解决方案。这是地图的初始化代码:

    map = new google.maps.Map2(document.getElementById("map"));

    ...

    GEvent.addListener(map, "infowindowopen", function() {
        // Get a reference to the infoWindow
        var infoWindow = $(this.getInfoWindow().getContentContainers());
        // Find all <a> tags in the infoWindow and reset their target attribute
        $("a", infoWindow).attr("target", "_self");
    });
于 2009-07-26T18:55:15.827 回答
2

这对我有用。

<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
function initialize() {
  var myLatlng = new google.maps.LatLng(49.8,15.8);
  var myOptions = {
    zoom: 7,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    mapTypeControl: false
  }
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  var ctaLayer = new google.maps.KmlLayer('http://zonglovani.info/mapa/mapa-cz.kml');
  ctaLayer.setMap(map);

  google.maps.event.addListener(ctaLayer, 'click', function(kmlEvent) {
    kmlEvent.featureData.description = kmlEvent.featureData.description.replace(/ target="_blank"/ig, "");
  });
}

function loadScript() {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
  document.body.appendChild(script);
});

window.onload = loadScript;
</script>
于 2010-08-22T08:16:00.767 回答
1

为了获得这些点击事件,您还可以使用 jQuery 实时事件:(请注意,Google 地图弹出窗口位于 id 为“iw”或 id 为“iw_kml”的 div 中)

$('#iw a').live('click', function () {
   $(this)... (Gives you the clicked a-object)

});

现场活动将附加到所有未来的匹配元素。

于 2009-12-07T09:38:53.980 回答
1

我在 Google Map API V3 中尝试了几种解决方案,但无法使其中任何一种正常工作。这是我最近的尝试,似乎有效:

google.maps.event.addListener(mapKmlLayer, 'click', function(kmlEvent) {
  kmlEvent.featureData.description = kmlEvent.featureData.description.gsub("_blank", "_self");
}); 
于 2010-07-07T21:21:00.457 回答
0

我找到了一个更简单的解决方案,只需在链接中添加一个 onclick 行为:

onclick='return false;'

您的链接将自动更改为target="_self"

但是如果您想更改为其他目标,或者 pheraps 删除属性,您应该添加一个侦听器和一个 javascript 替换,如下所示:

  GEvent.addListener(map,"infowindowprepareopen", function(iwtabs) {
  iwtabs[0].contentElem.innerHTML = iwtabs[0].contentElem.innerHTML.replace("_blank", "_parent");
  });

当您在信息窗口框中有一个灯箱(或类似)链接时,这非常有用。

干杯

于 2010-01-14T09:26:03.757 回答