GPS位置保存在两个post meta字段上:geo_latitude 和geo_longitude.
您可以使用这些元值在Google地图上绘制帖子作为标记。
例如,使用Google Maps API,您可以有如下模板:
<h1>Posts on map</h1>
<div id="map" style="height: 400px;"></div>
<script>
// init map with the values you need
function initMap() {
var map = new google.maps.Map(document.getElementById(\'map\'), {
zoom: 1,
center: {lat: 0, lng: 0}
});
// get all posts that have meta_key geo_latitude
<?php $query = new WP_Query( array( \'meta_key\' => \'geo_latitude\' ) ); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php
// get latitude and longitude from post meta
$geo_latitude = get_post_meta( get_the_ID(), \'geo_latitude\', true );
$geo_longitude = get_post_meta( get_the_ID(), \'geo_longitude\', true );
?>
<?php if(!empty( $geo_latitude ) && !empty( $geo_longitude )): ?>
var pos = {lat: <?php echo $geo_latitude; ?>, lng: <?php echo $geo_longitude; ?>};
var marker = new google.maps.Marker({
position: pos,
map: map,
title: \'<?php the_title(); ?>\'
});
<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY&callback=initMap">
</script>