首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ACF:在前端问题中显示Google地图

ACF:在前端问题中显示Google地图
EN

Stack Overflow用户
提问于 2021-08-18 18:09:47
回答 1查看 393关注 0票数 0

我有一个带有ACF谷歌地图字段的CTP。此字段中的值通过API来自外部。

我设置了正确的API,因为在后端,我可以正确地看到经度、纬度和地址,并且Google Map可以正常工作。

然而,当我尝试按照this向导在前端渲染地图时,我得到的是一个没有标记的地图,并且具有错误的经度和纬度(在海洋中的某个地方)。

在我的functions.php文件中,我有以下内容

代码语言:javascript
复制
//GOOGLE MAPS API KEY
function my_acf_init() {
     
    acf_update_setting('google_api_key', 'MY_API_KEY');
}
 
add_action('acf/init', 'my_acf_init');

在我的CPT模板中,我有这个

代码语言:javascript
复制
<style type="text/css">
.acf-map {
width: 100%;
height: 400px;
border: #ccc solid 1px;
margin: 20px 0;
}

// Fixes potential theme css conflict.
.acf-map img {
   max-width: inherit !important;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY"></script>
<script type="text/javascript">
(function( $ ) {

/**
 * initMap
 *
 * Renders a Google Map onto the selected jQuery element
 *
 * @date    22/10/19
 * @since   5.8.6
 *
 * @param   jQuery $el The jQuery element.
 * @return  object The map instance.
 */
function initMap( $el ) {

// Find marker elements within map.
var $markers = $el.find('.marker');

// Create gerenic map.
var mapArgs = {
    zoom        : $el.data('zoom') || 16,
    mapTypeId   : google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map( $el[0], mapArgs );

// Add markers.
map.markers = [];
$markers.each(function(){
    initMarker( $(this), map );
});

// Center map based on markers.
centerMap( map );

// Return map instance.
return map;
}

/**
 * initMarker
 *
 * Creates a marker for the given jQuery element and map.
 *
 * @date    22/10/19
 * @since   5.8.6
 *
 * @param   jQuery $el The jQuery element.
 * @param   object The map instance.
 * @return  object The marker instance.
 */
function initMarker( $marker, map ) {

// Get position from marker.
var lat = $marker.data('lat');
var lng = $marker.data('lng');
var latLng = {
    lat: parseFloat( lat ),
    lng: parseFloat( lng )
};

// Create marker instance.
var marker = new google.maps.Marker({
    position : latLng,
    map: map
});

// Append to reference for later use.
map.markers.push( marker );

// If marker contains HTML, add it to an infoWindow.
if( $marker.html() ){

    // Create info window.
    var infowindow = new google.maps.InfoWindow({
        content: $marker.html()
    });

    // Show info window when marker is clicked.
    google.maps.event.addListener(marker, 'click', function() {
        infowindow.open( map, marker );
    });
}
}

/**
 * centerMap
 *
 * Centers the map showing all markers in view.
 *
 * @date    22/10/19
 * @since   5.8.6
 *
 * @param   object The map instance.
 * @return  void
 */
function centerMap( map ) {

// Create map boundaries from all map markers.
var bounds = new google.maps.LatLngBounds();
map.markers.forEach(function( marker ){
    bounds.extend({
        lat: marker.position.lat(),
        lng: marker.position.lng()
    });
});

// Case: Single marker.
if( map.markers.length == 1 ){
    map.setCenter( bounds.getCenter() );

// Case: Multiple markers.
} else{
    map.fitBounds( bounds );
}
}

// Render maps on page load.
$(document).ready(function(){
$('.acf-map').each(function(){
    var map = initMap( $(this) );
});
});
})(jQuery);
</script>

这就是我试图在前端渲染地图的方式

代码语言:javascript
复制
<?php 
    $location = get_field('map');
    if( $location ): ?>
        <div class="acf-map" data-zoom="16">
            <div class="marker" data-lat="<?php echo esc_attr($location['lat']); ?>" data-lng="<?php echo esc_attr($location['lng']); ?>"></div>
        </div>
    <?php endif; ?>

这是我的ACF字段。文件名为“map”

代码语言:javascript
复制
<?php $map = get_field( 'map' ); ?>
<?php if ( $map ) : ?>
<?php echo $map['address']; ?>
<?php echo $map['lat']; ?>
<?php echo $map['lng']; ?>
<?php echo $map['zoom']; ?>
<?php $optional_data_keys = array('street_number', 'street_name', 'city', 'state', 'post_code', 'country'); ?>
<?php foreach ( $optional_data_keys as $key ) : ?>
    <?php if ( isset( $map[ $key ] ) ) : ?>
        <?php echo $map[ $key ]; ?>
    <?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>

有什么提示吗?:D

EN

回答 1

Stack Overflow用户

发布于 2021-10-11 13:57:15

上次更新后,我也遇到了同样的问题。对我来说,解决方案是回到旧版本。希望它能在下一次更新中解决。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68837408

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档