1- /*! jquery-locationpicker - v0.1.13 - 2016-03-11 */
1+ /*! jquery-locationpicker - v0.1.14 - 2016-09-20 */
22( function ( $ ) {
33 function GMapContext ( domElement , options ) {
44 var _map = new google . maps . Map ( domElement , options ) ;
55 var _marker = new google . maps . Marker ( {
66 position : new google . maps . LatLng ( 54.19335 , - 3.92695 ) ,
77 map : _map ,
88 title : "Drag Me" ,
9- draggable : options . draggable ,
9+ visible : options . markerVisible ,
10+ draggable : options . markerDraggable ,
1011 icon : options . markerIcon !== undefined ? options . markerIcon : undefined
1112 } ) ;
1213 return {
6061 gMapContext . map . panTo ( location ) ;
6162 this . drawCircle ( gMapContext , location , gMapContext . radius , { } ) ;
6263 if ( gMapContext . settings . enableReverseGeocode ) {
63- gMapContext . geodecoder . geocode ( {
64- latLng : gMapContext . location
65- } , function ( results , status ) {
66- if ( status == google . maps . GeocoderStatus . OK && results . length > 0 ) {
67- gMapContext . locationName = results [ 0 ] . formatted_address ;
68- gMapContext . addressComponents = GmUtility . address_component_from_google_geocode ( results [ 0 ] . address_components ) ;
69- }
70- if ( callback ) {
71- callback . call ( this , gMapContext ) ;
72- }
73- } ) ;
64+ this . updateLocationName ( gMapContext , callback ) ;
7465 } else {
7566 if ( callback ) {
7667 callback . call ( this , gMapContext ) ;
8374 longitude : lnlg . lng ( )
8475 } ;
8576 } ,
77+ addressByFormat : function ( addresses , format ) {
78+ var result = null ;
79+ for ( var i = addresses . length - 1 ; i >= 0 ; i -- ) {
80+ if ( addresses [ i ] . types . indexOf ( format ) >= 0 ) {
81+ result = addresses [ i ] ;
82+ }
83+ }
84+ return result || addresses [ 0 ] ;
85+ } ,
86+ updateLocationName : function ( gmapContext , callback ) {
87+ gmapContext . geodecoder . geocode ( {
88+ latLng : gmapContext . marker . position
89+ } , function ( results , status ) {
90+ if ( status == google . maps . GeocoderStatus . OK && results . length > 0 ) {
91+ var address = GmUtility . addressByFormat ( results , gmapContext . settings . addressFormat ) ;
92+ gmapContext . locationName = address . formatted_address ;
93+ gmapContext . addressComponents = GmUtility . address_component_from_google_geocode ( address . address_components ) ;
94+ }
95+ if ( callback ) {
96+ callback . call ( this , gmapContext ) ;
97+ }
98+ } ) ;
99+ } ,
86100 address_component_from_google_geocode : function ( address_components ) {
87101 var result = { } ;
88102 for ( var i = address_components . length - 1 ; i >= 0 ; i -- ) {
116130 }
117131 function updateInputValues ( inputBinding , gmapContext ) {
118132 if ( ! inputBinding ) return ;
119- var currentLocation = GmUtility . locationFromLatLng ( gmapContext . location ) ;
133+ var currentLocation = GmUtility . locationFromLatLng ( gmapContext . marker . position ) ;
120134 if ( inputBinding . latitudeInput ) {
121135 inputBinding . latitudeInput . val ( currentLocation . latitude ) . change ( ) ;
122136 }
145159 }
146160 if ( inputBinding . locationNameInput && gmapContext . settings . enableAutocomplete ) {
147161 var blur = false ;
148- gmapContext . autocomplete = new google . maps . places . Autocomplete ( inputBinding . locationNameInput . get ( 0 ) ) ;
162+ gmapContext . autocomplete = new google . maps . places . Autocomplete ( inputBinding . locationNameInput . get ( 0 ) , gmapContext . settings . autocompleteOptions ) ;
149163 google . maps . event . addListener ( gmapContext . autocomplete , "place_changed" , function ( ) {
150164 blur = false ;
151165 var place = gmapContext . autocomplete . getPlace ( ) ;
195209 }
196210 GmUtility . setPosition ( gmapContext , new google . maps . LatLng ( $ ( this ) . val ( ) , gmapContext . location . lng ( ) ) , function ( context ) {
197211 context . settings . onchanged . apply ( gmapContext . domContainer , [ GmUtility . locationFromLatLng ( context . location ) , context . radius , false ] ) ;
212+ updateInputValues ( gmapContext . settings . inputBinding , gmapContext ) ;
198213 } ) ;
199214 } ) ;
200215 }
205220 }
206221 GmUtility . setPosition ( gmapContext , new google . maps . LatLng ( gmapContext . location . lat ( ) , $ ( this ) . val ( ) ) , function ( context ) {
207222 context . settings . onchanged . apply ( gmapContext . domContainer , [ GmUtility . locationFromLatLng ( context . location ) , context . radius , false ] ) ;
223+ updateInputValues ( gmapContext . settings . inputBinding , gmapContext ) ;
208224 } ) ;
209225 } ) ;
210226 }
293309 var gmapContext = new GMapContext ( this , {
294310 zoom : settings . zoom ,
295311 center : new google . maps . LatLng ( settings . location . latitude , settings . location . longitude ) ,
296- mapTypeId : google . maps . MapTypeId . ROADMAP ,
312+ mapTypeId : settings . mapTypeId ,
297313 mapTypeControl : false ,
314+ styles : settings . styles ,
298315 disableDoubleClickZoom : false ,
299316 scrollwheel : settings . scrollwheel ,
300317 streetViewControl : false ,
301318 radius : settings . radius ,
302319 locationName : settings . locationName ,
303320 settings : settings ,
321+ autocompleteOptions : settings . autocompleteOptions ,
322+ addressFormat : settings . addressFormat ,
304323 draggable : settings . draggable ,
305- markerIcon : settings . markerIcon
324+ markerIcon : settings . markerIcon ,
325+ markerDraggable : settings . markerDraggable ,
326+ markerVisible : settings . markerVisible
306327 } ) ;
307328 $target . data ( "locationpicker" , gmapContext ) ;
308- google . maps . event . addListener ( gmapContext . marker , "dragend" , function ( event ) {
329+ function displayMarkerWithSelectedArea ( ) {
309330 GmUtility . setPosition ( gmapContext , gmapContext . marker . position , function ( context ) {
310331 var currentLocation = GmUtility . locationFromLatLng ( gmapContext . location ) ;
311- context . settings . onchanged . apply ( gmapContext . domContainer , [ currentLocation , context . radius , true ] ) ;
312332 updateInputValues ( gmapContext . settings . inputBinding , gmapContext ) ;
333+ context . settings . onchanged . apply ( gmapContext . domContainer , [ currentLocation , context . radius , true ] ) ;
334+ } ) ;
335+ }
336+ if ( settings . markerInCenter ) {
337+ gmapContext . map . addListener ( "bounds_changed" , function ( ) {
338+ if ( ! gmapContext . marker . dragging ) {
339+ gmapContext . marker . setPosition ( gmapContext . map . center ) ;
340+ updateInputValues ( gmapContext . settings . inputBinding , gmapContext ) ;
341+ }
342+ } ) ;
343+ gmapContext . map . addListener ( "idle" , function ( ) {
344+ if ( ! gmapContext . marker . dragging ) {
345+ displayMarkerWithSelectedArea ( ) ;
346+ }
313347 } ) ;
348+ }
349+ google . maps . event . addListener ( gmapContext . marker , "drag" , function ( event ) {
350+ updateInputValues ( gmapContext . settings . inputBinding , gmapContext ) ;
351+ } ) ;
352+ google . maps . event . addListener ( gmapContext . marker , "dragend" , function ( event ) {
353+ displayMarkerWithSelectedArea ( ) ;
314354 } ) ;
315355 GmUtility . setPosition ( gmapContext , new google . maps . LatLng ( settings . location . latitude , settings . location . longitude ) , function ( context ) {
316356 updateInputValues ( settings . inputBinding , gmapContext ) ;
327367 locationName : "" ,
328368 radius : 500 ,
329369 zoom : 15 ,
370+ mapTypeId : google . maps . MapTypeId . ROADMAP ,
371+ styles : [ ] ,
330372 scrollwheel : true ,
331373 inputBinding : {
332374 latitudeInput : null ,
336378 } ,
337379 enableAutocomplete : false ,
338380 enableAutocompleteBlur : false ,
381+ autocompleteOptions : null ,
382+ addressFormat : "postal_code" ,
339383 enableReverseGeocode : true ,
340384 draggable : true ,
341385 onchanged : function ( currentLocation , radius , isMarkerDropped ) { } ,
342386 onlocationnotfound : function ( locationName ) { } ,
343387 oninitialized : function ( component ) { } ,
344- markerIcon : undefined
388+ markerIcon : undefined ,
389+ markerDraggable : true ,
390+ markerVisible : true
345391 } ;
346392} ) ( jQuery ) ;
0 commit comments