dongrou839975 2018-01-09 23:40
浏览 50

WordPress循环和谷歌地图Api查询

I have created a custom post with custom fields that give me the Lat and Lons of my locations.

I am needing a way to populate a map with all location marker showing for each location.

When a user selects a location the map needs to still show all locations on the map but the selected/active pin needs to change icon. I can only show one location at a time with the approach I have made below:

How the user selects a location:

<?php 
$args = array( 'post_type' => 'office_locations', 'posts_per_page' => -1, 'order_by' => 'title', 'order' => 'ASC'  );
$loop = new WP_Query( $args ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

<option<?php echo (isset($_POST['selectedValue']) && $_POST['selectedValue'] == get_the_title() ? ' selected' : null) ?>><?php echo get_the_title();?></option>

<?php endwhile; ?>

I then get my selection location like so:

<?php
if( $_POST['selectedValue'] == $_POST['selectedValue'] ){
    $location = $_POST['selectedValue'];
}
?>

How the map utilises the data from the loop:

I reopen the loop:

<?php $args = array( 'post_type' => 'office_locations', 'posts_per_page' => 1, 'title' => $location  ); $loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();

$lat = get_field( "lat" );  // Lattitude
$lon = get_field( "lon" ); // Longitude

My Google map markers and mappings:

function initMap() {

var map = new google.maps.Map(document.getElementById('map'), {
   zoom: 8,
   center: {lat: 53.381129, lng: -1.470085}
});

var icon = {
   url: "http://www.clker.com/cliparts/0/V/t/A/W/N/google-maps-gris-hi.png",
   scaledSize: new google.maps.Size(40, 50), // scaled size
   origin: new google.maps.Point(0,0), // origin
   anchor: new google.maps.Point(0, 0) // anchor
};

var <?php echo preg_replace('/\s+/', '', $location); ?> = new google.maps.Marker({
   position: {lat: <?php echo $lat; ?>, lng: <?php echo $lon; ?>},
   map: map,
   icon: icon
});
}

From here I am unsure on how to display all of the markers on the map at once, and then upon selection replacing the active icon/marker with another image.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 BP神经网络控制倒立摆
    • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
    • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
    • ¥30 Unity接入微信SDK 无法开启摄像头
    • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
    • ¥20 cad图纸,chx-3六轴码垛机器人
    • ¥15 移动摄像头专网需要解vlan
    • ¥20 access多表提取相同字段数据并合并
    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算