Created
September 13, 2013 22:34
-
-
Save mattbontrager/6556947 to your computer and use it in GitHub Desktop.
This method takes an address (string), geocodes it through google maps api, processes the results, and packages it up nicely in an object literal.
This file contains 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
GeoKode: { | |
init: function geoKodeInit(addressString) { | |
!!App.development && console.log('in geoKodeInit'); | |
var self = this, submissionString = addressString.replace(/ /g, '+'), | |
geocoder = new google.maps.Geocoder(); | |
geocoder.geocode({'address': submissionString}, function attemptingToGeoCode(results, status) { | |
if (status == google.maps.GeocoderStatus.OK) { | |
console.log('geocode results: ', results); | |
$.each(results, function(i, result) { | |
var components = result.address_components, | |
g = result.geometry, | |
geoLoc = g.location, | |
AddressResult = { | |
latitude: geoLoc.pb, | |
longitude: geoLoc.qb, | |
formatted: result.formatted_address, | |
viewport: g.viewport | |
}; | |
if (g.location_type) { | |
AddressResult.type = g.location_type; | |
} | |
$.each(components, function(j, c) { | |
var types = c.types; | |
$.each(types, function(k, type) { | |
switch (type) { | |
// indicates the floor of a building address | |
case 'floor': | |
if (!AddressResult.Floor) { | |
AddressResult.Floor = {}; | |
} | |
if (c.long_name) { | |
AddressResult.Floor.Long = c.long_name; | |
} | |
if (c.short_name) { | |
AddressResult.Floor.Short = c.short_name; | |
} | |
break; | |
// indicates the precise street number | |
case 'street_number': | |
if (!AddressResult.Street) { | |
AddressResult.Street = {}; | |
} | |
if (!AddressResult.Street.Number) { | |
AddressResult.Street.Number = {}; | |
} | |
if (c.long_name) { | |
AddressResult.Street.Number.Long = c.long_name; | |
} | |
if (c.short_name) { | |
AddressResult.Street.Number.Short = c.short_name; | |
} | |
break; | |
// indicates a precise street address | |
case 'street_address': | |
if (!AddressResult.Street.Address) { | |
AddressResult.Street.Address = {}; | |
} | |
if (c.long_name) { | |
AddressResult.Street.Address.Long = c.long_name; | |
} | |
if (c.short_name) { | |
AddressResult.Street.Address.Short = c.short_name; | |
} | |
break; | |
// indicates a named route (such as "US 101") | |
case 'route': | |
if (!AddressResult.Street.Name) { | |
AddressResult.Street.Name = {}; | |
} | |
if (c.long_name) { | |
AddressResult.Street.Name.Long = c.long_name; | |
} | |
if (c.short_name) { | |
AddressResult.Street.Name.Short = c.short_name; | |
} | |
break; | |
// indicates a major intersection, usually of two major roads | |
case 'intersection': | |
if (!AddressResult.Intersection) { | |
AddressResult.Intersection = {}; | |
} | |
if (c.long_name) { | |
AddressResult.Intersection.Long = c.long_name; | |
} | |
if (c.short_name) { | |
AddressResult.Intersection.Short = c.short_name; | |
} | |
break; | |
// Usually, this type indicates a polygon of some civil administration | |
case 'political': | |
if (!AddressResult.PoliticalArea) { | |
AddressResult.PoliticalArea = {}; | |
} | |
if (c.long_name) { | |
AddressResult.PoliticalArea.Long = c.long_name; | |
} | |
if (c.short_name) { | |
AddressResult.PoliticalArea.Short = c.short_name; | |
} | |
break; | |
// indicates an incorporated city or town political entity | |
case 'locality': | |
if (!AddressResult.City) { | |
AddressResult.City = {}; | |
} | |
if (c.long_name) { | |
AddressResult.City.Long = c.long_name; | |
} | |
if (c.short_name) { | |
AddressResult.City.Short = c.short_name; | |
} | |
break; | |
// indicates a first-order civil entity below a locality | |
case 'sublocality': | |
var longSubLocal = (c.long_name) ? c.long_name: undefined; | |
var shortSubLocal = (c.short_name) ? c.short_name: undefined; | |
break; | |
// indicates a named neightborhood | |
case 'neighborhood': | |
if (!AddressResult.Neighborhood) { | |
AddressResult.Neighborhood = {}; | |
} | |
if (c.long_name) { | |
AddressResult.Neighborhood.Long = c.long_name; | |
} | |
if (c.short_name) { | |
AddressResult.Neighborhood.Short = c.short_name; | |
} | |
break; | |
// indicates a named location, usually a building or collection of buildings with a common name | |
case 'premise': | |
if (!AddressResult.BuildingGroup) { | |
AddressResult.BuildingGroup = {}; | |
} | |
if (c.long_name) { | |
AddressResult.BuildingGroup.Long = c.long_name; | |
} | |
if (c.short_name) { | |
AddressResult.BuildingGroup.Short = c.short_name; | |
} | |
break; | |
// indicates a first-order entity below a named location, usually a singular building within a | |
case 'subpremise': | |
if (!AddressResult.Buildings) { | |
AddressResult.Buildings = {}; | |
} | |
if (c.long_name) { | |
AddressResult.Buildings.Long = c.long_name; | |
} | |
if (c.short_name) { | |
AddressResult.Buildings.Short = c.short_name; | |
} | |
break; | |
// indicates a third-order civil entity below the country level | |
case 'administrative_area_level_3': | |
var longMinorCivil = (c.long_name) ? c.long_name: undefined; | |
var shortMinorCivil = (c.short_name) ? c.short_name: undefined; | |
break; | |
// indicates a second-order civil entity below the country level | |
case 'administrative_area_level_2': | |
if (!AddressResult.County) { | |
AddressResult.County = {}; | |
} | |
if (c.long_name) { | |
AddressResult.County.Long = c.long_name; | |
} | |
if (c.short_name) { | |
AddressResult.County.Short = c.short_name; | |
} | |
break; | |
// indicates a first-order civil entity below the country level | |
case 'administrative_area_level_1': | |
if (!AddressResult.State) { | |
AddressResult.State = {}; | |
} | |
if (c.long_name) { | |
AddressResult.State.Long = c.long_name; | |
} | |
if (c.short_name) { | |
AddressResult.State.Short = c.short_name; | |
} | |
break; | |
// indicates a commonly-used alternative name for the entity | |
case 'colloquial_area': | |
if (!AddressResult.CommonName) { | |
AddressResult.CommonName = {}; | |
} | |
if (c.long_name) { | |
AddressResult.CommonName.Long = c.long_name; | |
} | |
if (c.short_name) { | |
AddressResult.CommonName.Short = c.short_name; | |
} | |
break; | |
// indicates the national political entity | |
case 'country': | |
if (!AddressResult.Country) { | |
AddressResult.Country = {}; | |
} | |
if (c.long_name) { | |
AddressResult.Country.Long = c.long_name; | |
} | |
if (c.short_name) { | |
AddressResult.Country.Short = c.short_name; | |
} | |
break; | |
// indicates a postal code as used to address postal mail within the country | |
case 'postal_code': | |
if (!AddressResult.ZipCode) { | |
AddressResult.ZipCode = {}; | |
} | |
if (c.long_name) { | |
AddressResult.ZipCode.Long = c.long_name; | |
} | |
if (c.short_name) { | |
AddressResult.ZipCode.Short = c.short_name; | |
} | |
break; | |
// indicates a prominent natural feature. | |
case 'natural_feature': | |
if (!AddressResult.NaturalFeature) { | |
AddressResult.NaturalFeature = {}; | |
} | |
if (c.long_name) { | |
AddressResult.NaturalFeature.Long = c.long_name; | |
} | |
if (c.short_name) { | |
AddressResult.NaturalFeature.Short = c.short_name; | |
} | |
break; | |
// indicates an airport | |
case 'airport': | |
if (!AddressResult.Airport) { | |
AddressResult.Airport = {}; | |
} | |
if (c.long_name) { | |
AddressResult.Airport.Long = c.long_name; | |
} | |
if (c.short_name) { | |
AddressResult.Airport.Short = c.short_name; | |
} | |
break; | |
// indicates a named park | |
case 'park': | |
if (!AddressResult.Park) { | |
AddressResult.Park = {}; | |
} | |
if (c.long_name) { | |
AddressResult.Park.Long = c.long_name; | |
} | |
if (c.short_name) { | |
AddressResult.Park.Short = c.short_name; | |
} | |
break; | |
// indicates a named point of interest (e.g. Empire State Building) | |
case 'point_of_interest': | |
if (!AddressResult.PointOfInterest) { | |
AddressResult.PointOfInterest = {}; | |
} | |
if (c.long_name) { | |
AddressResult.PointOfInterest.Long = c.long_name; | |
} | |
if (c.short_name) { | |
AddressResult.PointOfInterest.Short = c.short_name; | |
} | |
break; | |
// typically indicates a place that has not yet been categorized | |
case 'establishment': | |
if (!AddressResult.Establishment) { | |
AddressResult.Establishment = {}; | |
} | |
if (c.long_name) { | |
AddressResult.Establishment.Long = c.long_name; | |
} | |
if (c.short_name) { | |
AddressResult.Establishment.Short = c.short_name; | |
} | |
break; | |
// indicates a specific post box | |
case 'post_box': | |
if (!AddressResult.PostBox) { | |
AddressResult.PostBox = {}; | |
} | |
if (c.long_name) { | |
AddressResult.PostBox.Long = c.long_name; | |
} | |
if (c.short_name) { | |
AddressResult.PostBox.Short = c.short_name; | |
} | |
break; | |
// indicates the room of a building address | |
case 'room': | |
if (!AddressResult.Room) { | |
AddressResult.Room = {}; | |
} | |
if (c.long_name) { | |
AddressResult.Room.Long = c.long_name; | |
} | |
if (c.short_name) { | |
AddressResult.Room.Short = c.short_name; | |
} | |
break; | |
// indicates the location of a train or public transit stop | |
case 'train_station': | |
if (!AddressResult.TrainStation) { | |
AddressResult.TrainStation = {}; | |
} | |
if (c.long_name) { | |
AddressResult.TrainStation.Long = c.long_name; | |
} | |
if (c.short_name) { | |
AddressResult.TrainStation.Short = c.short_name; | |
} | |
break; | |
} | |
}); | |
}); | |
!!App.development && console.log('AddressResult: ', AddressResult); | |
}); | |
} else if (status == google.maps.GeocoderStatus.ZERO_RESULTS) { | |
App.Notify.failure('There were no results from your geocoding query.'); | |
} else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) { | |
App.Notify.failure('You are over your geocoding limit.'); | |
} else if (status == google.maps.GeocoderStatus.REQUEST_DENIED) { | |
App.Notify.failure('Your geocode request was denied. Have you set your sensor properly?'); | |
} else if (status == google.maps.GeocoderStatus.INVALID_REQUEST) { | |
App.Notify.failure('Looks like you are missing the address filter for your geocoding request.'); | |
} else if (status == google.maps.GeocoderStatus.UNKNOWN_ERROR) { | |
App.Notify.failure('The google maps api server encountered an error while attempting to process your geocode request.'); | |
} | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment