Last active
June 8, 2016 09:09
-
-
Save arashmilani/56abc95038954439def5e096b49b23f8 to your computer and use it in GitHub Desktop.
A simple WordPress shortcode for Google Maps
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 | |
/* | |
Usage example: | |
[googlemap height='300' lat='38.0658495' lng='46.3238727' | |
zoom='17' title='A wonderfull place', icon='../wp-content/themes/my-theme/images/pin.png' disableDefaultUI='true'] | |
*/ | |
function google_map ($atts) { | |
$height = isset($atts['height']) ? $atts['height'] : 300; | |
$height .= 'px'; | |
$rand = '_' . rand(1000001, 9999999); | |
$lat = isset($atts['lat']) ? $atts['lat'] : 38.0658495; | |
$lng = isset($atts['lng']) ? $atts['lng'] : 46.3238727; | |
$zoom = isset($atts['zoom']) ? $atts['zoom'] : 17; | |
$title = isset($atts['title']) ? $atts['title'] : ''; | |
$icon = isset($atts['icon']) ? $atts['icon'] : ''; | |
$disable_default_UI = isset($atts['disabledefaultui']) ? ($atts['disabledefaultui'] === 'true' ? 'true' : 'false') : 'false'; | |
return " | |
<div class='google-map-wrap'> | |
<div id='address-map$rand' style='height: $height' class='google-map'></div> | |
</div> | |
<script async defer | |
src='https://maps.googleapis.com/maps/api/js?v=3.24&callback=renderMap$rand'></script> | |
<script> | |
function renderMap$rand(){ | |
var mapCanvas = document.getElementById('address-map$rand'); | |
if(!mapCanvas) return; | |
var latLang = new google.maps.LatLng($lat, $lng); | |
var mapOptions = { | |
disableDefaultUI: $disable_default_UI, | |
center: latLang, | |
zoom: $zoom, | |
mapTypeId: google.maps.MapTypeId.ROADMAP, | |
scrollwheel: false, | |
styles: [{ | |
stylers: [{ | |
saturation: -100 | |
}] | |
}] | |
} | |
var map = new google.maps.Map(mapCanvas, mapOptions); | |
var marker = new google.maps.Marker({ | |
position: latLang, | |
map: map, | |
title: '$title', | |
icon: '$icon' | |
}); | |
}; | |
</script>"; | |
} | |
add_shortcode( 'googlemap', 'google_map' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment