Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script type="text/javascript">
- (function($) {
- /*
- * new_map
- *
- * This function will render a Google Map onto the selected jQuery element
- *
- * @type function
- * @date 8/11/2013
- * @since 4.3.0
- *
- * @param $el (jQuery element)
- * @return n/a
- */
- function offsetCenter(map, latlng, offsetx, offsety) {
- var scale = Math.pow(2, map.getZoom());
- var worldCoordinateCenter = map.getProjection().fromLatLngToPoint(latlng);
- var pixelOffset = new google.maps.Point((offsetx/scale) || 0,(offsety/scale) ||0)
- var worldCoordinateNewCenter = new google.maps.Point(
- worldCoordinateCenter.x - pixelOffset.x,
- worldCoordinateCenter.y + pixelOffset.y
- );
- var newCenter = map.getProjection().fromPointToLatLng(worldCoordinateNewCenter);
- map.setCenter(newCenter);
- }
- function new_map( $el ) {
- // var
- var $markers = $el.find('.marker');
- // vars
- var args = {
- zoom : 16, //default zoom
- center : new google.maps.LatLng(0, 0),
- mapTypeControl: false, //disable map view
- streetViewControl: false, //disable street view
- mapTypeId : google.maps.MapTypeId.SATELLITE
- };
- // create map
- var map = new google.maps.Map( $el[0], args);
- // add a markers reference
- map.markers = [];
- // add markers
- $markers.each(function(index){
- add_marker( $(this), map, index);
- });
- // center map based on all markers
- //center_map( map );
- // center map to first marker position
- center_map_first_marker(map)
- // return
- return map;
- }
- /*
- * add_marker
- *
- * This function will add a marker to the selected Google Map
- *
- * @type function
- * @date 8/11/2013
- * @since 4.3.0
- *
- * @param $marker (jQuery element)
- * @param map (Google Map object)
- * @return n/a
- */
- function add_marker( $marker, map, index ) {
- // var
- var latlng = new google.maps.LatLng( $marker.attr('data-lat'), $marker.attr('data-lng') );
- var eventType = is_touch_device() ? 'click' : 'mouseover';
- // if marker contains HTML, add it to an infoWindow
- if( $marker.html() ) {
- var infowindow = new google.maps.InfoWindow({ content: $marker.html()});
- var marker = new google.maps.Marker({
- position: latlng,
- map: map<?php if( $default_marker_icon ): ?>,
- icon: '<?php echo $default_marker_icon; ?>'<?php endif; ?>,
- infowindow: infowindow
- });
- // show info window when marker is hovered over
- google.maps.event.addListener(marker, eventType, function() {
- hideAllInfoWindows(map);
- this.infowindow.open( map, marker );
- });
- if (index === 0 ) {
- marker.infowindow.open( map, marker );
- }
- } else {
- var marker = new google.maps.Marker({
- position : latlng,
- map : map<?php if( $default_marker_icon ): ?>,
- icon: '<?php echo $default_marker_icon; ?>'<?php endif; ?>
- });
- }
- // add to array
- map.markers.push( marker );
- }
- function hideAllInfoWindows(map) {
- map.markers.forEach(function(marker) {
- if (!!marker.infowindow) {
- marker.infowindow.close(map, marker);
- }
- });
- }
- /*
- * center_map
- *
- * This function will center the map, showing all markers attached to this map
- *
- * @type function
- * @date 8/11/2013
- * @since 4.3.0
- *
- * @param map (Google Map object)
- * @return n/a
- */
- function center_map( map ) {
- // vars
- var bounds = new google.maps.LatLngBounds();
- // loop through all markers and create bounds
- $.each( map.markers, function( i, marker ){
- var latlng = new google.maps.LatLng( marker.position.lat(), marker.position.lng() );
- bounds.extend( latlng );
- });
- // only 1 marker?
- if( map.markers.length === 1 )
- {
- // set center of map
- map.setCenter( bounds.getCenter() );
- map.setZoom( 16 ); //zoom for single marker case, if you use perv function
- }
- else
- {
- // fit to bounds
- map.fitBounds( bounds );
- }
- }
- function center_map_first_marker( map ) {
- // vars
- if( map.markers.length) {
- var marker = map.markers[0];
- var latlng = new google.maps.LatLng( marker.position.lat(), marker.position.lng() );
- // set center of map
- // map.setCenter( latlng );
- offsetCenter(map, latlng, 0 200);
- map.setZoom( 14 );
- }
- }
- function is_touch_device() {
- return (('ontouchstart' in window)
- || (navigator.MaxTouchPoints > 0)
- || (navigator.msMaxTouchPoints > 0));
- }
- /*
- * document ready
- *
- * This function will render each map when the document is ready (page has loaded)
- *
- * @type function
- * @date 8/11/2013
- * @since 5.0.0
- *
- * @param n/a
- * @return n/a
- */
- // global var
- var map = null;
- $(document).ready(function(){
- $('.acf-map').each(function(){
- // create map
- map = new_map( $(this) );
- });
- });
- })(jQuery);
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement