yahoo.inc 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. // $Id$
  3. /**
  4. * @file
  5. * Plugin to provide a yahoo 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("Yahoo Placefinder"),
  13. 'description' => t('Geocodes via Yahoo Placefinder'),
  14. 'callback' => 'geocoder_yahoo',
  15. 'field_types' => array('text', 'text_long', 'addressfield', 'text_with_summary', 'computed', 'taxonomy_term_reference'),
  16. 'field_callback' => 'geocoder_yahoo_field',
  17. 'terms_of_service' => 'http://developer.yahoo.com/geo/placefinder/',
  18. );
  19. /**
  20. * Process Markup
  21. */
  22. function geocoder_yahoo($address, $options = array()) {
  23. $geocoder_settings = variable_get("geocoder_settings", array());
  24. $consumer_key = $geocoder_settings["geocoder_apikey_yahoo"];
  25. $request = drupal_http_request("http://where.yahooapis.com/geocode?location=" . urlencode($address) . "&flags=J&appid={$consumer_key}");
  26. $data = json_decode($request->data);
  27. geophp_load();
  28. return _geocoder_yahoo_geometry($data);
  29. }
  30. function geocoder_yahoo_field($field, $field_item) {
  31. if ($field['type'] == 'text' || $field['type'] == 'text_long' || $field['type'] == 'text_with_summary' || $field['type'] == 'computed') {
  32. return geocoder_yahoo($field_item['value']);
  33. }
  34. if ($field['type'] == 'addressfield') {
  35. $address = geocoder_widget_parse_addressfield($field_item);
  36. return geocoder_yahoo($address);
  37. }
  38. if ($field['type'] == 'taxonomy_term_reference') {
  39. $term = taxonomy_term_load($field_item['tid']);
  40. return geocoder_yahoo($term->name);
  41. }
  42. }
  43. function _geocoder_yahoo_geometry(&$data) {
  44. if (!isset($data->ResultSet->Results[0]->longitude, $data->ResultSet->Results[0])) {
  45. return NULL;
  46. }
  47. return new Point($data->ResultSet->Results[0]->longitude, $data->ResultSet->Results[0]->latitude);
  48. }