-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjquery-ui.placepicker.google-maps.js
159 lines (134 loc) · 3.92 KB
/
jquery-ui.placepicker.google-maps.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
( function ( $ ) {
function GoogleMap ( options ) {
var self = this;
self.options = options;
self.zoom[ $.ui.placepicker.zoom.STREET ] = 14;
if ( self.options.click ) {
google.maps.event.addListener( options.map, 'click', function ( e ) {
self.options.click( { lat: e.latLng.lat(), lng: e.latLng.lng() } );
} );
}
}
$.extend( GoogleMap.prototype, $.ui.placepicker.Map.prototype, {
defaults: {
marker: {
visible: false
}
},
panTo: function ( latlng ) {
options.map.panTo( new google.maps.LatLng( latlng.lat, latlng.lng ) );
},
zoom: function ( zoom ) {
options.map.setZoom( this.zoom[ zoom ] );
},
createMarker: function ( options ) {
options = this._normalizeOptions( options );
options._googleMarker = new google.maps.Marker( options );
return options;
},
showMarker: function ( marker, latlng ) {
marker._googleMarker.setPosition( this._getLatLng( latlng ) );
marker._googleMarker.setVisible( true );
},
hideMarker: function ( marker ) {
marker._googleMarker.setVisible( false );
},
updateMarker: function ( marker ) {
// TODO: Update changed properties
//if ( marker.latlng
},
_normalizeOptions: function ( options ) {
options = $.extend( {}, this.defaults.markers, options );
if ( options.latlng ) {
options.latlng = this._getLatLng( options.latlng );
delete options.latlng;
}
options.map = this.options.map;
return options;
},
_getLatLng: function ( latlng ) {
return new google.maps.LatLng( latlng.lat, latlng.lng );
},
getCenter: function () {
var latlng = this.options.map.getCenter();
return {
lat: latlng.lat(),
lng: latlng.lng()
};
}
} );
function GoogleGeocoder ( options ) {
this.options = options;
this._geocoder = new google.maps.Geocoder();
}
$.extend( GoogleGeocoder.prototype, $.ui.placepicker.Geocoder.prototype, {
StatusOK: google.maps.GeocoderStatus.OK,
geocode: function ( query, callback ) {
if ( query.latlng ) {
query.latLng = new google.maps.LatLng( query.latlng.lat,
query.latlng.lng );
delete query.latlng;
}
this._geocoder.geocode( query, callback );
},
locationFromResponse: function ( response ) {
var self = this;
var location;
$.each( response, function () {
location = self.locationFromItem( this );
if ( self.options.placepicker.isCompleteLocation( location ) ) {
return false;
}
} );
return location;
},
locationFromItem: function ( responseItem ) {
var location = {};
var street = {};
$.each( responseItem.address_components, function () {
var nonpol = $.grep( this.types, function ( n, i ) {
return n != 'political';
} );
if ( nonpol.length == 0 ) {
return true;
}
var type = nonpol[0];
switch ( type ) {
case 'street_number':
street.number = this.long_name;
break;
case 'route':
street.name = this.long_name;
break;
case 'locality':
location.city = this.long_name;
break;
// only use this if there isn't already a match; don't know if this is even useful
case 'sublocality':
location.city = location.city || this.long_name;
break;
case 'administrative_area_level_1':
location.province = this.long_name;
break;
case 'country':
location.country = this.long_name;
break;
case 'postal_code':
location.postal_code = this.long_name;
}
if ( street.name && street.number ) {
location.street = street.number + ' ' + street.name;
}
} );
location.latlng = {
lat: responseItem.geometry.location.lat(),
lng: responseItem.geometry.location.lng()
};
return location;
}
} );
$.ui.placepicker.geocoder.push( GoogleGeocoder );
$.ui.placepicker.geocoder[ 'Google' ] = GoogleGeocoder;
$.ui.placepicker.map.push( GoogleMap );
$.ui.placepicker.map[ 'Google' ] = GoogleMap;
} )( jQuery );