You are here

location_API.txt in Location 5

This file describes the public API for the CivicSpace location system as defined by 
in the library implemented by "location.inc" and its supporting files.

For a example of this API's usage, please consult "location.module"


FUNCTION SPECIFICATIONS DESCRIBED IN THIS FILE:
----------------------------------------------
  location_nearby_postalcodes_bylocation(): A function that searches for postal codes within a specified search-radius
                  for a specified location.
                  
  location_nearby_postalcodes_bylatlon(): A function that searches for postal codes within a specified search-radius for
                  a specified latitude and longitude.
                  
  location_get_postalcode_data(): A function that takes a (postalcode,country) pair an returns lat/lon, city, province.  This
                  function is meant to replace location_latlon_rough(); see below.
                  
  location_latlon_rough(): A function that returns the latitude and longitude of the specified postal-code/country pair.
                  This latitude and longitude will be of the approximate center of the postal-code's area.  This function
                  will soon be removed from the API as it has been replaced by the more comprehensive
                  location_get_postalcode_data() described above. [TO BE DEPRECATED]
                  
  location_latlon_exact(): A function that returns the latitude and longitude of the given full location.  Typically implemented
                  on top of a web-service. [TO BE IMPLEMENTED]

  location_map_link(): A function that returns, based on the site configuration, either NULL or 1 or more deep-links to mapping
                   web sites for the parameter location array.
  
  location_driving_directions_link(): A function that returns, given 2 locationes, a deep-link to Yahoo! Maps or some other site 
                  that provides driving directions.  The site chosen depends on the implementation at the country-level.
  
  location_form(): A function that generates a form for collecting locative information.  Depending on the parameters,
                  it can collect just the postal code and country or a full location or whatever fields are specified
                  in its parameters.
                  
  location_proximity_form(): A function that generates a form for collecting proximity search parameters.
  
  location_valid(): A function for validating locationes. [TO BE SPECIFIED]
  
  theme_location(): A function that takes in an location and themes it.  (e.g., $output .= theme('location', $location)).
  
  location_distance_between(): A function that, given a pair of lat/lon pairs, returns the distance between them.
  
  location_form2api(): A function that, given an location submitted via a form generated by location_form, converts the
                      submitted location into a form to be used by the rest of the API.
                      
  location_api2form(): A function that, given an location in the standard array format (see "IMPORTANT NOTES" below)
                      returns an location in a format that can be passed to location_form in order to render the
                      form with the appropriate prefilled values.
  
  
"[TO BE SPECIFIED]"   => Function spec has not been completed and may possibly be eliminated from spec.
"[TO BE IMPLEMENTED]" => Function spec exists but is to be implemented in a future release.
"[TO BE DEPRECATED]"  => This function will soon be removed from the API.
----------------------------------------------



IMPORTANT NOTES:
----------------
Formats
---
In the following API, a 'location' is merely an associative array with the following keys:
  'street'      => A string for the street portion of an location
  'additional'  => A string for the additional street portion of an location
  'province'    => An upper-case, standard postal abbreviation of a state/province/territory
  'postal_code' => A string or integer for the postal code
  'country'     => The lower-case of the ISO 3166 two-letter alpha code (e.g., 'us' for U.S., 'ca' for Canada).
  
For locations that are passed to location_form() for the $prefilled_values parameter, the same format applies
except that the 'province' field is the concatenation of the country code, '-', and the province abbreviation.
For example, 'CA' is the value for California for all purposes, but when passing this in a location for prefilled
values, it should be 'us-CA'.  There are two functions to help convert back and forth between these formats:
location_form2api() and location_api2form().  These are described further below.

Delegation
---
With the exception of location_form() and location_proximity_form(), the calls to functions listed here are, in the end,
dispatched to country-specific location libraries which are implemented in country-specific '.inc' files.  For example,
location_latlon_rough(), when given a U.S. location, really returns a result that is determined by call to 
"location_latlon_rough_us()" which is implemented in the file "location.us.inc".

Current Implementation
---
Currently, the only country supported is the United States.  For the future, however, there will be documentation for how to
implement a country-specific include file that can be plugged into the system to support these calls for new countries.  This
scheme will revolve around method signatures for a handful of simple-to-write functions.






++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function location_nearby_postalcodes_bylocation($location, $distance, $distance_unit = 'km');
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Takes an location and a distance and returns an array of all postal-codes (from all countries that are supported)
within the specified distance of the specified location.
 
@param $location 
  An associative array where
    'street'       => the street location
    'additional'   => extra street location or building name or hall or something like that.
    'city'         => a city name
    'province'     => province code as defined by the country specific include file
    'country'      => lower-cased two-letter ISO 3166 code (REQUIRED)
    'postal_code'  => the postal_code
 
@param $distance
  The number portion of the distance; it is forced to an integer ceiling
 
@param $distance_unit 
  The unit of distance being used for the distance being submitted.
  Valid arguments for $distance_unit are 'mile' and 'km'.  If something other than one of
  these unit types is submitted for this argument, it is forced to 'km'.

