Skip to content

Commit 3e2f245

Browse files
author
Dmitry Berezovsky
committed
Geocoder now returns address components along with formatted address.
1 parent 789fc67 commit 3e2f245

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

locationpicker.jquery.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@
1919
location: _marker.position,
2020
radius: options.radius,
2121
locationName: options.locationName,
22+
addressComponents: {
23+
formatted_address: null,
24+
addressLine1: null,
25+
addressLine2: null,
26+
streetName: null,
27+
streetNumber: null,
28+
city: null,
29+
state: null,
30+
stateOrProvince: null
31+
},
2232
settings: options.settings,
2333
domContainer: domElement,
2434
geodecoder: new google.maps.Geocoder()
@@ -72,6 +82,8 @@
7282
gMapContext.geodecoder.geocode({latLng: gMapContext.location}, function(results, status){
7383
if (status == google.maps.GeocoderStatus.OK && results.length > 0){
7484
gMapContext.locationName = results[0].formatted_address;
85+
gMapContext.addressComponents =
86+
GmUtility.address_component_from_google_geocode(results[0].address_components);
7587
}
7688
if (callback) {
7789
callback.call(this, gMapContext);
@@ -86,8 +98,41 @@
8698
},
8799
locationFromLatLng: function(lnlg) {
88100
return {latitude: lnlg.lat(), longitude: lnlg.lng()}
101+
},
102+
address_component_from_google_geocode: function(address_components) {
103+
var result = {};
104+
for (var i = address_components.length; i>=0; i--) {
105+
var component = address_components[i];
106+
// Postal code
107+
if (component.types.indexOf('postal_code') >= 0) {
108+
result.postalCode = component.short_name;
109+
}
110+
// Street number
111+
else if (component.types.indexOf('street_number') >= 0) {
112+
result.streetNumber = component.short_name;
113+
}
114+
// Street name
115+
else if (component.types.indexOf('route') >= 0) {
116+
result.streetName = component.short_name;
117+
}
118+
// City
119+
else if (component.types.indexOf('sublocality') >= 0) {
120+
result.city = component.short_name;
121+
}
122+
// State \ Province
123+
else if (component.types.indexOf('administrative_area_level_1') >= 0) {
124+
result.stateOrProvince = component.short_name;
125+
}
126+
// State \ Province
127+
else if (component.types.indexOf('country') >= 0) {
128+
result.country = component.short_name;
129+
}
130+
}
131+
result.addressLine1 = [result.streetNumber, result.streetName].join(' ').trim();
132+
result.addressLine2 = '';
133+
return result;
89134
}
90-
}
135+
};
91136

92137
function isPluginApplied(domObj) {
93138
return getContextForElement(domObj) != undefined;

0 commit comments

Comments
 (0)