yandex.inc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. // $Id$
  3. /**
  4. * @file
  5. * Plugin to provide a yandex 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("Yandex (Яндекс.Карт)"),
  13. 'description' => t('Geocodes addresses via Yandex (Яндекс.Карт)'),
  14. 'callback' => 'geocoder_yandex',
  15. 'field_types' => array('text','text_long','addressfield', 'location','text_with_summary','computed', 'taxonomy_term_reference'),
  16. 'field_callback' => 'geocoder_yandex_field',
  17. 'terms_of_service' => 'http://api.yandex.ru/maps/geocoder/doc/desc/concepts/About.xml',
  18. );
  19. /**
  20. * Process Address
  21. */
  22. function geocoder_yandex($address, $options = array()) {
  23. global $base_path;
  24. $geocoder_settings = variable_get("geocoder_settings", array());
  25. if (!empty($geocoder_settings["geocoder_apikey_yandex"])) {
  26. $consumer_key = $geocoder_settings["geocoder_apikey_yandex"];
  27. }
  28. else {
  29. drupal_set_message("You must set up your Yandex API key. Click <a href='$base_path/admin/config/content/geocoder'>here</a>",'error');
  30. return;
  31. }
  32. $params = array (
  33. 'format' => 'json',
  34. 'results' => 1,
  35. 'key' => $consumer_key,
  36. 'geocode' => $address,
  37. );
  38. $request = drupal_http_request("http://geocode-maps.yandex.ru/1.x/?" . http_build_query($params));
  39. $data = json_decode($request->data);
  40. return _geocoder_yandex_geometry($data);
  41. }
  42. function geocoder_yandex_field($field, $field_item) {
  43. if ($field['type'] == 'text' || $field['type'] == 'text_long' || $field['type'] == 'text_with_summary' || $field['type'] == 'computed') {
  44. return geocoder_yandex($field_item['value']);
  45. }
  46. if ($field['type'] == 'addressfield') {
  47. $address = geocoder_widget_parse_addressfield($field_item);
  48. return geocoder_yandex($address);
  49. }
  50. if ($field['type'] == 'location') {
  51. $address = geocoder_widget_parse_locationfield($field_item);
  52. return geocoder_google($address);
  53. }
  54. if ($field['type'] == 'taxonomy_term_reference') {
  55. $term = taxonomy_term_load($field_item['tid']);
  56. return geocoder_yandex($term->name);
  57. }
  58. }
  59. function _geocoder_yandex_geometry(&$data) {
  60. try {
  61. geophp_load();
  62. if (isset($data->error)) {
  63. $args = array(
  64. '@status' => $data->error->status,
  65. '@error' => $data->error->message,
  66. );
  67. $msg = t('Yandex API has reported an error.\nStatus: @status\nError: @error', $args);
  68. throw new Exception($msg);
  69. }
  70. if($data->response->GeoObjectCollection->metaDataProperty->GeocoderResponseMetaData->found == 0) {
  71. return NULL;
  72. }
  73. $loc = explode(' ', $data->response->GeoObjectCollection->featureMember[0]->GeoObject->Point->pos);
  74. return new Point ($loc[0], $loc[1]);
  75. } catch (Exception $e) {
  76. watchdog_exception('geocoder', $e);
  77. }
  78. }