geocoder A standardized interface for GeoCoding APIs

This extension allows you to use a standard interface for GeoCoding APIs. As well as to auto generate a Yahoo or Google map of the GeoCoded location.

Currently supported APIs are Google, GeoCoder.us, and Yahoo.

Resources

Documentation

Requirements
  • Yii 1.0.10 or above
Installation
  • Extract the release file under protected/extensions
Usage

Register for an API Key
Google key: http://code.google.com/apis/maps/signup.html<br /> Yahoo key: http://developer.yahoo.com/maps/rest/V1/geocode.html

See the following code examples:

Standalone

Yii::import('application.extensions.geocoder.GeoCoder');
$geocode = new GeoCoder;
$geocode->init();
$geocode->setApiKey('<your API key>'); // As of 1.0.2
$geocode->setDriver('<your driver>'); // 'google' or 'yahoo'
$result = $geocode->query('Las Vegas, NV 89103');

Application Component

'components' => array(
  ...
    'geocoder' => array(
       'class' => 'application.extensions.geocoder.GeoCoder',
       'api_key' => '<your API key>',
       'api_driver' => '<your driver>', // 'google' or 'yahoo'
     ),
  ...
)

$result = Yii::app()->geocoder->query('Las Vegas, NV 89103');
Rendering Maps

Summary
The map rendering is handled by the GeoCode_Result class. It uses its own render functions to generate the maps. It also requires javascript to be enabled.

Map Types
The map types that are rendered need to be passed to the view. These are constants as defined by the map supplier. Only include the constant name, do NOT include namespaces, etc.

Caveats
It is possible to change the map provider that is used for rendering a specific GeoCode_Result by passing an optional 3rd parameter to the renderMap function with the name of the provider. However, because some map APIs (eg. Yahoo) require an API key to validate access, these maps can only be rendered from a Result derived from their respective driver.

Because this is using v3 of the Google Maps API, it does not require an API key to access the javascript. Therefore, a google map can be rendered from any Result object. The same can not be said for the other map providers.

Usage
Create an empty div in your HTML with the id of "map_canvas", then use the following code:

Yahoo Map Example

// $result is a GeoCode_Result object from a Yahoo Driver
$result->renderMap('map_canvas', array(
	'mapType' => 'YAHOO_MAP_REG'
));

Google Map Example

// $result is a GeoCode_Result object from a Google Driver
$result->renderMap('map_canvas', array(
	'mapTypeId' => 'ROADMAP',
	'zoom' => 13
));

Note: Some browsers will not render the contents of the canvas and the maps will appear to be hidden. To fix this, add a new CSS entry to main.css that will force the browser to give the canvas a width and height. ~~~ [css]

map_canvas {

  1. Documentation
  2. Change Log
width: 500px;
height: 300px;

} ~~~

Multiple Points on the same map

As of version 1.0.4 it is now possible to render multiple points to the same map. This can be accomplished in two ways: 1) by rendering multiple results to the same container, 2) by manually specifying the lat/lon where the marker is to be placed.

Example

// $result is an array of GeoCode_Result objects from a Google Driver
$result[0]->renderMap('map_canvas', array(
	'mapTypeId' => 'ROADMAP',
	'zoom' => 13
));

// Render the second result to the same map
// Any options passed to the function will be ignored
//  since this canvas has already been initialized
$result[1]->renderMap('map_canvas');

// Render a point manually to that same canvas
// Arguments are in this order because if no canvas is
//  specified, it defaults to using the last one created
$result[0]->renderPoint($my_lat, $my_lon, 'map_canvas');

Change Log

November 12, 2009
  • Initial release.
November 12, 2009 (version 1.0.1)
  • libraries/drivers/Yahoo.php
    1. Adding support for properly handling warning messages
    2. Changed how the cleaned_query is calculated to make it more smart
November 13, 2009 (version 1.0.2)
  • Added functionality to generate a map of the GeoCoded location
  • Fixed Bug #482262
November 17, 2009 (version 1.0.3)
  • Added a new driver for geocoder.us, use 'geocoder.us' as the driver name
  • Fixed Bug #483921
  • Fixed Bug #484029
December 2, 2009 (version 1.0.4)
  • Added ability to render many results to the same map using renderMap
  • Added ability to manually add markers to a map using renderPoint
3 1
4 followers
2 202 downloads
Yii Version: 1.1
License: (not set)
Category: Others
Tags:
Developed by: killermonk
Created on: Nov 12, 2009
Last updated: 14 years ago

Downloads

show all