Skip to content

Instantly share code, notes, and snippets.

@jedateach
Last active December 16, 2015 08:58
Show Gist options
  • Save jedateach/5409398 to your computer and use it in GitHub Desktop.
Save jedateach/5409398 to your computer and use it in GitHub Desktop.
Initial ideas for address autocomplete
<?php
/*
* Provides a way to create, store and render addresses.
*
* Updates to addresses will cause a new address to be created, if it has been used in the past. Otherwise the existing address is updated.
*
*/
class Address extends DataObject{
/**
* @see http://www.endswithsaurus.com/2009/07/lesson-in-address-storage.html for more info on address fields
*/
static $db = array(
"Street" => "VarChar", //Simple / combined version of following fields
"StreetNumber" => "Int",
"StreetNumberSuffix" => "VarChar", //A~Z 1/3 1/2 2/3 3/4 etc
"StreetName" => "VarChar",
"StreetType" => "VarChar", // Street, Road, Place - there are 200+ street types
"StreetDirection" => "VarChar", //N, NE, E, SE, S, SW, W, NW
"AddressType" => "VarChar", // - For example Apartment, Suite, Office, Floor, Building etc.
"AddressTypeIdentifier" => "VarChar", // - For instance the apartment number, suite, office or floor number or building identifier.
"MinorMunicipality" => "VarChar", // (Village/Hamlet/Suburb)
"MajorMunicipality" => "VarChar", //(Town/City)
"Governing District" => "VarChar", //(Province, State, County)
"PostalArea" => "VarChar", //(Postal Code/Zip/Postcode)
"Country" => "VarChar",
"Longitude" => "Decimal",
"Latitude" => "Decimal"
);
/**
* Do the best possible with given data to fill out address with given data.
*/
function create(array $data){
if(isset($data[""])){
}
}
//https://developers.google.com/maps/documentation/geocoding/
static $google_components = array(
'street_address', //indicates a precise street address.
'route', //indicates a named route (such as "US 101").
'intersection', //indicates a major intersection, usually of two major roads.
'political', //indicates a political entity. Usually, this type indicates a polygon of some civil administration.
'country', //indicates the national political entity, and is typically the highest order type returned by the Geocoder.
'administrative_area_level_1', //indicates a first-order civil entity below the country level. Within the United States, these administrative levels are states. Not all nations exhibit these administrative levels.
'administrative_area_level_2', //indicates a second-order civil entity below the country level. Within the United States, these administrative levels are counties. Not all nations exhibit these administrative levels.
'administrative_area_level_3', //indicates a third-order civil entity below the country level. This type indicates a minor civil division. Not all nations exhibit these administrative levels.
'colloquial_area', //indicates a commonly-used alternative name for the entity.
'locality', //indicates an incorporated city or town political entity.
'sublocality', //indicates an first-order civil entity below a locality
'neighborhood', //indicates a named neighborhood
'premise', //indicates a named location, usually a building or collection of buildings with a common name
'subpremise', //indicates a first-order entity below a named location, usually a singular building within a collection of buildings with a common name
'postal_code', //indicates a postal code as used to address postal mail within the country.
'natural_feature', //indicates a prominent natural feature.
'airport', //indicates an airport.
'park', //indicates a named park.
'point_of_interest', //indicates a named point of interest. Typically, these "POI"s are prominent local entities that don't easily fit in another category such as "Empire State Building" or "Statue of Liberty."
//In addition to the above, address components may exhibit the following types:
'post_box', //indicates a specific postal box.
'street_number', //indicates the precise street number.
'floor indicates', //the floor of a building address.
'room indicates', //the room of a building address.'
);
}
//prereq: http://maps.google.com/maps/api/js?sensor=true&libraries=places
var placeSearch, autocomplete;
var component_form = {
'street_number' : 'short_name',
'route' : 'long_name',
'locality' : 'long_name',
'administrative_area_level_1' : 'short_name',
'country' : 'long_name',
'postal_code' : 'short_name'
};
function initialize() {
autocomplete = new google.maps.places.Autocomplete(document
.getElementById('autocomplete'), {
types : [ 'geocode' ]
});
google.maps.event.addListener(autocomplete, 'place_changed', function() {
fillInAddress();
});
}
function fillInAddress() {
var place = autocomplete.getPlace();
//empty fields
for ( var component in component_form) {
document.getElementById(component).value = "";
document.getElementById(component).disabled = false;
}
//map place component data to form field values
for ( var j = 0; j < place.address_components.length; j++) {
var att = place.address_components[j].types[0];
if (component_form[att]) {
var val = place.address_components[j][component_form[att]];
document.getElementById(att).value = val;
}
}
}
//get geo location from browser
function geolocate() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var geolocation = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
autocomplete.setBounds(new google.maps.LatLngBounds(geolocation,
geolocation));
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment