PHPackages                             willdurand/propel-geocodable-behavior - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. willdurand/propel-geocodable-behavior

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

willdurand/propel-geocodable-behavior
=====================================

The GeocodableBehavior helps you build geo-aware applications. It automatically geocodes your models when they are saved, giving you the ability to search by location and calculate distances between records.

1.2.1(11y ago)2667.4k↑62.5%17[1 issues](https://github.com/geocoder-php/GeocodableBehavior/issues)[1 PRs](https://github.com/geocoder-php/GeocodableBehavior/pulls)MITPHPPHP &gt;=5.2.4

Since Apr 22Pushed 8y ago4 watchersCompare

[ Source](https://github.com/geocoder-php/GeocodableBehavior)[ Packagist](https://packagist.org/packages/willdurand/propel-geocodable-behavior)[ RSS](/packages/willdurand-propel-geocodable-behavior/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (16)Used By (0)

GeocodableBehavior
==================

[](#geocodablebehavior)

[![Build Status](https://camo.githubusercontent.com/51c57116ca76194992d2859a0e5ae056c2fced1448a99da093a8b00c55e57625/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f67656f636f6465722d7068702f47656f636f6461626c654265686176696f722e706e67)](http://travis-ci.org/geocoder-php/GeocodableBehavior)

The **GeocodableBehavior** helps you build geo-aware applications. It automatically geocodes your models when they are saved, giving you the ability to search by location and calculate distances between records.

This behavior uses [Geocoder](https://github.com/geocoder-php/Geocoder), the Geocoder PHP 5.3 library and requires [Propel](http://github.com/propelorm/Propel)1.6.4-dev and above.

Installation
------------

[](#installation)

Cherry-pick the `GeocodableBehavior.php` file is `src/`, put it somewhere, then add the following line to your `propel.ini` or `build.properties`configuration file:

```
propel.behavior.geocodable.class = path.to.GeocodableBehavior
```

Usage
-----

[](#usage)

Just add the following XML tag in your `schema.xml` file:

```

```

Basically, the behavior will add:

- two new columns to your model (`latitude` and `longitude`);
- four new methods to the *ActiveRecord* API (`getDistanceTo()`, `isGeocoded()`, `getCoordinates()`, and `setCoordinates()`);
- three new methods to the *ActiveQuery* API (`withDistance()`, `filterByDistanceFrom()`, `filterNear()`).

### ActiveRecord API

[](#activerecord-api)

`getDistanceTo()` returns the distance between the current object and a given one. The method takes two arguments:

- a geocoded object;
- a measure unit (`KILOMETERS_UNIT`, `MILES_UNIT`, or `NAUTICAL_MILES_UNIT`defined in the `Peer` class of the geocoded model).

`isGeocoded()` returns a boolean value whether the object has been geocoded, or not.

`getCoordinates()`, `setCoordinates()` allows to quickly set/get latitude, and longitude values.

### ActiveQuery API

[](#activequery-api)

`withDistance()` takes three arguments:

- a latitude value;
- a longitude value;
- a measure unit (`KILOMETERS_UNIT`, `MILES_UNIT`, or `NAUTICAL_MILES_UNIT`defined in the `Peer` class of the geocoded model);

It will add a `Distance` column on your current query and returns itself for fluid interface. Example use: combine with `orderByDistance()` and `limit()` to return closest matches.

`filterByDistanceFrom()` takes five arguments:

- a latitude value;
- a longitude value;
- a distance value;
- a measure unit (`KILOMETERS_UNIT`, `MILES_UNIT`, or `NAUTICAL_MILES_UNIT`defined in the `Peer` class of the geocoded model);
- a comparison sign (`Criteria::LESS_THAN` is the default value).

It will add a filter by distance on your current query and returns itself for fluid interface.

`filterNear` takes three arguments:

- a model object;
- a distance value;
- a measure unit (`KILOMETERS_UNIT`, `MILES_UNIT`, or `NAUTICAL_MILES_UNIT`defined in the `Peer` class of the geocoded model).

Automatic Geocoding
-------------------

[](#automatic-geocoding)

At this step, you have to fill in the two columns (`latitude` and `longitude`) yourself. It's not really useful, right ?

Automatic geocoding to the rescue! There are two automatic ways to get geocoded information:

- using IP addresses;
- using street addresses.

It provides a `geocode()` method that autoupdate the location values. To prevent autofill when modified, just set `auto_update` attribute to false.

This method returns a [`ResultInterface`](https://github.com/geocoder-php/Geocoder/blob/master/src/Geocoder/Result/ResultInterface.php)object, so you can override this method to fill in more fields depending on your model:

```
