Created
July 22, 2015 23:02
-
-
Save barmmie/82a0d96d2b32cfd5c337 to your computer and use it in GitHub Desktop.
Easily parse results from google address autocomplete
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Ex. GoogleParser.getCity(place); -Where place is a google place object returned from autocomplete | |
| var GoogleParser = { | |
| getStreetNumber: function (place) { | |
| var COMPONENT_TEMPLATE = {street_number: 'short_name'}, | |
| streetNumber = this.getAddrComponent(place, COMPONENT_TEMPLATE); | |
| return streetNumber; | |
| }, | |
| getStreet: function (place) { | |
| var COMPONENT_TEMPLATE = {route: 'long_name'}, | |
| street = this.getAddrComponent(place, COMPONENT_TEMPLATE); | |
| return street; | |
| }, | |
| getCity: function (place) { | |
| var COMPONENT_TEMPLATE = {locality: 'long_name'}, | |
| city = this.getAddrComponent(place, COMPONENT_TEMPLATE); | |
| return city; | |
| }, | |
| getState: function (place) { | |
| var COMPONENT_TEMPLATE = {administrative_area_level_1: 'short_name'}, | |
| state = this.getAddrComponent(place, COMPONENT_TEMPLATE); | |
| return state; | |
| }, | |
| getCountryShort: function (place) { | |
| var COMPONENT_TEMPLATE = {country: 'short_name'}, | |
| countryShort = this.getAddrComponent(place, COMPONENT_TEMPLATE); | |
| return countryShort; | |
| }, | |
| getCountry: function (place) { | |
| var COMPONENT_TEMPLATE = {country: 'long_name'}, | |
| country = this.getAddrComponent(place, COMPONENT_TEMPLATE); | |
| return country; | |
| }, | |
| isGeometryExist: function (place) { | |
| return this.isObject(place) && this.isObject(place.geometry); | |
| }, | |
| getLatitude: function (place) { | |
| if (!this.isGeometryExist(place)) return; | |
| return place.geometry.location.A || place.geometry.location.lat; | |
| }, | |
| getLongitude: function (place) { | |
| if (!this.isGeometryExist(place)) return; | |
| return place.geometry.location.F || place.geometry.location.lng; | |
| }, | |
| isObject: function(a){ | |
| if( (typeof a === "object") && (a !== null) ) | |
| { | |
| return true | |
| } | |
| return false; | |
| }, | |
| isGooglePlace: function (place) { | |
| if (!place) | |
| return false; | |
| return !!place.place_id; | |
| }, | |
| getAddrComponent: function (place, componentTemplate) { | |
| var result; | |
| if (!this.isGooglePlace(place)) | |
| return; | |
| for (var i = 0; i < place.address_components.length; i++) { | |
| var addressType = place.address_components[i].types[0]; | |
| if (componentTemplate[addressType]) { | |
| result = place.address_components[i][componentTemplate[addressType]]; | |
| return result; | |
| } | |
| } | |
| return; | |
| }, | |
| getPlaceId: function (place) { | |
| if (!this.isGooglePlace(place)) | |
| return; | |
| return place.place_id; | |
| }, | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment