我正在使用KML在Google地图上叠加形状。 <description>
元素中包含与每个形状对应的信息,以及指向与该形状对应的详细信息页面的链接。
例如,我的KML包括:
<description>
<![CDATA[
<div>
...
<p>
<a href="Concession.20.aspx">View details</a>
</p>
</div>
]]>
当然,我希望该链接在同一个窗口中打开,因为它只是导航到同一站点上的另一个页面。不幸的是,as documented here,KML文件的<description>
元素中嵌入的链接会被target='_blank'
重写。
包含在内时会忽略目标 HTML直接写入KML; 所有这些链接都打开就好了 target设置为_blank。任何指定的 目标被忽略。
我的问题:任何人都可以想到一种可以覆盖这种(令人讨厌的,恕我直言)行为并强制在同一窗口中打开这些链接的解决方法吗?
作为一种方法的一个示例,我目前正在尝试覆盖这些链接上的click事件(使用jQuery),但它们是由Google地图动态生成的,我似乎无法及早获得它们够了。
答案 0 :(得分:6)
我无法让这些例子起作用。最后,我在jQuery中执行了此操作,只要单击它就会打开链接。
$('#map_canvas').delegate('a', 'click', function(event) {
window.location.href=$(this).attr('href');
return false;
});
答案 1 :(得分: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");
});
答案 2 :(得分: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>
答案 3 :(得分:1)
为了获得这些点击事件,您还可以使用jQuery实时事件:(请注意,Google Map弹出窗口位于div中,ID为“iw”或id为“iw_kml”)
$('#iw a').live('click', function () {
$(this)... (Gives you the clicked a-object)
});
Live events将附加到所有未来的匹配元素。
答案 4 :(得分:1)
我在Google Map API V3中尝试了多种解决方案,但无法让其中任何一种解决方案正常运行。这是我最近的尝试似乎有效:
google.maps.event.addListener(mapKmlLayer, 'click', function(kmlEvent) {
kmlEvent.featureData.description = kmlEvent.featureData.description.gsub("_blank", "_self");
});
答案 5 :(得分: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");
});
当你在infowindow框内有一个灯箱(或类似的)链接时,这是非常有用的。
欢呼声