@return 
  An array where 
    -> the keys are a postive integer ranking of the search result's closeness to the parameter $postal_code
       with 1 being assigned to the nearest postal code
    -> the values are an associative array where 
         'postal_code'   => A postal code that fell within the search-radius given by $distance and $distance_unit.
         'country'       => The two-letter ISO code for the home-country of this particular postal_code search result.
         'city'          => The city to which this postal code belongs.
         'province'      => The province to which this postal code belongs.
         'lon'           => The longitude coordinate of the approximate center of the area covered by 'postal_code'
         'lat'           => The latitude coordinate of the approximate center of the area covered by 'postal_code'
         'distance'      => The number of 'km's or 'mile's that are between the approximate center of the area of 
                            the $postal_code parameter and that of the 'postal_code' in this subarray
         'distance_unit' => The unit of distance specified by 'scalar'



         
         
            
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function location_nearby_postalcodes_bylatlon($lon, $lat, $distance, $distance_unit = 'km');
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Takes a latitude, longitude, and a distance, and returns all postal_codes within 

@param $lon
  A floating point of the longitude coordinate of the search point
 
@param $lat
  A floating point of the latitude coordinate of the search point
 
@param $distance 
  The number portion of the distance; it is forced to an integer ceiling 

@param $distance_unit
  The unit of distance being used for the distance being submitted.
  Valid arguments for $distance_unit are 'mile' and 'km'.  If something other than one of
  these unit types is submitted for this argument, it is forced to 'km'.

@return 
  An array where 
    -> the keys are a contatenation of the country's ISO code and the postal code.  For example, if 
       one of the search results is for postal code "94803" in the United States, the key is then "us94803"
    -> the values are an associative array where 
         'postal_code'   => A postal code that fell within the search-radius given by $distance and $distance_unit.
         'country'       => The two-letter ISO code for the home-country of this particular postal_code search result.
         'lon'           => The longitude coordinate of the approximate center of the area covered by 'postal_code'
         'lat'           => The latitude coordinate of the approximate center of the area covered by 'postal_code'
         'distance'      => The number of 'km's or 'mile's that are between the approximate center of the area of the 
                            $postal_code parameter and that of the 'postal_code' in this array
         'distance_unit' => The unit of distance specified by 'distance'


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function location_get_postalcode_data($location = array());                                            +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Takes a location and uses the combination of postal code and country to return an array that gives the
city, province, and lat/lon dat for that postal code.

@param $location
  An associative array $location where
    'street'       => the street portion of the location
    'additional' => additional street portion of the location
    'province'     => the province, state, or territory
    'country'      => lower-cased two-letter ISO code (REQUIRED)
    'postal_code'  => international postal code (REQUIRED)
    
@return
  NULL if data cannot be found.
  Otherwise, an associative array where
    'lat' => is a floating point of the latitude coordinate of this location
    'lon' => is a floating point of the longitude coordinate of this location
    'city' => is a string for the city this postalcode is in (or the most recognized city at the given postal)
    'province' => the province of the given postal code.         
  
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function location_latlon_rough($location = array());                                                   +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Takes an location and returns a "rough" latitude/longitude pair based on the postal code
data available for the given country.

@param $location
  An associative array $location where
    'street'       => the street portion of the location
    'additional' => additional street portion of the location
    'province'     => the province, state, or territory
    'country'      => lower-cased two-letter ISO code (REQUIRED)
    'postal_code'  => international postal code (REQUIRED)

@return
  NULL if data cannont be found.
  Otherwise, an associative array where
    'lat' => is a floating point of the latitude coordinate of this location
    'lon' => is a floating point of the longitude coordinate of this location



 
    
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function location_latlon_exact($location = array());                                                     +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Currently, this is not a priority until there is an implementable use for exact longitude, 
latitude coordinates for an location.  The idea is that this call will eventually retrieve
information through a web-service.  Whereas location_latlon_rough() returns an approximate
lat/lon pair based strictly on the postal code where this lat/lon pair is pulled from a
database table, this function is intended to send the entire location to a web-service and 
to retrieve exact lat/lon coordinates.

@param $location
  An array where  
    -> the key values are 'street', 'additional', 'province', 'country', 'postal_code'
    -> the values are:
        'street'         => the string representing the street location (REQUIRED)
        'additional'     => the string representing the additional street location portion in the location form
        'city'           => the city name (REQUIRED)
        'province'       => the province code defined in the country-specific include file
        'country'        => the lower-case of the two-letter ISO code (REQUIRED)
        'postal_code'    => the postal-code (REQUIRED)
 
@return
  NULL if the delegated-to function that does the actual look-up does not exist.
  If the appropriate function exists, then this function returns an associative array where
     'lon' => A floating point number for the longitude coordinate of the parameter location
     'lat' => A floating point number for the latitude coordinate of the parameter location
 
 

     
     
     
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function location_map_link($location = array(), $link_text = 'See map');                                 +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Get deep-links to a mapping services such as Yahoo! Maps or MapQuest (actual providers depend on configuration
of location module) given a location.  The call is delegated based on the 'country' value in the $location parameter.

@param $location
  An associative array where
     'street'       => A string representing the street location
     'additional'   => A string for any additional portion of the street location
     'city'         => A string for the city name
     'province'     => The standard postal abbreviation for the province
     'country'      => The two-letter ISO code for the country of the location (REQUIRED)
     'postal_code'  => The international postal code for the location

@return
  NULL if there are not mapping providers configured for the country or if no links could be generated.
  A string of the form "See map: Yahoo! Maps, MapQuest" where Yahoo! Maps and Mapquest are links to the 
  given location and can be replaced with other options (e.g., Google) available in the location module settings.
  The idea is to encode the appropriate parameters as HTTP GET variables to the URL.



  
  
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function location_driving_directions_link($locationA = array(), $locationB = array(), $link_text = 'Get directions');
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Takes two locationes and tries to return a deep-link to driving directions. 

