最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Map iframe in a tab issue

programmeradmin1浏览0评论

I have embedded a map on the third tab on a page.

Page here > 'Itinerary' tab

If you go straight to the tab while the page is still loading - it loads the map perfectly. However, if you wait until the page has fully loaded and then click on the tab 'Itinerary', the map loads totally zoomed out and not where it should be.

I have tried entering all the details like

<iframe src="" height="500px" width="100%" frameborder="0" .790716,4.166708&z=8&t=m&hl=en-GB&gl=US&mapclient=apiv3&skstate=action:mps_dialog$apiref:1&output=classic></iframe>

but it still won't work for me. I have very little knowledge of Javascript but I'm guessing that it needs to be reloaded on the tab or something to get it to zoom in.

I have embedded a map on the third tab on a page.

Page here > 'Itinerary' tab

If you go straight to the tab while the page is still loading - it loads the map perfectly. However, if you wait until the page has fully loaded and then click on the tab 'Itinerary', the map loads totally zoomed out and not where it should be.

I have tried entering all the details like

<iframe src="http://ridewithgps/routes/1342529/embed" height="500px" width="100%" frameborder="0" http://maps.google/maps?ll=43.790716,4.166708&z=8&t=m&hl=en-GB&gl=US&mapclient=apiv3&skstate=action:mps_dialog$apiref:1&output=classic></iframe>

but it still won't work for me. I have very little knowledge of Javascript but I'm guessing that it needs to be reloaded on the tab or something to get it to zoom in.

Share Improve this question edited Jul 7, 2016 at 10:30 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Jan 2, 2014 at 13:01 CiaránCiarán 511 gold badge2 silver badges10 bronze badges 1
  • Currently your google maps address is sitting in the middle of your iframe tag. It should take place of the src attribute, unless I'm missing something. – Eric Holmes Commented Jan 2, 2014 at 14:50
Add a comment  | 

3 Answers 3

Reset to default 3

You are using iframe in tab structure.So,
1) When page loads fully, the iframe in itenary tab has display none. so it is not loading the zoomed map properly.
2) When you switch to itenary tab before loading completes, iframe is display block i.e visible so it loads the url/map properly.

So for this what you can do is give id to your iframe say "#itenary_map". and reinitiate iframe when itenary tab is clicked as below


var iframe = document.getElementById("itenary_map");
iframe.src = iframe.src;

Update:
You can use below jQuery code in your main.js.

jQuery('#section-tabs-header li:nth-child(3)').click(function() { //click on itenary tab

if( !jQuery( '#itenerary_map' ).find('iframe').hasClass('clicked') ){//Check if its clicked once or else will reload iframe on every click jQuery( '#itenerary_map' ).find('iframe').attr( 'src', function ( i, val ) { return val; });
jQuery( '#itenerary_map' ).find('iframe').addClass( 'clicked' ); // add any class to say its clicked } });

You can modifiy above js, by giving classes or id to your iframe. I've written this in generalised form.

Excellent response - just ran into something similar and worked on it a bit more to make it even more generalized:

jQuery().ready(function($) {
    $('.tabs').tabs();
    $('.tabs li').not(':first').click(function() { 
        tab_id=$(this).children('a').attr('href');
        iframe_id=$(tab_id).children('iframe').attr('id');
        if (!$('#'+iframe_id).hasClass('clicked')) {
            $('#'+iframe_id).attr(  'src', function ( i, val ) { return val; });
            $('#'+iframe_id).addClass( 'clicked' ); // add a class to say its clicked
        }
    });
});

For me, what fix was add a default width style for all iframes in my page:

iframe {
    width: 100% !important;
}
发布评论

评论列表(0)

  1. 暂无评论