| 1 |
(function($) { |
| 2 |
|
| 3 |
var maps = []; |
| 4 |
|
| 5 |
var CMBGmapsInit = function( fieldEl ) { |
| 6 |
var search = $('#_map_address'); |
| 7 |
var searchInput = search.get(0); |
| 8 |
var mapCanvas = $('.map', fieldEl ).get(0); |
| 9 |
var latitude = $('._geolocation_lat', fieldEl ); |
| 10 |
var longitude = $('._geolocation_long', fieldEl ); |
| 11 |
var elevation = $('._geolocation_elevation', fieldEl ); |
| 12 |
var elevator = new google.maps.ElevationService(); |
| 13 |
var map_type = $('[name="_map_type"]'); |
| 14 |
var map_zoom = $('[name="_map_zoom"]'); |
| 15 |
var streetview = $('[name="_map_no_streetview"]'); |
| 16 |
// Set map options |
| 17 |
var mapOptions = { |
| 18 |
center: new google.maps.LatLng( CMBGmaps.latitude, CMBGmaps.longitude ), |
| 19 |
zoom: parseInt( CMBGmaps._map_zoom ), |
| 20 |
scrollwheel: (CMBGmaps.scrollwheel == "true"), |
| 21 |
streetViewControl: (CMBGmaps._map_no_streetview != "on") |
| 22 |
}; |
| 23 |
|
| 24 |
// Create map |
| 25 |
var map = new google.maps.Map( mapCanvas, mapOptions ); |
| 26 |
|
| 27 |
// Set initial map type |
| 28 |
setMapType( CMBGmaps._map_type ); |
| 29 |
|
| 30 |
// Set marker options |
| 31 |
var markerOptions = { |
| 32 |
map: map, |
| 33 |
draggable: true, |
| 34 |
title: CMBGmaps.markerTitle |
| 35 |
}; |
| 36 |
|
| 37 |
// Create new marker |
| 38 |
var marker = new google.maps.Marker( markerOptions ); |
| 39 |
marker.setPosition( mapOptions.center ); |
| 40 |
|
| 41 |
// Set stored Coordinates |
| 42 |
// if ( latitude.val() && longitude.val() ) { |
| 43 |
// latLng = new google.maps.LatLng( latitude.val(), longitude.val() ); |
| 44 |
// setPosition( latLng, parseInt( CMBGmaps._map_zoom )) |
| 45 |
// } |
| 46 |
|
| 47 |
// Click on map sets location |
| 48 |
window.google.maps.event.addListener( map, 'click', function ( event ) { |
| 49 |
setPosition( event.latLng ); |
| 50 |
setSearchInput( event.latLng ); |
| 51 |
} ); |
| 52 |
// Drag marker sets location |
| 53 |
window.google.maps.event.addListener( marker, 'dragend', function() { |
| 54 |
setPosition( marker.getPosition() ); |
| 55 |
setSearchInput( marker.getPosition() ); |
| 56 |
}); |
| 57 |
|
| 58 |
|
| 59 |
// Search with autocomplete |
| 60 |
var autocomplete = new google.maps.places.Autocomplete(searchInput); |
| 61 |
autocomplete.bindTo('bounds', map); |
| 62 |
|
| 63 |
// Set location after autocomplete |
| 64 |
google.maps.event.addListener(autocomplete, 'place_changed', function() { |
| 65 |
var place = autocomplete.getPlace(); |
| 66 |
setPosition( place.geometry.location ); |
| 67 |
}); |
| 68 |
|
| 69 |
$(searchInput).keypress(function(e) { |
| 70 |
if (e.keyCode === 13) { |
| 71 |
e.preventDefault(); |
| 72 |
} |
| 73 |
}); |
| 74 |
|
| 75 |
// Change map type |
| 76 |
map_type.change(function() { |
| 77 |
var map_type_val = map_type.filter(':checked').val(); |
| 78 |
setMapType( map_type_val ); |
| 79 |
}); |
| 80 |
|
| 81 |
// Change zoom level |
| 82 |
map_zoom.change(function() { |
| 83 |
var map_zoom_val = $('option:selected',this).val(); |
| 84 |
map.setZoom( parseInt(map_zoom_val) ); |
| 85 |
}); |
| 86 |
|
| 87 |
// Toggle streetview |
| 88 |
streetview.change(function() { |
| 89 |
if($(this).is(":checked")) { |
| 90 |
map.setOptions({ streetViewControl: false }); |
| 91 |
} else { |
| 92 |
map.setOptions({ streetViewControl: true }); |
| 93 |
} |
| 94 |
}); |
| 95 |
|
| 96 |
// Change latitude field manually |
| 97 |
$(document).on('keyup', '#_geolocation_lat', function(e) { |
| 98 |
updatePosition(); |
| 99 |
}); |
| 100 |
$(document).on('keyup', '#_geolocation_long', function(e) { |
| 101 |
updatePosition(); |
| 102 |
}); |
| 103 |
function updatePosition(){ |
| 104 |
var lat = parseFloat(document.getElementById("_geolocation_lat").value); |
| 105 |
var lng = parseFloat(document.getElementById("_geolocation_long").value); |
| 106 |
|
| 107 |
if (lat && lng) { |
| 108 |
var mapCanvas = $('.map', fieldEl ).get(0); |
| 109 |
// Set map options |
| 110 |
var mapOptions = { |
| 111 |
center: new google.maps.LatLng( lat, lng ), |
| 112 |
zoom: parseInt( CMBGmaps._map_zoom ), |
| 113 |
scrollwheel: (CMBGmaps.scrollwheel == "true"), |
| 114 |
streetViewControl: (CMBGmaps._map_no_streetview != "on") |
| 115 |
}; |
| 116 |
// Create map |
| 117 |
var map = new google.maps.Map( mapCanvas, mapOptions ); |
| 118 |
// Set marker options |
| 119 |
var markerOptions = { |
| 120 |
map: map, |
| 121 |
draggable: true, |
| 122 |
title: CMBGmaps.markerTitle |
| 123 |
}; |
| 124 |
// Create new marker |
| 125 |
var marker = new google.maps.Marker( markerOptions ); |
| 126 |
marker.setPosition( { lat: parseFloat(lat), lng: parseFloat(lng)} ); |
| 127 |
|
| 128 |
latitude.val( lat ); |
| 129 |
longitude.val( lng ); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
// Set marker position |
| 134 |
function setPosition( latLng, zoom ) { |
| 135 |
|
| 136 |
marker.setPosition( latLng, CMBGmaps.zoom ); |
| 137 |
|
| 138 |
setTimeout(function() { |
| 139 |
map.panTo(marker.getPosition()); |
| 140 |
}, 500); |
| 141 |
|
| 142 |
if ( zoom ) { |
| 143 |
map.setZoom( zoom ); |
| 144 |
} |
| 145 |
|
| 146 |
latitude.val( latLng.lat() ); |
| 147 |
longitude.val( latLng.lng() ); |
| 148 |
|
| 149 |
elevator.getElevationForLocations( { locations: [ marker.getPosition() ] }, function (results, status) { |
| 150 |
if (status == google.maps.ElevationStatus.OK && results[0] ) { |
| 151 |
elevation.val( results[0].elevation ); |
| 152 |
} |
| 153 |
}); |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
// Set search input |
| 158 |
function setSearchInput( latLngPos ) { |
| 159 |
var latlng = new google.maps.LatLng( latLngPos.lat(), latLngPos.lng() ); |
| 160 |
geocoder = new google.maps.Geocoder(); |
| 161 |
|
| 162 |
geocoder.geocode({'latLng': latlng}, function(results, status) { |
| 163 |
if (status == google.maps.GeocoderStatus.OK) { |
| 164 |
if (results[0]) { |
| 165 |
search.val(results[0].formatted_address); |
| 166 |
} else { |
| 167 |
search.val('invalid address'); |
| 168 |
} |
| 169 |
} else { |
| 170 |
//alert('Geocoder failed due to: ' + status); |
| 171 |
} |
| 172 |
}); |
| 173 |
} |
| 174 |
|
| 175 |
// Set map type |
| 176 |
function setMapType( mapType ) { |
| 177 |
if( mapType == 'ROADMAP' ) { map.setMapTypeId(google.maps.MapTypeId.ROADMAP); } |
| 178 |
if( mapType == 'SATELLITE' ) { map.setMapTypeId(google.maps.MapTypeId.SATELLITE); } |
| 179 |
if( mapType == 'HYBRID' ) { map.setMapTypeId(google.maps.MapTypeId.HYBRID); } |
| 180 |
if( mapType == 'TERRAIN' ) { map.setMapTypeId(google.maps.MapTypeId.TERRAIN); } |
| 181 |
} |
| 182 |
|
| 183 |
maps.push( map ); |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
$( '.cmb-type-map' ).each(function() { |
| 188 |
CMBGmapsInit( $(this) ); |
| 189 |
}); |
| 190 |
|
| 191 |
|
| 192 |
// Resize map when meta box is opened |
| 193 |
if ( typeof postboxes !== 'undefined' ) { |
| 194 |
postboxes.pbshow = function () { |
| 195 |
var arrayLength = maps.length; |
| 196 |
for (var i = 0; i < arrayLength; i++) { |
| 197 |
var mapCenter = maps[i].getCenter(); |
| 198 |
google.maps.event.trigger(maps[i], 'resize'); |
| 199 |
maps[i].setCenter(mapCenter); |
| 200 |
} |
| 201 |
}; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
}(jQuery)); |