Created
March 30, 2017 19:15
-
-
Save jetfire21/2b0fab08ee4287cb9dd5790e09da19d7 to your computer and use it in GitHub Desktop.
A bit sloppy implimentation of Google Maps, MarkerClusters.js, and Wordpress location custom fields.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Grabbing locations depending on the page | |
if ( is_page( 'facilities' ) ) { | |
$metaValue = 'facility'; | |
} elseif ( is_page( 'dropoff' ) ) { | |
$metaValue = "dropoff"; | |
} else { | |
$metaValue = "event"; | |
} | |
$args = array( | |
'post_type' => 'location', | |
'meta_key' => '_location_type', | |
'meta_value' => $metaValue | |
); | |
$location_query = new WP_Query( $args ); | |
$location_array = array(); | |
if ( $location_query->have_posts() ) : while ( $location_query->have_posts() ) : $location_query->the_post(); | |
$loc_lat = get_post_meta( $location_query->post->ID, '_location_lat' ); | |
$loc_name = get_post_meta( $location_query->post->ID, '_location_name' ); | |
$loc_lng = get_post_meta( $location_query->post->ID, '_location_lng' ); | |
$loc_addr = get_post_meta( $location_query->post->ID, '_location_address' ); | |
$loc_type = get_post_meta( $location_query->post->ID, '_location_type' ); | |
$loc_desc = get_post_meta( $location_query->post->ID, '_description' ); | |
//Shove into array | |
$loc_array[] = array( | |
'name' => $loc_name, | |
'lat' => $loc_lat, | |
'lng' => $loc_lng, | |
'addr' => $loc_addr, | |
'type' => $loc_type, | |
'desc' => $loc_desc | |
); | |
endwhile; endif; | |
wp_reset_postdata(); | |
?> | |
<script src='https://maps.googleapis.com/maps/api/js?key=API_KEY&sensor=false&extension=.js'></script> | |
<script> | |
google.maps.event.addDomListener(window, 'load', init); | |
var map; | |
function init() { | |
var mapOptions = { | |
//center: new google.maps.LatLng(39.690281,-92.695314), | |
zoom: 5, | |
map: map, | |
zoomControl: false, | |
disableDoubleClickZoom: false, | |
mapTypeControl: false, | |
scaleControl: false, | |
scrollwheel: false, | |
panControl: false, | |
streetViewControl: false, | |
draggable: false, | |
overviewMapControl: false, | |
overviewMapControlOptions: { | |
opened: false, | |
}, | |
mapTypeId: google.maps.MapTypeId.ROADMAP, | |
} | |
var mapElement = document.getElementById('vt-locations'); | |
var map = new google.maps.Map(mapElement, mapOptions); | |
var image = '<?php echo get_template_directory_uri(); ?>/css/images/green-marker.png'; | |
var bounds = new google.maps.LatLngBounds(); | |
var iw = new google.maps.InfoWindow(); | |
var locations = <?php echo json_encode($loc_array); ?>; | |
var markers = []; | |
for (i = 0; i < locations.length; i++) { | |
marker = new google.maps.Marker({ | |
icon: image, | |
position: new google.maps.LatLng(locations[i].lat[0], locations[i].lng[0]), | |
title: locations[i].name[0], | |
address: locations[i].addr[0], | |
desc: locations[i].desc[0] | |
}); | |
//Setting bounding boxes and starting clusters | |
bounds.extend(marker.position); | |
markers.push(marker); | |
<?php if(is_page('facilities')) { ?> | |
google.maps.event.addListener(marker, 'click', function () { | |
var html = "<div style='color:#000;background-color:#fff;padding:5px;width:150px;'><h4>" + this.title + "</h4><p>" + this.address + "<p><p>" + this.desc + "<p></div>"; | |
iw.setContent(html); | |
iw.open(map, this); | |
}); | |
<?php } ?> | |
} | |
//Clusters and bounds | |
var clusterStyles = [ | |
{ | |
url: '<?php echo get_template_directory_uri(); ?>/css/images/green-marker.png', | |
height: 32, | |
width: 24, | |
textColor: '#000000', | |
textSize: 18 | |
} | |
] | |
var markerCluster = new MarkerClusterer(map, markers, { | |
styles: clusterStyles | |
}); | |
map.fitBounds(bounds); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment