google.inc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. // $Id$
  3. /**
  4. * @file
  5. * Plugin to provide a google 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("Google Geocoder"),
  13. 'description' => t('Geocodes via google geocoder'),
  14. 'callback' => 'geocoder_google',
  15. 'field_types' => array('text', 'text_long', 'addressfield', 'location', 'text_with_summary', 'computed', 'taxonomy_term_reference'),
  16. 'field_callback' => 'geocoder_google_field',
  17. 'settings_callback' => 'geocoder_google_form',
  18. 'terms_of_service' => 'http://code.google.com/apis/maps/documentation/geocoding/#Limits',
  19. );
  20. /**
  21. * Process Markup
  22. */
  23. function geocoder_google($address, $options = array()) {
  24. try {
  25. geophp_load();
  26. $query = array(
  27. 'address' => $address,
  28. 'sensor' => 'false',
  29. );
  30. $url = url("http://maps.googleapis.com/maps/api/geocode/json", array('query' => $query));
  31. $result = drupal_http_request($url);
  32. if (isset($result->error)) {
  33. $args = array(
  34. '@code' => $result->code,
  35. '@error' => $result->error,
  36. );
  37. $msg = t('HTTP request to google API failed.\nCode: @code\nError: @error', $args);
  38. throw new Exception($msg);
  39. }
  40. $data = json_decode($result->data);
  41. if ($data->status != 'OK') {
  42. $args = array('@status' => $data->status);
  43. $msg = t('Google API returned bad status.\nStatus: @status', $args);
  44. throw new Exception($msg);
  45. }
  46. $geometries = array();
  47. foreach ($data->results as $item) {
  48. // Check if we should reject these results
  49. if (isset($options['reject_results'])) {
  50. if (in_array($item->geometry->location_type, $options['reject_results'], TRUE)) {
  51. continue;
  52. }
  53. }
  54. // Construct a geoPHP Geometry depending on what type of geometry we want returned (defaults to point)
  55. if (!isset($options['geometry_type']) || $options['geometry_type'] == 'point') {
  56. $geom = new Point($item->geometry->location->lng, $item->geometry->location->lat);
  57. }
  58. elseif ($options['geometry_type'] == 'bounds') {
  59. $points = array(
  60. new Point($item->geometry->bounds->southwest->lng, $item->geometry->bounds->southwest->lat),
  61. new Point($item->geometry->bounds->southwest->lng, $item->geometry->bounds->northeast->lat),
  62. new Point($item->geometry->bounds->northeast->lng, $item->geometry->bounds->northeast->lat),
  63. new Point($item->geometry->bounds->northeast->lng, $item->geometry->bounds->southwest->lat),
  64. new Point($item->geometry->bounds->southwest->lng, $item->geometry->bounds->southwest->lat),
  65. );
  66. $geom = new Polygon(array(new LineString($points)));
  67. }
  68. elseif ($options['geometry_type'] == 'viewport') {
  69. $points = array(
  70. new Point($item->geometry->viewport->southwest->lng, $item->geometry->viewport->southwest->lat),
  71. new Point($item->geometry->viewport->southwest->lng, $item->geometry->viewport->northeast->lat),
  72. new Point($item->geometry->viewport->northeast->lng, $item->geometry->viewport->northeast->lat),
  73. new Point($item->geometry->viewport->northeast->lng, $item->geometry->viewport->southwest->lat),
  74. new Point($item->geometry->viewport->southwest->lng, $item->geometry->viewport->southwest->lat),
  75. );
  76. $geom = new Polygon(array(new LineString($points)));
  77. }
  78. // Add additional metadata to the geometry - it might be useful!
  79. $geom->data = array();
  80. $geom->data['geocoder_accuracy'] = $item->geometry->location_type;
  81. $geom->data['geocoder_formatted_address'] = $item->formatted_address;
  82. $geom->data['geocoder_address_components'] = $item->address_components;
  83. $geometries[] = $geom;
  84. }
  85. if (empty($geometries)) {
  86. return;
  87. }
  88. // Check if we should return all results as a compound geometry
  89. if (isset($options['all_results'])) {
  90. if ($options['all_results']) {
  91. return geoPHP::geometryReduce($geometries);
  92. }
  93. }
  94. // The connonical geometry is the first result (best guesse)
  95. $geometry = array_shift($geometries);
  96. // If there are any other geometries, these are auxiliary geometries that represent "alternatives"
  97. if (count($geometries)) {
  98. $geometry->data['geocoder_alternatives'] = $geometries;
  99. }
  100. return $geometry;
  101. } catch (Exception $e) {
  102. watchdog_exception('geocoder', $e);
  103. return FALSE;
  104. }
  105. }
  106. function geocoder_google_field($field, $field_item, $options = array()) {
  107. if ($field['type'] == 'text' || $field['type'] == 'text_long' || $field['type'] == 'text_with_summary' || $field['type'] == 'computed') {
  108. return geocoder_google($field_item['value'], $options);
  109. }
  110. if ($field['type'] == 'addressfield') {
  111. $address = geocoder_widget_parse_addressfield($field_item);
  112. return geocoder_google($address, $options);
  113. }
  114. if ($field['type'] == 'location') {
  115. $address = geocoder_widget_parse_locationfield($field_item);
  116. return geocoder_google($address, $options);
  117. }
  118. if ($field['type'] == 'taxonomy_term_reference') {
  119. $term = taxonomy_term_load($field_item['tid']);
  120. return geocoder_google($term->name);
  121. }
  122. }
  123. function geocoder_google_form($default_values = array()) {
  124. $form = array();
  125. $form['geometry_type'] = array(
  126. '#type' => 'select',
  127. '#title' => 'Geometry Type',
  128. '#options' => array(
  129. 'point' => 'Point (default)',
  130. 'bounds' => 'Bounding Box',
  131. 'viewport' => 'Viewport',
  132. ),
  133. '#default_value' => isset($default_values['geometry_type']) ? $default_values['geometry_type'] : 'point',
  134. );
  135. $form['all_results'] = array(
  136. '#type' => 'checkbox',
  137. '#title' => 'Geocode all alternative results',
  138. '#default_value' => isset($default_values['all_results']) ? $default_values['all_results'] : FALSE,
  139. '#description' => 'Often an ambiguous address (such as "Springfield USA") can result in multiple hits. By default we only return the first (best guess) result. Check this to return all results as a Multi-Geometry (MultiPoint or MultiPolygon).',
  140. );
  141. $form['reject_results'] = array(
  142. '#type' => 'checkboxes',
  143. '#title' => 'Reject Results',
  144. '#options' => array(
  145. 'APPROXIMATE' => 'APPROXIMATE: indicates that the returned result is approximate.',
  146. 'GEOMETRIC_CENTER' => 'GEOMETRIC_CENTER: indicates that the returned result is the geometric center of a result such as a polyline (for example, a street) or polygon (region).',
  147. 'RANGE_INTERPOLATED' => 'RANGE_INTERPOLATED: indicates that the returned result reflects an approximation (usually on a road) interpolated between two precise points (such as intersections). Interpolated results are generally returned when rooftop geocodes are unavailable for a street address.',
  148. 'ROOFTOP' => 'ROOFTOP: indicates that the returned result is a precise geocode for which we have location information accurate down to street address precision.',
  149. ),
  150. '#default_value' => isset($default_values['reject_results']) ? $default_values['reject_results'] : array(),
  151. '#description' => 'Reject results that do not meet a certain level of quality or precision. Check all types of results to reject.',
  152. );
  153. return $form;
  154. }