简体   繁体   English

每个标记的InfoBox(getElementById) - Google Maps API 3

[英]InfoBox (getElementById) for each marker – Google Maps API 3

I am using Google Maps API v3, with InfoBox, in order to style the popups that appear on clicking a marker. 我正在使用带有InfoBox的Google Maps API v3,以便设置单击标记时显示的弹出窗口的样式。

Here's my InfoBox setup: 这是我的InfoBox设置:

// New InfoWindow
infobox = new InfoBox({
    content: document.getElementById('marker-info'),
    disableAutoPan: false,
    pixelOffset: new google.maps.Size(-140, 0),
    zIndex: null,
    closeBoxMargin: '10px 9px 12px 0',
    closeBoxURL: siteURL + 'wp-content/themes/bmk-wp-build/img/close-google-popup.png',
    infoBoxClearance: new google.maps.Size(1, 1)
});

I have a Google Map with many markers, which is being set up with Wordpress using ACF and Locations field. 我有一个带有许多标记的Google Map,它使用ACF和Locations字段与Wordpress一起设置。 All working great but the only issue is the InfoBox is outputting the same content for each marker. 一切正常,但唯一的问题是InfoBox为每个标记输出相同的内容。 This is, clearly, to do with this line: 这显然与这条线有关:

content: document.getElementById('marker-info'),

Here is my PHP/HTML: 这是我的PHP / HTML:

<?php while ( $company_query->have_posts() ) : $company_query->the_post(); ?>
<?php $location = get_field('location'); if ( !empty($location) ) : ?>
    <div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>" id="<?php echo get_the_ID(); ?>">
        <div id="marker-info" class="marker-info <?php echo get_the_ID(); ?>">
            <?php echo wp_get_attachment_image(get_field('logo'), 'full'); ?>
        </div>
    </div>
<?php endif; ?>
<?php endwhile; ?>

Clearly, having an id set in the while loop is a bad idea, and stupid, so I can obviously get rid of that, but how would I change: 很明显,在while循环中设置id是一个坏主意,而且很愚蠢,所以我显然可以摆脱它,但我怎么会改变:

content: document.getElementById('marker-info'),

to get each .marker-info for each InfoBox for each marker? 获取每个标记的每个InfoBox的每个.marker-info?

I worked it out with this... 我用这个来解决这个问题......

google.maps.event.addListener(marker, 'click', function() {
    console.log( $marker.children().html() );

    infobox.open(map,marker);
    infobox.setContent($marker.children().html());

});

Why not use the same InfoBox everywhere, and declare it initially without the content option? 为什么不在任何地方使用相同的InfoBox,并在没有内容选项的情况下最初声明它? If I understand right, you can have an event handler for each marker, and set the callback to use the one infobox. 如果我理解正确,您可以为每个标记设置一个事件处理程序,并将回调设置为使用一个信息框。 Something like the following: 类似于以下内容:

var markers = document.getElementsByClassName('marker');
for( var i = 0; i < markers.length; i++ ) {
    google.maps.event.addDomListener(markers[i], 'click', function() {
         infobox.setContent(this.children[0]);
         infobox.open(map);
    });
}

With this, you can probably get rid of using the ID altogether. 有了这个,你可以完全摆脱使用ID。 If you want to keep the ID, I suggest you move setting the ID inside the class name, to appending it to the actual ID inside the PHP code. 如果你想保留ID,我建议你移动在类名中设置ID,将它附加到PHP代码中的实际ID。

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

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