Parameters:
@param $locationA
  An associative array that represents an location where
     'street'       => the street portions of the location
     'additional'   => additional street portion of the location
     'city'         => the city name
     'province'     => the province, state, or territory
     'country'      => lower-cased two-letter ISO code (REQUIRED)
     'postal_code'  => the postal code
 
@param $locationB
  An associative array that represents an location in the same way that
  parameter $locationA does.

@param $link_text
  The text of the HTML link that is to be generated.

@return 
  A deep-link to driving directions on Yahoo! or some other mapping service, if enough fields are filled in the parameters.
  A deep-link to a form for driving directions with information pre-filled if not enough, but some fields are filled in the parameters.
  The empty string if no information is provided (or if so little information is provided that there is no function to which to delegate
  the call.

  We dispatch the call to a country-specific function.  The country-specific function, in this case,
  will be the one reflected by the country parameter of the first function.  We require that
  both locationes supplied have a country field at the minimum.  

  The country-specific functions will ultimately decide, with the parameters given, whether to
  to link to a form for driving directions is provided, where this form will be
  pre-populated with whatever values were available or whether to link directly to the driving
  directions themselves if enough fields are filled for each location.


 
  
  
  
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function location_form($fields = array(), $prefilled_values = array(), $required_fields = array(), $description = '', $form_name = 'location', $function = NULL);
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Generates a Drupal HTML form for collecting locationes.

@param $fields
  An array of values where each value is one of 'street', 'city', 'province', 'postal_code', or 'country'.
  The presence of values in this array determines which fields will be served in the location form generated
  by a call to this function.  If this array is empty, all fields are generated.

@param $prefilled_values
  An associative array where
    -> Each key is one of the location fields: 'street', 'additional', 'city', 'province', 'postal_code', 'country'
    -> Each value is a prefilled value for the given field.                                       ..... ))

@param $required_fields
  An array of values that are required.  Each string can be one of 'street', 'city', 'postal_code', 'province', or 'country'.
  The presence of values in this array determines which fields will be marked as 'required'.  Validation (i.e., making sure
  a required value is actually filled in is the responsibility of the caller)   

@param $description
  A text description of specifically what location is being collected by the form to be served.

@param $form_name
  An additional parameter to help prevent HTML input name collisions.  If the caller is using this
  function to generate more than 1 location form on a page, then the generated name for each HTML input's 
  "name" attribute will go by the value supplied for $form_name.  This parameter is defaulted to 'location' 
  For example, if $form_name == 'xyz' and there is a 'street' field in the form to be served, 
  the "name" attribute for the HTML <input> will be "edit[xyz][street]"

@param $function
  A string that tells location_form() which location API function will be using the location submitted via the
  generated form.  For example, if $function == 'latlon_rough', then the returned location_form (if it includes
  a country field) will only generate a list of countries in the HTML select for which function location_latlon_rough()
  is supported.  To figure out which countries these are, we check to see which of the configured countries have existing 
  functions to support the call.  In this case, we would check to see if there existed a function called "location_latlon_rough_us()"
  before listing the United States in the HTML SELECT for the generated location form.  $function is defaulted to NULL.
  If $function is NULL, the HTML SELECT that is generated will list all countries.
 
@return
  An location form based on the parameters specified.  If the $fields array is empty, then the
  function returns a form in which all possible fields are served as optional form items.


