From 2e6b629cb9184c796a6ccb0baabf9b0ff575b359 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Tue, 18 Nov 2014 15:46:06 -0500 Subject: [PATCH] Add google map into theme --- functions.php | 30 ++++++++++++- js/google-map.js | 109 +++++++++++++++++++++++++++++++++++++++++++++++ js/map.xml | 51 ++++++++++++++++++++++ 3 files changed, 188 insertions(+), 2 deletions(-) create mode 100644 js/google-map.js create mode 100644 js/map.xml diff --git a/functions.php b/functions.php index 6268212..91a9254 100755 --- a/functions.php +++ b/functions.php @@ -22,7 +22,7 @@ function glm_page_menu($parent = 0, $class = '') $childs = get_pages('child_of=' . $page->ID); if (count($childs) > 0) { echo '
  • '."\n"; - echo ''.$page->post_title.''."\n"; + echo ''.$page->post_title.''."\n"; echo glm_page_menu($page->ID, 'sub-menu dropdown'); echo '
  • '."\n"; } else { @@ -59,9 +59,35 @@ function preston_feather_scripts() '1.0', true ); + wp_enqueue_script( + 'glm_google_map_code', + 'http://maps.google.com/maps/api/js?sensor=false', + 'jquery', + '1', + true + ); + wp_enqueue_script( + 'glm_google_map', + get_template_directory_uri() . '/js/google-map.js', + 'jquery', + '1', + true + ); } } - +/** + * Output the div for the google map + * + * @param type $w Width of the div + * @param type $h Height of the div + * + * @return type + */ +function glm_google_map($w = '500px', $h = '200px') +{ + $format = '
    Loading...
    '; + return sprintf($format, $w, $h); +} add_action('wp_enqueue_scripts', 'preston_feather_scripts'); ?> \ No newline at end of file diff --git a/js/google-map.js b/js/google-map.js new file mode 100644 index 0000000..13d4746 --- /dev/null +++ b/js/google-map.js @@ -0,0 +1,109 @@ +var Map = { + _map: null, + _latLngBounds: null, + _infoWindow: null, + arrMarkers: [], + + init: function() + { + var canvas = document.getElementById('map-canvas'); + var myOptions = { + zoom: 13, + mapTypeControl: true, + mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}, + navigationControl: true, + navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL}, + mapTypeId: google.maps.MapTypeId.ROADMAP + } + + Map._map = new google.maps.Map(canvas, myOptions); + Map._latLngBounds = new google.maps.LatLngBounds(); + Map._infoWindow = new google.maps.InfoWindow; + + $.get("http://localhost/WordPress/preston/wp-content/plugins/glm-google-map/map.xml", Map._loadData, 'xml'); + }, + + _loadData: function(data) + { + var markers = data.documentElement.getElementsByTagName("marker"); + for (i = 0; i < markers.length; i++) { + var name = markers[i].getAttribute('member_name'); + var street = markers[i].getAttribute('street'); + var city = markers[i].getAttribute('city_name'); + var state = markers[i].getAttribute('state_abb'); + var zip = markers[i].getAttribute('zip'); + var lat = markers[i].getAttribute('lat'); + var lng = markers[i].getAttribute('lng'); + var phone = markers[i].getAttribute('phone'); + var tfree = markers[i].getAttribute('tfree'); + var zIndex = markers[i].getAttribute('zIndex'); + + var point = new google.maps.LatLng( + parseFloat(lat), + parseFloat(lng) + ); + Map._latLngBounds.extend(point); + + var html = '
    '; + html += '

    ' + name + '
    ' + + street + '
    ' + city + ', ' + state + ' ' + zip; + + if (phone != '') { + html += '
    ' + phone; + } + if (tfree != '') { + html += '
    ' + tfree; + } + + html += '

    '; + html += '
    '; + +// html += ' - '; + var marker = new google.maps.Marker({ + title: name, + map: Map._map, + position: point + }); + + Map.arrMarkers.push(marker); + Map._bindInfoWindow(marker, Map._map, Map._infoWindow, html); + } + + Map._map.setCenter( + Map._latLngBounds.getCenter() + ); + Map._map.fitBounds(Map._latLngBounds); + + if (typeof glm_searchMapIconActive !== "undefined" && glm_searchMapIconActive) { + $(".map-link").click(function (){ + var linkId = $(this).attr("rel"); + google.maps.event.trigger(Map.arrMarkers[linkId], "click"); + myAnchor = '#map-canvas'; + window.location = String(window.location).replace(/\#.*$/, "") + myAnchor; + Map._map.setZoom(14); + }); + } + }, + + _bindInfoWindow: function(marker, map, infoWindow, html) + { + google.maps.event.addListener(marker, 'click', function() { + infoWindow.setContent(html); + infoWindow.open(map, marker); + + $('.addToPlanner a:first').click(function(event) { + if ($(this).text() == 'Add To Planner') { + event.preventDefault(); + + $.get($(this).attr('href'), function(data, textstatus) { + $(".trip-list-count").html(data); + $('.addToPlanner a').toggle(); + }); + return false; + } + }); + }); + } +}; + +$(document).ready(Map.init); \ No newline at end of file diff --git a/js/map.xml b/js/map.xml new file mode 100644 index 0000000..9af6f27 --- /dev/null +++ b/js/map.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + \ No newline at end of file -- 2.17.1