mapquest_nominatim.inc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. // $Id$
  3. /**
  4. * @file
  5. * Plugin to provide a MapQuest Nominatim geocoder.
  6. */
  7. /**
  8. * Plugins are described by creating a $plugin array which will be used
  9. * by the system that includes this file.
  10. */
  11. $plugin = array(
  12. 'title' => t("MapQuest Nominatim"),
  13. 'description' => t('Geocodes via MapQuest Nominatim'),
  14. 'callback' => 'geocoder_mapquest_nominatim',
  15. 'field_types' => array('text', 'text_long', 'addressfield', 'location', 'text_with_summary', 'computed', 'taxonomy_term_reference'),
  16. 'field_callback' => 'geocoder_mapquest_nominatim_field',
  17. 'terms_of_service' => 'http://developer.mapquest.com/web/info/terms-of-use',
  18. );
  19. /**
  20. * Process Markup
  21. */
  22. function geocoder_mapquest_nominatim($address, $options = array()) {
  23. $geocoder_settings = variable_get("geocoder_settings", array());
  24. $api_url = "http://open.mapquestapi.com/nominatim/v1/search";
  25. $params = array(
  26. 'q' => str_replace(' ', '+', $address),
  27. 'format' => 'json',
  28. 'addressdetails' => 0,
  29. 'limit' => 1,
  30. 'osm_type' => 'N',
  31. );
  32. $request = drupal_http_request($api_url . '?' . drupal_http_build_query($params));
  33. $data = json_decode($request->data);
  34. return _geocoder_mapquest_nominatim_geometry($data);
  35. }
  36. function geocoder_mapquest_nominatim_field($field, $field_item) {
  37. if ($field['type'] == 'text' || $field['type'] == 'text_long' || $field['type'] == 'text_with_summary' || $field['type'] == 'computed') {
  38. return geocoder_mapquest_nominatim($field_item['value']);
  39. }
  40. if ($field['type'] == 'addressfield') {
  41. $address = geocoder_widget_parse_addressfield($field_item);
  42. return geocoder_mapquest_nominatim($address);
  43. }
  44. if ($field['type'] == 'location') {
  45. $address = geocoder_widget_parse_locationfield($field_item);
  46. return geocoder_mapquest_nomination($address);
  47. }
  48. if ($field['type'] == 'taxonomy_term_reference') {
  49. $term = taxonomy_term_load($field_item['tid']);
  50. return geocoder_mapquest_nominatim($term->name);
  51. }
  52. }
  53. function _geocoder_mapquest_nominatim_geometry(&$data) {
  54. if (!isset($data[0]->lon)) {
  55. return NULL;
  56. }
  57. geophp_load();
  58. return new Point($data[0]->lon, $data[0]->lat);
  59. }