yahoo.inc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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', 'location', '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. global $base_path;
  24. $geocoder_settings = variable_get("geocoder_settings", array());
  25. if (!empty($geocoder_settings["geocoder_apikey_yahoo"])) {
  26. $consumer_key = $geocoder_settings["geocoder_apikey_yahoo"];
  27. }
  28. else {
  29. drupal_set_message("You must set up your Yahoo API key. Click <a href='$base_path/admin/config/content/geocoder'>here</a>",'error');
  30. return;
  31. }
  32. $request = drupal_http_request("http://where.yahooapis.com/geocode?location=" . urlencode($address) . "&flags=J&appid={$consumer_key}");
  33. $data = json_decode($request->data);
  34. geophp_load();
  35. return _geocoder_yahoo_geometry($data);
  36. }
  37. function geocoder_yahoo_field($field, $field_item) {
  38. if ($field['type'] == 'text' || $field['type'] == 'text_long' || $field['type'] == 'text_with_summary' || $field['type'] == 'computed') {
  39. return geocoder_yahoo($field_item['value']);
  40. }
  41. if ($field['type'] == 'addressfield') {
  42. $address = geocoder_widget_parse_addressfield($field_item);
  43. return geocoder_yahoo($address);
  44. }
  45. if ($field['type'] == 'location') {
  46. $address = geocoder_widget_parse_locationfield($field_item);
  47. return geocoder_yahoo($address);
  48. }
  49. if ($field['type'] == 'taxonomy_term_reference') {
  50. $term = taxonomy_term_load($field_item['tid']);
  51. return geocoder_yahoo($term->name);
  52. }
  53. }
  54. function _geocoder_yahoo_geometry(&$data) {
  55. if (!isset($data->ResultSet)) {
  56. return NULL;
  57. }
  58. // Yahoo Placefinder API v2.
  59. elseif (isset($data->ResultSet->Result->longitude)) {
  60. return new Point($data->ResultSet->Result->longitude, $data->ResultSet->Result->latitude);
  61. }
  62. // Yahoo Placefinder API v1.
  63. elseif (isset($data->ResultSet->Results[0]->longitude)) {
  64. return new Point($data->ResultSet->Results[0]->longitude, $data->ResultSet->Results[0]->latitude);
  65. }
  66. }