google.class.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. * or see http://www.gnu.org/
  17. */
  18. /**
  19. * \file htdocs/core/class/google.class.php
  20. * \brief A set of functions for using Google APIs
  21. */
  22. /**
  23. * Class to manage Google API
  24. */
  25. class GoogleAPI
  26. {
  27. /**
  28. * @var DoliDB Database handler.
  29. */
  30. public $db;
  31. /**
  32. * @var string Error code (or message)
  33. */
  34. public $error='';
  35. public $key;
  36. /**
  37. * Constructor
  38. *
  39. * @param DoliDB $db Database handler
  40. * @param string $key Google key
  41. */
  42. function __construct($db,$key)
  43. {
  44. $this->db=$db;
  45. $this->key=$key;
  46. }
  47. /**
  48. * Return geo coordinates of an address
  49. *
  50. * @param string $address Address
  51. * Example: 68 Grande rue Charles de Gaulle,+94130,+Nogent sur Marne,+France
  52. * Example: 188, rue de Fontenay,+94300,+Vincennes,+France
  53. * @return string Coordinates
  54. */
  55. function getGeoCoordinatesOfAddress($address)
  56. {
  57. global $conf;
  58. $i=0;
  59. // Desired address
  60. $urladdress = "https://maps.google.com/maps/geo?q=".urlencode($address)."&output=xml&key=".$this->key;
  61. // Retrieve the URL contents
  62. $page = file_get_contents($urladdress);
  63. $code = strstr($page, '<coordinates>');
  64. $code = strstr($code, '>');
  65. $val=strpos($code, "<");
  66. $code = substr($code, 1, $val-1);
  67. //print $code;
  68. //print "<br>";
  69. $latitude = substr($code, 0, strpos($code, ","));
  70. $longitude = substr($code, strpos($code, ",")+1, dol_strlen(strpos($code, ","))-3);
  71. // Output the coordinates
  72. //echo "Longitude: $longitude ',' Latitude: $latitude";
  73. $i++;
  74. }
  75. }