yandex.inc 2.9 KB

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