How to override target=_blank in KML popups in embedded Google map?

我与影子孤独终老i 提交于 2019-12-04 07:29:51

I couldn't get those examples to work. In the end I did this in jQuery which opens the link as soon as it's clicked.

  $('#map_canvas').delegate('a', 'click', function(event) {
    window.location.href=$(this).attr('href');
    return false;
  });

I've come up with a working solution using jQuery and the map's infowindowopen event. This is in the initialization code for the map:

    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");
    });

This work for me.

<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>

In order to get to those click events, you can also use the jQuery live events: (Note that the Google Map popups are in a div either with the id 'iw' or the id 'iw_kml')

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

});

Live events will attach to all future matching elements.

I tried several solutions in Google Map API V3, but could not make any one of them to work properly. Here is my latest attempt that seems to work:

google.maps.event.addListener(mapKmlLayer, 'click', function(kmlEvent) {
  kmlEvent.featureData.description = kmlEvent.featureData.description.gsub("_blank", "_self");
}); 

i've found a more simple solution, just add an onclick behavior to the link:

onclick='return false;'

your links will change to target="_self" automatically.

but if you want to change to other target, or pheraps remove the attribute you should add a listener and a javascript replace like this:

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

this's very usefull to use when you have a lightbox (or similar) link inside the infowindow box.

cheers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!