1
+ ( function ( ) {
2
+ var geocoder ;
3
+ var map ;
4
+ var markers = { } ;
5
+ var bounds = new google . maps . LatLngBounds ( ) ;
6
+ var mapElement = jQuery ( 'div.js-map' ) ;
7
+
8
+ function initialize ( ) {
9
+ geocoder = new google . maps . Geocoder ( ) ;
10
+ var mapOptions = {
11
+ mapTypeId : google . maps . MapTypeId . ROADMAP ,
12
+ mapTypeControl : true ,
13
+ scaleControl : true ,
14
+ panControl : true ,
15
+ streetViewControl : true ,
16
+ zoomControl : true ,
17
+ keyboardShortcuts : false ,
18
+ draggable : true ,
19
+ zoomControlOptions : {
20
+ style : google . maps . ZoomControlStyle . DEFAULT
21
+ }
22
+ } ;
23
+ map = new google . maps . Map ( mapElement [ 0 ] , mapOptions ) ;
24
+ jQuery ( '.js-scroll-to-map' ) . each ( function ( ) {
25
+ var address = jQuery ( this ) . attr ( 'data-address' ) ;
26
+ addMarker ( address ) ;
27
+ jQuery ( this ) . click ( function ( ) {
28
+ panToMarker ( address ) ;
29
+ } ) ;
30
+ } ) ;
31
+ }
32
+
33
+ function panToMarker ( address ) {
34
+ jQuery ( 'html, body' ) . animate ( {
35
+ scrollTop : mapElement . offset ( ) . top
36
+ } , 500 ) ;
37
+ map . setZoom ( 17 ) ;
38
+ map . panTo ( markers [ address ] . position ) ;
39
+ }
40
+
41
+ function addMarker ( address ) {
42
+ geocoder . geocode ( { 'address' : address } , function ( results , status ) {
43
+ if ( status == google . maps . GeocoderStatus . OK ) {
44
+ var marker = new google . maps . Marker ( {
45
+ map : map ,
46
+ position : results [ 0 ] . geometry . location
47
+ } ) ;
48
+ markers [ address ] = marker ;
49
+ bounds . extend ( marker . getPosition ( ) ) ;
50
+ map . fitBounds ( bounds ) ;
51
+ } else {
52
+ console . warn ( 'Geocode was not successful for the following reason: ' + status ) ;
53
+ }
54
+ } ) ;
55
+ }
56
+
57
+ google . maps . event . addDomListener ( window , 'load' , initialize ) ;
58
+ } ( ) ) ;
0 commit comments