EXAMPLES:

   -> The following call returns a form that only contains fields for a postal_code and country where
      the postal_code is required:
           ---  
           $form = location_form(array('postal_code', 'country'), array(), array('postal_code', 'country'), 'Permanent location')
           ---
      The form returned by this call is generated with calls to Drupal's 'form_' functions:

           $form  = form_textfield('Postal Code', 'location][postal_code', '', 64, 64, NULL, NULL, TRUE);
      -------------------------------------------------------------------------------------------------
   -> The following call returns a form that contains fields for a street, province, and postal_code,
      but the street, city, and province fields are optional while postal_code is required:
           ---
           $form = location_form(array('street', 'city', 'province', 'postal_code'), array(), array('postal_code'));
           ---
   -> The form returned by this call is generated with the following calls to Drupal's 'form_' functions:

           $form  = form_textfield('Street', 'location][street', '', 64, 64);
           $form .= form_textfield('Additional', 'location][additional', '', 64, 64);
           // The 'Additional' field is always and only generated when 'street' is specified as one of the fields.
           // The 'Additional' field is always optional whether or not 'Street' is required.
           $form .= form_textfield('City', 'location][city', '', 64, 64);
           $form .= _location_province_select_options(); // defined below
           $form .= form_textfield('Postal Code', 'location][postal_code', '', 64, 64, NULL, NULL, TRUE);
       ------------------------------------------------------------------------------------------------
   For the following examples, assume we have the following two locationes:
      (1) $locationA = ('street' => '2010 Broadway St', 'city' => 'Redwood City', 'province' => 'CA', 'postal_code' => '94063', 'country' => 'us');
      (2) $locationB = ('street' => '2010 Broadway St', 'city' => 'Redwood City', 'province' => 'us-CA', 'postal_code' => '94063', 'country' => 'us');
    -> The following calls return the exact same form that contains fields for a street, province, postal_code, where prefilled
       values are submitted to the form.

           $form = location_form(array('street', 'city', 'province', 'postal_code', 'country'), $locationB, array('street', 'city', 'province', 'postal_code',  country'), '', 'home_location');
 
           $form = location_form(array('street', 'city', 'province', 'postal_code', 'country'), location_api2form($locationA), array('street', 'city', 'province', 'postal_code', 'country'), '', 'home_location');

    -> The form returned by these call is ultimately generated with the following calls to Drupal's 'form_' functions:

           $form  = textfield('Street', 'home_location][street', '2010 Broadway St.', 64, 64, NULL, NULL, TRUE);
           $form .= textfield('Additional', 'home_location][additional', 'Attn: Ankur Rishi', 64, 64, NULL, NULL, TRUE);
           $form .= textfield('City', 'home_location][city', 'Redwood City', 64, 64, NULL, NULL, TRUE);
           $form .= _location_province_select_options(TRUE, 'us-CA', 'home_location');
           $form .= textfield('Postal Code', 'home_location][postal_code', '94063', 64, 64, NULL, NULL, TRUE);
           $form .= _location_country_select_options(TRUE, 'us', 'home_location');

       Note that in both cases, the first and third argument can take the same array since all the fields are being required.



       
       
       
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
function location_proximity_form($location_form = '', $label = '', $description = '', $prefilled_values = array(), $form_name = 'location');
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
This function generates a form for doing proximity searches within a certain distance
of a specified point.  

Depending on the context within which this function is called, the search-point can either
be user-supplied via the location form that is passed (if one is available) or done within 
a search-point extracted from a contact table or some other location source specified by 
the programmer calling this function.

@param $location_form           
  An optional location form, preferably generated by location_form().  If the script processing this
  form also needs a user-supplied location, this parameter is used to specify a form for collecting the
  search-point about which this search is being done.  If the caller does not supply a form, it is
  assumed that the caller already has a search point in mind and that this will be made clear in
  the $description parameter.

@param $label
  The label you want to apply to the form group that is returned (passed as $legend param to form_group()).
 
@param $description
  A text description of what is being searched for (e.g., 'Find all upcoming events near you.')

@param $prefilled_values
  An associative array for prefilled values for the proximity search parameters, where 
    'distance' => is the prefilled int value to be selected for the distance scalar
    'distance_unit' => is 'km' or 'mile'
 
@param $form_name
  An additional parameter to help prevent HTML input name collisions.  If the caller is using this
  function to generate more than 1 location proximity form on a page, then the generated name for 
  each HTML input's "name" attribute will be $form_name.  Allowing the caller to pass $form_name allows 
  the caller the flexibility of using more than one location proximity search form on one page.

@return
  An HTML form (generated by Drupal form functions) that lets users specify proximity search parameters that include distance,
  the unit of distance, and a search-point if the optional $location_form parameter is passed.  If one is not passed,
  the caller of this function will be assumed to already have one.  


 
 
  

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function theme_location($location = array());                                                            +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Generates HTML for the passed location.

@param $location
  An associative array where
     'street'       => A string representing the street location
     'additional'   => A string for any additional portion of the street location
     'city'         => A string for the city name
     'province'     => The standard postal abbreviation for the province
     'country'      => The two-letter ISO code for the country of the location (REQUIRED)
     'postal_code'  => The international postal code for the location

@return
  An HTML string with special tags for locationes.


  
  

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function location_distance_between($latlonA = array(), $latlonB = array(), $distance_unit = 'km');      +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Given two points in lat/lon form, returns the distance between them.

@param $latlonA
  An associative array where 
     'lon' => is a floating point of the longitude coordinate for the point given by latlonA
     'lat' => is a floating point of the latitude coordinate for the point given by latlonB

@param $latlonB
     Another point formatted like $latlonB

@param $distance_unit
     A string that is either 'km' or 'mile'.
     If neither 'km' or 'mile' is passed, the parameter is forced to 'km'

@return
   NULL if sense can't be made of the parameters.
   An associative array where
     'scalar' => Is the distance between the two lat/lon parameter points 
     'distance_unit' => Is the unit of distance being represented by 'scalar'.
                        This will be 'km' unless 'mile' is passed for the $distance_unit param

 
                        
                        

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function location_form2api($location = array());                                                         +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@param $location
  An associative array that has been submitted by an HTML form generated by location_form().

@return
  An associative array in which the submitted values are modified to pass to the location API
  as the $location parameter (excepting location_form()).

  This means changing the province field to remove the country code and dash.
  For example: California is served by the key 'us-CA' in the location form and this is what is passed when it is
               submitted through a form generated by location_form().

               This is changed to 'CA' in the returned array.

 




++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function location_api2form($location = array());                                                         +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Inverse of location_form2api()

@param $location
  An associative array that can be passed as the $location parameter to the location API.

@return
  An associative array with the same values modified so that the array can be passed as the
  $prefilled_values parameter to location_api2form()

  Meant to take the standard location array format used by the public API (minus the form generating functions) and convert them
  into values that can be used by location_form() to fill in the prefilled values.


 
 
 


 

 
 

File

location_API.txt
View source
  1. This file describes the public API for the CivicSpace location system as defined by
  2. in the library implemented by "location.inc" and its supporting files.
  3. For a example of this API's usage, please consult "location.module"
  4. FUNCTION SPECIFICATIONS DESCRIBED IN THIS FILE:
  5. ----------------------------------------------
  6. location_nearby_postalcodes_bylocation(): A function that searches for postal codes within a specified search-radius
  7. for a specified location.
  8. location_nearby_postalcodes_bylatlon(): A function that searches for postal codes within a specified search-radius for
  9. a specified latitude and longitude.
  10. location_get_postalcode_data(): A function that takes a (postalcode,country) pair an returns lat/lon, city, province. This
  11. function is meant to replace location_latlon_rough(); see below.
  12. location_latlon_rough(): A function that returns the latitude and longitude of the specified postal-code/country pair.
  13. This latitude and longitude will be of the approximate center of the postal-code's area. This function
  14. will soon be removed from the API as it has been replaced by the more comprehensive
  15. location_get_postalcode_data() described above. [TO BE DEPRECATED]
  16. location_latlon_exact(): A function that returns the latitude and longitude of the given full location. Typically implemented
  17. on top of a web-service. [TO BE IMPLEMENTED]
  18. location_map_link(): A function that returns, based on the site configuration, either NULL or 1 or more deep-links to mapping
  19. web sites for the parameter location array.
  20. location_driving_directions_link(): A function that returns, given 2 locationes, a deep-link to Yahoo! Maps or some other site
  21. that provides driving directions. The site chosen depends on the implementation at the country-level.
  22. location_form(): A function that generates a form for collecting locative information. Depending on the parameters,
  23. it can collect just the postal code and country or a full location or whatever fields are specified
  24. in its parameters.
  25. location_proximity_form(): A function that generates a form for collecting proximity search parameters.
  26. location_valid(): A function for validating locationes. [TO BE SPECIFIED]
  27. theme_location(): A function that takes in an location and themes it. (e.g., $output .= theme('location', $location)).
  28. location_distance_between(): A function that, given a pair of lat/lon pairs, returns the distance between them.
  29. location_form2api(): A function that, given an location submitted via a form generated by location_form, converts the
  30. submitted location into a form to be used by the rest of the API.
  31. location_api2form(): A function that, given an location in the standard array format (see "IMPORTANT NOTES" below)
  32. returns an location in a format that can be passed to location_form in order to render the
  33. form with the appropriate prefilled values.
  34. "[TO BE SPECIFIED]" => Function spec has not been completed and may possibly be eliminated from spec.
  35. "[TO BE IMPLEMENTED]" => Function spec exists but is to be implemented in a future release.
  36. "[TO BE DEPRECATED]" => This function will soon be removed from the API.
  37. ----------------------------------------------
  38. IMPORTANT NOTES:
  39. ----------------
  40. Formats
  41. ---
  42. In the following API, a 'location' is merely an associative array with the following keys:
  43. 'street' => A string for the street portion of an location
  44. 'additional' => A string for the additional street portion of an location
  45. 'province' => An upper-case, standard postal abbreviation of a state/province/territory
  46. 'postal_code' => A string or integer for the postal code
  47. 'country' => The lower-case of the ISO 3166 two-letter alpha code (e.g., 'us' for U.S., 'ca' for Canada).
  48. For locations that are passed to location_form() for the $prefilled_values parameter, the same format applies
  49. except that the 'province' field is the concatenation of the country code, '-', and the province abbreviation.
  50. For example, 'CA' is the value for California for all purposes, but when passing this in a location for prefilled
  51. values, it should be 'us-CA'. There are two functions to help convert back and forth between these formats:
  52. location_form2api() and location_api2form(). These are described further below.
  53. Delegation
  54. ---
  55. With the exception of location_form() and location_proximity_form(), the calls to functions listed here are, in the end,
  56. dispatched to country-specific location libraries which are implemented in country-specific '.inc' files. For example,
  57. location_latlon_rough(), when given a U.S. location, really returns a result that is determined by call to
  58. "location_latlon_rough_us()" which is implemented in the file "location.us.inc".
  59. Current Implementation
  60. ---
  61. Currently, the only country supported is the United States. For the future, however, there will be documentation for how to
  62. implement a country-specific include file that can be plugged into the system to support these calls for new countries. This
  63. scheme will revolve around method signatures for a handful of simple-to-write functions.
  64. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  65. function location_nearby_postalcodes_bylocation($location, $distance, $distance_unit = 'km');
  66. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  67. Takes an location and a distance and returns an array of all postal-codes (from all countries that are supported)
  68. within the specified distance of the specified location.
  69. @param $location
  70. An associative array where
  71. 'street' => the street location
  72. 'additional' => extra street location or building name or hall or something like that.
  73. 'city' => a city name
  74. 'province' => province code as defined by the country specific include file
  75. 'country' => lower-cased two-letter ISO 3166 code (REQUIRED)
  76. 'postal_code' => the postal_code
  77. @param $distance
  78. The number portion of the distance; it is forced to an integer ceiling
  79. @param $distance_unit
  80. The unit of distance being used for the distance being submitted.
  81. Valid arguments for $distance_unit are 'mile' and 'km'. If something other than one of
  82. these unit types is submitted for this argument, it is forced to 'km'.
  83. @return
  84. An array where
  85. -> the keys are a postive integer ranking of the search result's closeness to the parameter $postal_code
  86. with 1 being assigned to the nearest postal code
  87. -> the values are an associative array where
  88. 'postal_code' => A postal code that fell within the search-radius given by $distance and $distance_unit.
  89. 'country' => The two-letter ISO code for the home-country of this particular postal_code search result.
  90. 'city' => The city to which this postal code belongs.
  91. 'province' => The province to which this postal code belongs.
  92. 'lon' => The longitude coordinate of the approximate center of the area covered by 'postal_code'
  93. 'lat' => The latitude coordinate of the approximate center of the area covered by 'postal_code'
  94. 'distance' => The number of 'km's or 'mile's that are between the approximate center of the area of
  95. the $postal_code parameter and that of the 'postal_code' in this subarray
  96. 'distance_unit' => The unit of distance specified by 'scalar'
  97. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  98. function location_nearby_postalcodes_bylatlon($lon, $lat, $distance, $distance_unit = 'km');
  99. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  100. Takes a latitude, longitude, and a distance, and returns all postal_codes within
  101. @param $lon
  102. A floating point of the longitude coordinate of the search point
  103. @param $lat
  104. A floating point of the latitude coordinate of the search point
  105. @param $distance
  106. The number portion of the distance; it is forced to an integer ceiling
  107. @param $distance_unit
  108. The unit of distance being used for the distance being submitted.
  109. Valid arguments for $distance_unit are 'mile' and 'km'. If something other than one of
  110. these unit types is submitted for this argument, it is forced to 'km'.
  111. @return
  112. An array where
  113. -> the keys are a contatenation of the country's ISO code and the postal code. For example, if
  114. one of the search results is for postal code "94803" in the United States, the key is then "us94803"
  115. -> the values are an associative array where
  116. 'postal_code' => A postal code that fell within the search-radius given by $distance and $distance_unit.
  117. 'country' => The two-letter ISO code for the home-country of this particular postal_code search result.
  118. 'lon' => The longitude coordinate of the approximate center of the area covered by 'postal_code'
  119. 'lat' => The latitude coordinate of the approximate center of the area covered by 'postal_code'
  120. 'distance' => The number of 'km's or 'mile's that are between the approximate center of the area of the
  121. $postal_code parameter and that of the 'postal_code' in this array
  122. 'distance_unit' => The unit of distance specified by 'distance'
  123. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  124. function location_get_postalcode_data($location = array()); +
  125. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  126. Takes a location and uses the combination of postal code and country to return an array that gives the
  127. city, province, and lat/lon dat for that postal code.
  128. @param $location
  129. An associative array $location where
  130. 'street' => the street portion of the location
  131. 'additional' => additional street portion of the location
  132. 'province' => the province, state, or territory
  133. 'country' => lower-cased two-letter ISO code (REQUIRED)
  134. 'postal_code' => international postal code (REQUIRED)
  135. @return
  136. NULL if data cannot be found.
  137. Otherwise, an associative array where
  138. 'lat' => is a floating point of the latitude coordinate of this location
  139. 'lon' => is a floating point of the longitude coordinate of this location
  140. 'city' => is a string for the city this postalcode is in (or the most recognized city at the given postal)
  141. 'province' => the province of the given postal code.
  142. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  143. function location_latlon_rough($location = array()); +
  144. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  145. Takes an location and returns a "rough" latitude/longitude pair based on the postal code
  146. data available for the given country.
  147. @param $location
  148. An associative array $location where
  149. 'street' => the street portion of the location
  150. 'additional' => additional street portion of the location
  151. 'province' => the province, state, or territory
  152. 'country' => lower-cased two-letter ISO code (REQUIRED)
  153. 'postal_code' => international postal code (REQUIRED)
  154. @return
  155. NULL if data cannont be found.
  156. Otherwise, an associative array where
  157. 'lat' => is a floating point of the latitude coordinate of this location
  158. 'lon' => is a floating point of the longitude coordinate of this location
  159. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  160. function location_latlon_exact($location = array()); +
  161. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  162. Currently, this is not a priority until there is an implementable use for exact longitude,
  163. latitude coordinates for an location. The idea is that this call will eventually retrieve
  164. information through a web-service. Whereas location_latlon_rough() returns an approximate
  165. lat/lon pair based strictly on the postal code where this lat/lon pair is pulled from a
  166. database table, this function is intended to send the entire location to a web-service and
  167. to retrieve exact lat/lon coordinates.
  168. @param $location
  169. An array where
  170. -> the key values are 'street', 'additional', 'province', 'country', 'postal_code'
  171. -> the values are:
  172. 'street' => the string representing the street location (REQUIRED)
  173. 'additional' => the string representing the additional street location portion in the location form
  174. 'city' => the city name (REQUIRED)
  175. 'province' => the province code defined in the country-specific include file
  176. 'country' => the lower-case of the two-letter ISO code (REQUIRED)
  177. 'postal_code' => the postal-code (REQUIRED)
  178. @return
  179. NULL if the delegated-to function that does the actual look-up does not exist.
  180. If the appropriate function exists, then this function returns an associative array where
  181. 'lon' => A floating point number for the longitude coordinate of the parameter location
  182. 'lat' => A floating point number for the latitude coordinate of the parameter location
  183. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  184. function location_map_link($location = array(), $link_text = 'See map'); +
  185. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  186. Get deep-links to a mapping services such as Yahoo! Maps or MapQuest (actual providers depend on configuration
  187. of location module) given a location. The call is delegated based on the 'country' value in the $location parameter.
  188. @param $location
  189. An associative array where
  190. 'street' => A string representing the street location
  191. 'additional' => A string for any additional portion of the street location
  192. 'city' => A string for the city name
  193. 'province' => The standard postal abbreviation for the province
  194. 'country' => The two-letter ISO code for the country of the location (REQUIRED)
  195. 'postal_code' => The international postal code for the location
  196. @return
  197. NULL if there are not mapping providers configured for the country or if no links could be generated.
  198. A string of the form "See map: Yahoo! Maps, MapQuest" where Yahoo! Maps and Mapquest are links to the
  199. given location and can be replaced with other options (e.g., Google) available in the location module settings.
  200. The idea is to encode the appropriate parameters as HTTP GET variables to the URL.
  201. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  202. function location_driving_directions_link($locationA = array(), $locationB = array(), $link_text = 'Get directions');
  203. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  204. Takes two locationes and tries to return a deep-link to driving directions.
  205. Parameters:
  206. @param $locationA
  207. An associative array that represents an location where
  208. 'street' => the street portions of the location
  209. 'additional' => additional street portion of the location
  210. 'city' => the city name
  211. 'province' => the province, state, or territory
  212. 'country' => lower-cased two-letter ISO code (REQUIRED)
  213. 'postal_code' => the postal code
  214. @param $locationB
  215. An associative array that represents an location in the same way that
  216. parameter $locationA does.
  217. @param $link_text
  218. The text of the HTML link that is to be generated.
  219. @return
  220. A deep-link to driving directions on Yahoo! or some other mapping service, if enough fields are filled in the parameters.
  221. A deep-link to a form for driving directions with information pre-filled if not enough, but some fields are filled in the parameters.
  222. The empty string if no information is provided (or if so little information is provided that there is no function to which to delegate
  223. the call.
  224. We dispatch the call to a country-specific function. The country-specific function, in this case,
  225. will be the one reflected by the country parameter of the first function. We require that
  226. both locationes supplied have a country field at the minimum.
  227. The country-specific functions will ultimately decide, with the parameters given, whether to
  228. to link to a form for driving directions is provided, where this form will be
  229. pre-populated with whatever values were available or whether to link directly to the driving
  230. directions themselves if enough fields are filled for each location.
  231. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  232. function location_form($fields = array(), $prefilled_values = array(), $required_fields = array(), $description = '', $form_name = 'location', $function = NULL);
  233. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  234. Generates a Drupal HTML form for collecting locationes.
  235. @param $fields
  236. An array of values where each value is one of 'street', 'city', 'province', 'postal_code', or 'country'.
  237. The presence of values in this array determines which fields will be served in the location form generated
  238. by a call to this function. If this array is empty, all fields are generated.
  239. @param $prefilled_values
  240. An associative array where
  241. -> Each key is one of the location fields: 'street', 'additional', 'city', 'province', 'postal_code', 'country'
  242. -> Each value is a prefilled value for the given field. ..... ))
  243. @param $required_fields
  244. An array of values that are required. Each string can be one of 'street', 'city', 'postal_code', 'province', or 'country'.
  245. The presence of values in this array determines which fields will be marked as 'required'. Validation (i.e., making sure
  246. a required value is actually filled in is the responsibility of the caller)
  247. @param $description
  248. A text description of specifically what location is being collected by the form to be served.
  249. @param $form_name
  250. An additional parameter to help prevent HTML input name collisions. If the caller is using this
  251. function to generate more than 1 location form on a page, then the generated name for each HTML input's
  252. "name" attribute will go by the value supplied for $form_name. This parameter is defaulted to 'location'
  253. For example, if $form_name == 'xyz' and there is a 'street' field in the form to be served,
  254. the "name" attribute for the HTML will be "edit[xyz][street]"
  255. @param $function
  256. A string that tells location_form() which location API function will be using the location submitted via the
  257. generated form. For example, if $function == 'latlon_rough', then the returned location_form (if it includes
  258. a country field) will only generate a list of countries in the HTML select for which function location_latlon_rough()
  259. is supported. To figure out which countries these are, we check to see which of the configured countries have existing
  260. functions to support the call. In this case, we would check to see if there existed a function called "location_latlon_rough_us()"
  261. before listing the United States in the HTML SELECT for the generated location form. $function is defaulted to NULL.
  262. If $function is NULL, the HTML SELECT that is generated will list all countries.
  263. @return
  264. An location form based on the parameters specified. If the $fields array is empty, then the
  265. function returns a form in which all possible fields are served as optional form items.
  266. EXAMPLES:
  267. -> The following call returns a form that only contains fields for a postal_code and country where
  268. the postal_code is required:
  269. ---
  270. $form = location_form(array('postal_code', 'country'), array(), array('postal_code', 'country'), 'Permanent location')
  271. ---
  272. The form returned by this call is generated with calls to Drupal's 'form_' functions:
  273. $form = form_textfield('Postal Code', 'location][postal_code', '', 64, 64, NULL, NULL, TRUE);
  274. -------------------------------------------------------------------------------------------------
  275. -> The following call returns a form that contains fields for a street, province, and postal_code,
  276. but the street, city, and province fields are optional while postal_code is required:
  277. ---
  278. $form = location_form(array('street', 'city', 'province', 'postal_code'), array(), array('postal_code'));
  279. ---
  280. -> The form returned by this call is generated with the following calls to Drupal's 'form_' functions:
  281. $form = form_textfield('Street', 'location][street', '', 64, 64);
  282. $form .= form_textfield('Additional', 'location][additional', '', 64, 64);
  283. // The 'Additional' field is always and only generated when 'street' is specified as one of the fields.
  284. // The 'Additional' field is always optional whether or not 'Street' is required.
  285. $form .= form_textfield('City', 'location][city', '', 64, 64);
  286. $form .= _location_province_select_options(); // defined below
  287. $form .= form_textfield('Postal Code', 'location][postal_code', '', 64, 64, NULL, NULL, TRUE);
  288. ------------------------------------------------------------------------------------------------
  289. For the following examples, assume we have the following two locationes:
  290. (1) $locationA = ('street' => '2010 Broadway St', 'city' => 'Redwood City', 'province' => 'CA', 'postal_code' => '94063', 'country' => 'us');
  291. (2) $locationB = ('street' => '2010 Broadway St', 'city' => 'Redwood City', 'province' => 'us-CA', 'postal_code' => '94063', 'country' => 'us');
  292. -> The following calls return the exact same form that contains fields for a street, province, postal_code, where prefilled
  293. values are submitted to the form.
  294. $form = location_form(array('street', 'city', 'province', 'postal_code', 'country'), $locationB, array('street', 'city', 'province', 'postal_code', country'), '', 'home_location');
  295. $form = location_form(array('street', 'city', 'province', 'postal_code', 'country'), location_api2form($locationA), array('street', 'city', 'province', 'postal_code', 'country'), '', 'home_location');
  296. -> The form returned by these call is ultimately generated with the following calls to Drupal's 'form_' functions:
  297. $form = textfield('Street', 'home_location][street', '2010 Broadway St.', 64, 64, NULL, NULL, TRUE);
  298. $form .= textfield('Additional', 'home_location][additional', 'Attn: Ankur Rishi', 64, 64, NULL, NULL, TRUE);
  299. $form .= textfield('City', 'home_location][city', 'Redwood City', 64, 64, NULL, NULL, TRUE);
  300. $form .= _location_province_select_options(TRUE, 'us-CA', 'home_location');
  301. $form .= textfield('Postal Code', 'home_location][postal_code', '94063', 64, 64, NULL, NULL, TRUE);
  302. $form .= _location_country_select_options(TRUE, 'us', 'home_location');
  303. Note that in both cases, the first and third argument can take the same array since all the fields are being required.
  304. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  305. function location_proximity_form($location_form = '', $label = '', $description = '', $prefilled_values = array(), $form_name = 'location');
  306. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  307. This function generates a form for doing proximity searches within a certain distance
  308. of a specified point.
  309. Depending on the context within which this function is called, the search-point can either
  310. be user-supplied via the location form that is passed (if one is available) or done within
  311. a search-point extracted from a contact table or some other location source specified by
  312. the programmer calling this function.
  313. @param $location_form
  314. An optional location form, preferably generated by location_form(). If the script processing this
  315. form also needs a user-supplied location, this parameter is used to specify a form for collecting the
  316. search-point about which this search is being done. If the caller does not supply a form, it is
  317. assumed that the caller already has a search point in mind and that this will be made clear in
  318. the $description parameter.
  319. @param $label
  320. The label you want to apply to the form group that is returned (passed as $legend param to form_group()).
  321. @param $description
  322. A text description of what is being searched for (e.g., 'Find all upcoming events near you.')
  323. @param $prefilled_values
  324. An associative array for prefilled values for the proximity search parameters, where
  325. 'distance' => is the prefilled int value to be selected for the distance scalar
  326. 'distance_unit' => is 'km' or 'mile'
  327. @param $form_name
  328. An additional parameter to help prevent HTML input name collisions. If the caller is using this
  329. function to generate more than 1 location proximity form on a page, then the generated name for
  330. each HTML input's "name" attribute will be $form_name. Allowing the caller to pass $form_name allows
  331. the caller the flexibility of using more than one location proximity search form on one page.
  332. @return
  333. An HTML form (generated by Drupal form functions) that lets users specify proximity search parameters that include distance,
  334. the unit of distance, and a search-point if the optional $location_form parameter is passed. If one is not passed,
  335. the caller of this function will be assumed to already have one.
  336. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  337. function theme_location($location = array()); +
  338. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  339. Generates HTML for the passed location.
  340. @param $location
  341. An associative array where
  342. 'street' => A string representing the street location
  343. 'additional' => A string for any additional portion of the street location
  344. 'city' => A string for the city name
  345. 'province' => The standard postal abbreviation for the province
  346. 'country' => The two-letter ISO code for the country of the location (REQUIRED)
  347. 'postal_code' => The international postal code for the location
  348. @return
  349. An HTML string with special tags for locationes.
  350. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  351. function location_distance_between($latlonA = array(), $latlonB = array(), $distance_unit = 'km'); +
  352. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  353. Given two points in lat/lon form, returns the distance between them.
  354. @param $latlonA
  355. An associative array where
  356. 'lon' => is a floating point of the longitude coordinate for the point given by latlonA
  357. 'lat' => is a floating point of the latitude coordinate for the point given by latlonB
  358. @param $latlonB
  359. Another point formatted like $latlonB
  360. @param $distance_unit
  361. A string that is either 'km' or 'mile'.
  362. If neither 'km' or 'mile' is passed, the parameter is forced to 'km'
  363. @return
  364. NULL if sense can't be made of the parameters.
  365. An associative array where
  366. 'scalar' => Is the distance between the two lat/lon parameter points
  367. 'distance_unit' => Is the unit of distance being represented by 'scalar'.
  368. This will be 'km' unless 'mile' is passed for the $distance_unit param
  369. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  370. function location_form2api($location = array()); +
  371. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  372. @param $location
  373. An associative array that has been submitted by an HTML form generated by location_form().
  374. @return
  375. An associative array in which the submitted values are modified to pass to the location API
  376. as the $location parameter (excepting location_form()).
  377. This means changing the province field to remove the country code and dash.
  378. For example: California is served by the key 'us-CA' in the location form and this is what is passed when it is
  379. submitted through a form generated by location_form().
  380. This is changed to 'CA' in the returned array.
  381. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  382. function location_api2form($location = array()); +
  383. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  384. Inverse of location_form2api()
  385. @param $location
  386. An associative array that can be passed as the $location parameter to the location API.
  387. @return
  388. An associative array with the same values modified so that the array can be passed as the
  389. $prefilled_values parameter to location_api2form()
  390. Meant to take the standard location array format used by the public API (minus the form generating functions) and convert them
  391. into values that can be used by location_form() to fill in the prefilled values.