Skip to content

Instantly share code, notes, and snippets.

@sevein
Created September 20, 2012 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sevein/3758388 to your computer and use it in GitHub Desktop.
Save sevein/3758388 to your computer and use it in GitHub Desktop.
Generate latitude and longitude values for ICA-AtoM institution primary contacts
<?php
// See https://developers.google.com/maps/documentation/geocoding
// Thanks Google!
foreach (QubitRepository::getAll() as $item)
{
if (null !== $contact = $item->getPrimaryContact())
{
$address = $contact->streetAddress . ' ' . $contact->postalCode . ' ' . $contact->countryCode;
$address = str_replace(' ', '+', $address);
$url = 'http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address='.$address;
$json = file_get_contents($url);
$data = json_decode($json);
$results = $data->results[0];
$location = $results->geometry->location;
if (isset($location) && isset($location->lat) && isset($location->lng))
{
$contact->latitude = $location->lat;
$contact->longitude = $location->lng;
$contact->save();
echo ".";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment