bing.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. // $Id$
  3. /**
  4. * @file
  5. * Plugin to provide a bing geocoder.
  6. */
  7. /**
  8. * Plugin for Bing.
  9. */
  10. $plugin = array(
  11. 'title' => t("Bing"),
  12. 'description' => t('Geocodes via Bing'),
  13. 'callback' => 'geocoder_bing',
  14. 'field_types' => array('text', 'text_long', 'addressfield', 'location', 'text_with_summary', 'computed', 'taxonomy_term_reference'),
  15. 'field_callback' => 'geocoder_bing_field',
  16. 'terms_of_service' => 'http://msdn.microsoft.com/en-us/library/ff701714.aspx',
  17. );
  18. /**
  19. * Geocode an address.
  20. */
  21. function geocoder_bing($address, $options = array()) {
  22. try {
  23. geophp_load();
  24. $geocoder_settings = variable_get('geocoder_settings', array());
  25. if (!empty($geocoder_settings['geocoder_apikey_bing'])) {
  26. $consumer_key = $geocoder_settings['geocoder_apikey_bing'];
  27. }
  28. else {
  29. throw new Exception('You must specify a key.');
  30. }
  31. if (isset($options['address']) && is_array($options['address'])) {
  32. $query = array(
  33. 'countryRegion' => (isset($options['address']['country']) ? $options['address']['country'] : ''),
  34. 'adminDistrict' => (isset($options['address']['administrative_area']) ? $options['address']['administrative_area'] : ''),
  35. 'locality' => (isset($options['address']['locality']) ? $options['address']['locality'] : ''),
  36. 'postalCode' => (isset($options['address']['postal_code']) ? $options['address']['postal_code'] : ''),
  37. 'addressLine' => (isset($options['address']['thoroughfare']) ? $options['address']['thoroughfare'] : ''),
  38. 'userIp' => '127.0.0.1',
  39. 'includeNeighborhood' => 0,
  40. 'maxResults' => 1,
  41. 'key' => $consumer_key,
  42. );
  43. }
  44. else {
  45. $query = array(
  46. 'q' => $address,
  47. 'key' => $consumer_key,
  48. );
  49. }
  50. // Extra options
  51. if (!empty($options['userLocation'])) {
  52. $query['userLocation'] = $options['userLocation'];
  53. }
  54. if (!empty($options['userIp'])) {
  55. $query['userIp'] = $options['userIp'];
  56. }
  57. if (!empty($options['includeNeighborhood'])) {
  58. $query['includeNeighborhood'] = $options['includeNeighborhood'];
  59. }
  60. if (!empty($options['maxResults'])) {
  61. $query['maxResults'] = $options['maxResults'];
  62. }
  63. $url = url('http://dev.virtualearth.net/REST/v1/Locations', array('query' => $query));
  64. $result = drupal_http_request($url);
  65. if (isset($result->error)) {
  66. $args = array(
  67. '@code' => $result->code,
  68. '@error' => $result->error,
  69. );
  70. $msg = t('HTTP request to Bing API failed.\nCode: @code\nError: @error', $args);
  71. throw new Exception($msg);
  72. }
  73. $data = json_decode($result->data);
  74. if ($data->statusCode != 200) {
  75. $msg = t('Bing API returned bad status.\nStatus: @status\n@description', array(
  76. '@status' => $data->statusCode,
  77. '@description' => $data->statusDescription,
  78. ));
  79. throw new Exception($msg);
  80. }
  81. $geometries = array();
  82. foreach ($data->resourceSets as $resourceSet) {
  83. foreach ($resourceSet->resources as $resource) {
  84. $geom = new Point($resource->point->coordinates[1], $resource->point->coordinates[0]);
  85. $geom->data = $resource;
  86. $geometries[] = $geom;
  87. }
  88. }
  89. if (empty($geometries)) {
  90. return;
  91. }
  92. // The canonical geometry is the first result (best guess)
  93. $geometry = array_shift($geometries);
  94. // If there are any other geometries, these are auxiliary geometries that represent "alternatives"
  95. if (count($geometries)) {
  96. $geometry->data->geocoder_alternatives = $geometries;
  97. }
  98. return $geometry;
  99. } catch (Exception $e) {
  100. watchdog_exception('geocoder', $e);
  101. return FALSE;
  102. }
  103. }
  104. function geocoder_bing_field($field, $field_item) {
  105. if ($field['type'] == 'text' || $field['type'] == 'text_long' || $field['type'] == 'text_with_summary' || $field['type'] == 'computed') {
  106. return geocoder_bing($field_item['value']);
  107. }
  108. if ($field['type'] == 'addressfield' && module_exists('addressfield') && !addressfield_field_is_empty($field_item, $field)) {
  109. $address = geocoder_widget_parse_addressfield($field_item);
  110. return geocoder_bing($address, array('address' => $address));
  111. }
  112. if ($field['type'] == 'location') {
  113. $address = geocoder_widget_parse_locationfield($field_item);
  114. return geocoder_bing($address, array('address' => $address));
  115. }
  116. if ($field['type'] == 'taxonomy_term_reference') {
  117. $term = taxonomy_term_load($field_item['tid']);
  118. return geocoder_bing($term->name);
  119. }
  120. }