Advertisement
Guest User

Untitled

a guest
Aug 21st, 2015
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function ( $ )
  2. {
  3.     'use strict';
  4.  
  5.     // Use function construction to store map & DOM elements separately for each instance
  6.     var MapField = function ( $container )
  7.     {
  8.         this.$container = $container;
  9.     };
  10.  
  11.     // Use prototype for better performance
  12.     MapField.prototype = {
  13.         // Initialize everything
  14.         init              : function ()
  15.         {
  16.             this.initDomElements();
  17.             this.initMapElements();
  18.  
  19.             this.initMarkerPosition();
  20.             this.addListeners();
  21.             this.autocomplete();
  22.         },
  23.  
  24.         // Initialize DOM elements
  25.         initDomElements   : function ()
  26.         {
  27.             this.canvas = this.$container.find( '.rwmb-map-canvas' )[0];
  28.             this.$coordinate = this.$container.find( '.rwmb-map-coordinate' );
  29.             this.$findButton = this.$container.find( '.rwmb-map-goto-address-button' );
  30.             this.addressField = this.$findButton.val();
  31.         },
  32.  
  33.         // Initialize map elements
  34.         initMapElements   : function ()
  35.         {
  36.             var defaultLoc = $( this.canvas ).data( 'default-loc' ),
  37.                 latLng;
  38.  
  39.             defaultLoc = defaultLoc ? defaultLoc.split( ',' ) : [53.346881, -6.258860];
  40.             latLng = new google.maps.LatLng( defaultLoc[0], defaultLoc[1] ); // Initial position for map
  41.  
  42.             this.map = new google.maps.Map( this.canvas, {
  43.                 center           : latLng,
  44.                 zoom             : 14,
  45.                 streetViewControl: 0,
  46.                 mapTypeId        : google.maps.MapTypeId.ROADMAP
  47.             } );
  48.             this.marker = new google.maps.Marker( { position: latLng, map: this.map, draggable: true } );
  49.             this.geocoder = new google.maps.Geocoder();
  50.         },
  51.  
  52.         // Initialize marker position
  53.         initMarkerPosition: function ()
  54.         {
  55.             var coord = this.$coordinate.val(),
  56.                 l,
  57.                 zoom;
  58.  
  59.             if ( coord )
  60.             {
  61.                 l = coord.split( ',' );
  62.                 this.marker.setPosition( new google.maps.LatLng( l[0], l[1] ) );
  63.  
  64.                 zoom = l.length > 2 ? parseInt( l[2], 10 ) : 14;
  65.  
  66.                 this.map.setCenter( this.marker.position );
  67.                 this.map.setZoom( zoom );
  68.             }
  69.             else if ( this.addressField )
  70.             {
  71.                 this.geocodeAddress();
  72.             }
  73.         },
  74.  
  75.         // Add event listeners for 'click' & 'drag'
  76.         addListeners      : function ()
  77.         {
  78.             var that = this;
  79.             google.maps.event.addListener( this.map, 'click', function ( event )
  80.             {
  81.                 that.marker.setPosition( event.latLng );
  82.                 that.updateCoordinate( event.latLng );
  83.             } );
  84.             google.maps.event.addListener( this.marker, 'drag', function ( event )
  85.             {
  86.                 that.updateCoordinate( event.latLng );
  87.             } );
  88.  
  89.             this.$findButton.on( 'click', function ()
  90.             {
  91.                 that.geocodeAddress();
  92.                 return false;
  93.             } );
  94.  
  95.             /**
  96.              * Add a custom event that allows other scripts to refresh the maps when needed
  97.              * For example: when maps is in tabs or hidden div (this is known issue of Google Maps)
  98.              *
  99.              * @see https://developers.google.com/maps/documentation/javascript/reference
  100.              *      ('resize' Event)
  101.              */
  102.             $( window ).on( 'rwmb_map_refresh', function()
  103.             {
  104.                 if ( that.map )
  105.                 {
  106.                     google.maps.event.trigger( that.map, 'resize' );
  107.                 }
  108.             } );
  109.         },
  110.  
  111.         // Autocomplete address
  112.         autocomplete      : function ()
  113.         {
  114.             var that = this;
  115.  
  116.             // No address field or more than 1 address fields, ignore
  117.             if ( !this.addressField || this.addressField.split( ',' ).length > 1 )
  118.             {
  119.                 return;
  120.             }
  121.  
  122.             $( '#' + this.addressField ).autocomplete( {
  123.                 source: function ( request, response )
  124.                 {
  125.                     // TODO: add 'region' option, to help bias geocoder.
  126.                     that.geocoder.geocode( {
  127.                         'address': request.term
  128.                     }, function ( results )
  129.                     {
  130.                         response( $.map( results, function ( item )
  131.                         {
  132.                             return {
  133.                                 label    : item.formatted_address,
  134.                                 value    : item.formatted_address,
  135.                                 latitude : item.geometry.location.lat(),
  136.                                 longitude: item.geometry.location.lng()
  137.                             };
  138.                         } ) );
  139.                     } );
  140.                 },
  141.                 select: function ( event, ui )
  142.                 {
  143.                     var latLng = new google.maps.LatLng( ui.item.latitude, ui.item.longitude );
  144.  
  145.                     that.map.setCenter( latLng );
  146.                     that.marker.setPosition( latLng );
  147.                     that.updateCoordinate( latLng );
  148.                 }
  149.             } );
  150.         },
  151.  
  152.         // Update coordinate to input field
  153.         updateCoordinate  : function ( latLng )
  154.         {
  155.             this.$coordinate.val( latLng.lat() + ',' + latLng.lng() );
  156.         },
  157.  
  158.         // Find coordinates by address
  159.         // Find coordinates by address
  160.         geocodeAddress    : function ()
  161.         {
  162.             var address,
  163.                 addressList = [],
  164.                 fieldList = this.addressField.split( ',' ),
  165.                 loop,
  166.                 that = this;
  167.  
  168.             for ( loop = 0; loop < fieldList.length; loop++ )
  169.             {
  170.                 addressList[loop] = jQuery( '#' + fieldList[loop] ).val();
  171.             }
  172.  
  173.             address = addressList.join( ',' ).replace( /\n/g, ',' ).replace( /,,/g, ',' );
  174.  
  175.             if ( address )
  176.             {
  177.                 this.geocoder.geocode( { 'address': address }, function ( results, status )
  178.                 {
  179.                     if ( status === google.maps.GeocoderStatus.OK )
  180.                     {
  181.                         that.map.setCenter( results[0].geometry.location );
  182.                         that.marker.setPosition( results[0].geometry.location );
  183.                         that.updateCoordinate( results[0].geometry.location );
  184.                     }
  185.                 } );
  186.             }
  187.         }
  188.     };
  189.  
  190.     $( function ()
  191.     {
  192.         $( '.rwmb-map-field' ).each( function ()
  193.         {
  194.             var field = new MapField( $( this ) );
  195.             field.init();
  196.  
  197.             $( this ).data( 'mapController', field );
  198.  
  199.         } );
  200.  
  201.         $( '.rwmb-input' ).on( 'clone', function ()
  202.         {
  203.             $( '.rwmb-map-field' ).each( function ()
  204.             {
  205.                 var field = new MapField( $( this ) );
  206.                 field.init();
  207.  
  208.                 $( this ).data( 'mapController', field );
  209.             } );
  210.         } );
  211.     } );
  212.  
  213. })( jQuery );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement