简体   繁体   English

Google Maps API必须两次单击标记才能动态显示图像

[英]Google Maps API must click on marker twice to dynamically display image

I display many markers on a Google Map. 我在Google地图上显示了许多标记。 Clicking on a marker brings up an infowindow containing text and a small image. 单击标记会弹出一个包含文本和小图像的信息窗口。 The image is dynamically loaded using Jscript. 该图像是使用Jscript动态加载的。 The owner of the server where I get the image insists on dynamic image loading since I could be displaying 100s or 1000s of markers. 获取图像的服务器的所有者坚持动态图像加载,因为我可以显示100或1000多个标记。 The markers were working fine until all of a sudden, I have to click on a marker TWICE in order to have the image displayed. 标记工作正常,直到突然之间,我必须单击TWICE标记才能显示图像。 The text displays immediately but not the image. 文本立即显示,但不显示图像。 I have a default image that is replaced by the dynamically loaded image. 我有一个默认图片,该图片已由动态加载的图片替换。 Before, the default image would briefly display and then be replaced by the dynamically loaded image. 以前,默认图像会短暂显示,然后由动态加载的图像替换。 Now, the default image is displayed until the marker is clicked a 2nd time. 现在,将显示默认图像,直到第二次单击标记。

I should also mention that once the dynamic image has been displayed, you can go back and click on the marker and the dynamic image is displayed the 1st time. 我还要提到的是,一旦显示了动态图像,您就可以返回并单击标记,并且第一次显示动态图像。 Only cached images are dynamically displayed on the first click of the marker. 标记的第一次单击时仅动态显示缓存的图像。

I had been specifying v=3. 我一直在指定v = 3。 I tried eliminating the version number but that does not fix it. 我试图消除版本号,但不能解决该问题。

This exact same code used to display the image immediately when the marker was clicked. 该完全相同的代码用于单击标记时立即显示图像。 Now, you have to click on the marker TWICE. 现在,您必须单击标记TWICE。 I am not getting any console errors. 我没有收到任何控制台错误。

    var currentInfoWindow = null;
    var marker6891957 = new google.maps.Marker({
    position: new google.maps.LatLng(54.3958333, 9.79166666),
    icon: '/images/blue-dot.png',
    map: map,
    title: 'DC6UW'
});
var infowindow6891957 = new google.maps.InfoWindow({
    content: '<b><a href="https://www.qrz.com/db/DC6UW" target=_blank>DC6UW</a></b><br>Fed. Republic of Ger<br>Digital 20m 50.00W<br>Distance: 4159 miles<br>Milliwatts/Mile: 12.02<br><a href="https://s3.amazonaws.com/files.qrz.com/w/dc6uw/100_6713.jpg" target=_blank><img id="id" src="images/qrzimagen.png" height=110></a><br>Click for larger image<br><br>'
});
google.maps.event.addListener(marker6891957, 'click', function() {
    if (currentInfoWindow != null) {
        currentInfoWindow.close();
    }
    infowindow6891957.open(map, marker6891957);
    currentInfoWindow = infowindow6891957;
    $('#id').attr('src', 'https://s3.amazonaws.com/files.qrz.com/w/dc6uw/100_6713.jpg');
});

You are using jQuery to dynamically modify the InfoWindow content. 您正在使用jQuery动态修改InfoWindow内容。 You need to wait for the domready event on the InfoWindow before running that code. 您需要在InfoWindow上等待domready事件,然后再运行该代码。

google.maps.event.addListener(marker6891957, 'click', function() {
    if (currentInfoWindow != null) {
        currentInfoWindow.close();
    }
    infowindow6891957.open(map, marker6891957);
    currentInfoWindow = infowindow6891957;
    google.maps.event.addListenerOnce(currentInfoWindow, 'domready', function() {
       $('#id').attr('src', 'https://cdn.pixabay.com/photo/2015/11/07/11/34/kitten-1031261_960_720.jpg');
    });
});

proof of concept fiddle 概念证明

code snippet: 代码段:

 function initMap() { var map = new google.maps.Map(document.getElementById('map'), { center: new google.maps.LatLng(54.3958333, 9.79166666), zoom: 10 }); var currentInfoWindow = null; var marker6891957 = new google.maps.Marker({ position: new google.maps.LatLng(54.3958333, 9.79166666), // icon: '/images/blue-dot.png', map: map, title: 'DC6UW' }); var infowindow6891957 = new google.maps.InfoWindow({ content: '<b><a href="https://www.qrz.com/db/DC6UW" target=_blank>DC6UW</a></b><br>Fed. Republic of Ger<br>Digital 20m 50.00W<br>Distance: 4159 miles<br>Milliwatts/Mile: 12.02<br><a href="https://cdn.pixabay.com/photo/2015/11/07/11/34/kitten-1031261_960_720.jpg" target=_blank><img id="id" src="images/qrzimagen.png" height=110></a><br>Click for larger image<br><br>' }); google.maps.event.addListener(marker6891957, 'click', function() { if (currentInfoWindow != null) { currentInfoWindow.close(); } infowindow6891957.open(map, marker6891957); currentInfoWindow = infowindow6891957; google.maps.event.addListenerOnce(currentInfoWindow, 'domready', function() { $('#id').attr('src', 'https://cdn.pixabay.com/photo/2015/11/07/11/34/kitten-1031261_960_720.jpg'); }); }); google.maps.event.trigger(marker6891957, 'click'); } 
 html, body, #map { height: 100%; margin: 0; padding: 0; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input id="btn" value="click" type="button" /> <div id="map"></div> <!-- Replace the value of the key parameter with your own API key. --> <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=geometry&callback=initMap" async defer></script> 

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

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