And here is the most simple example I can have, a helper function that simply shows a map, without further interaction:
Code:
/**
* @author Ivan Tcholakov <[email protected]>, 2015
* @license The MIT License, http://opensource.org/licenses/MIT
*/
function gmap($latitude, $longitude, $zoom = null, $element_id = null) {
$latitude = (string) $latitude;
$longitude = (string) $longitude;
if ($latitude == '' || $longitude == '') {
return;
}
$zoom = (int) $zoom;
if ($zoom <= 0) {
$zoom = 6;
}
$element_id = (string) $element_id;
if ($element_id == '') {
$element_id = 'map_canvas';
}
$prefix = $element_id.'_';
ob_start();
?><div id="<?php echo $element_id; ?>" class="google-maps"></div>
<script type="text/javascript">
//<![CDATA[
if (typeof google == 'undefined' || typeof google.maps == 'undefined' ) {
document.write('<script type="text/javascript" src="<?php echo is_https() ? 'https:' : 'http:'; ?>//maps.google.com/maps/api/js?sensor=false">\x3C/script>');
}
$(function () {
var <?php echo $prefix; ?>latitude = <?php echo json_encode($latitude); ?>;
var <?php echo $prefix; ?>longitude = <?php echo json_encode($longitude); ?>;
var <?php echo $prefix; ?>zoom = <?php echo json_encode($zoom); ?>;
var <?php echo $prefix; ?>position = new google.maps.LatLng(<?php echo $prefix; ?>latitude, <?php echo $prefix; ?>longitude);
var <?php echo $prefix; ?>options = {
center: <?php echo $prefix; ?>position,
zoom: <?php echo $prefix; ?>zoom,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var <?php echo $prefix; ?>map = new google.maps.Map(document.getElementById('<?php echo $element_id; ?>'), <?php echo $prefix; ?>options);
var <?php echo $prefix; ?>map_marker = new google.maps.Marker({map: <?php echo $prefix; ?>map, position: <?php echo $prefix; ?>position});
});
//]]>
</script>
<?php
$result = ob_get_contents();
ob_end_clean();
return $result;
}