简体   繁体   English

如何为Google Maps标记动画预加载图像

[英]How to preload images for google maps marker animations

I am having an initial flickering problem with a google markers marker animation in Chrome (my version is 51). 我在使用Chrome(我的版本是51)中的Google标记标记动画时遇到了最初的闪烁问题。 The animation works by swapping out image paths referenced in the marker's icon property. 动画通过交换出标记的icon属性中引用的图像路径来工作。 It flickers in it's first iteration because it is loading the additional images for the first time. 它在第一次迭代中闪烁,因为它是第一次加载其他图像。 So to cure that, I added this to my page to preload the images. 因此,为了解决这个问题,我将其添加到页面中以预加载图像。

<div style="display:none">
    <img src="images/greenCar.png" />
    <img src="images/greenCarSigs1.png" />
    <img src="images/greenCarSigs2.png" />
</div>

There are more images, but that is the short version for brevity. 图片更多,但这是简短的缩写。 I added this right before my body tag close. 我在身体标签关闭之前添加了此内容。 So this is supposed to fix the problem, but strangely enough, it does not . 因此,这应该可以解决问题,但奇怪的是,事实并非如此 I even wait and use a setTimeout() of 2 secs inside of $document.ready, so these images should be in cache. 我什至等待并在$ document.ready内部使用2秒钟的setTimeout(),因此这些图像应该在缓存中。

This is happening in Chrome 51.0.2704.103. 这是在Chrome 51.0.2704.103中发生的。 Doesn't happen in Firefox 47.0 version that I have and it works fine. 我拥有的Firefox 47.0版本不会发生这种情况,并且可以正常工作。 So is there something about Chrome where it won't cache images unless they are set to be displayed? 那么,Chrome是否有一些功能,除非将其设置为显示,否则它不会缓存图像? If so, what to do? 如果是这样, 该怎么办? Here's my javscript just in case, but I don't think it has anything to do with the problem or it wouldn't run perfectly from the second iteration on. 这是我的javscript,以防万一,但是我认为这与问题无关,或者从第二次迭代起就无法完美运行。

function tongueAnimation() {
    //if animation has run six or less icon image swaps
    if (runcount < 7) {
        moveTongue();
    }
    else {
        //runcount gets set to 1 for counting handAnimation which will now be called
        runcount = 1;
        orderMarker.setIcon("images/hungry" + runcount + ".png");
        setTimeout(handAnimation, 500);
        //handAnimation();
    }
}

//sets the icon image for the marker
function moveTongue() {
    //images are named hungry1, hungry2 ... so the count decides which image name will be used
    orderMarker.setIcon("images/hungry" + runcount + ".png" );
    //count that fact that moveTongue has been called
    runcount++;
    //call the function that invoked this one
    setTimeout(tongueAnimation, 150);
}

function handAnimation() {
    //if animation has run six or less icon image swaps
    if (runcount < 7) {
        moveHands();
    }
    //else reset runcount to original value of 2 and start over by calling tongueAnimation after three seconds
    else {
        runcount = 2;
        setTimeout(tongueAnimation, 150);
    }
}

function moveHands() {
    if (orderMarker.icon != "images/hungryDown.png") {
    orderMarker.setIcon("images/hungryDown.png");
    }
    else {
       orderMarker.setIcon("images/hungry1.png");
    }
    runcount++;
    setTimeout(handAnimation, 250);
}

$(document).ready(function () {
    setTimeout(tongueAnimation, 2000);
}

Because the HTML you're using is set to display:none, the images never get rendered. 因为您使用的HTML设置为display:none,所以图像永远不会被渲染。 Try instead giving that div a width and height of 1px with the overflow hidden. 尝试改为给div宽度和高度1px,隐藏溢出。

Another possible approach is storing each of your image sources as a variable, then calling that variable instead of re-fetching the image. 另一种可能的方法是将每个图像源存储为一个变量,然后调用该变量而不是重新获取图像。

Try <link rel='prefetch'> or <link rel='preload'> 尝试<link rel='prefetch'><link rel='preload'>

https://css-tricks.com/prefetching-preloading-prebrowsing/ https://css-tricks.com/prefetching-preloading-prebrowsing/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM