google.class.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. var $db;
  28. var $error;
  29. var $key;
  30. /**
  31. * Constructor
  32. *
  33. * @param DoliDB $db Database handler
  34. * @param string $key Google key
  35. */
  36. function __construct($db,$key)
  37. {
  38. $this->db=$db;
  39. $this->key=$key;
  40. }
  41. /**
  42. * Return geo coordinates of an address
  43. *
  44. * @param string $address Address
  45. * Example: 68 Grande rue Charles de Gaulle,+94130,+Nogent sur Marne,+France
  46. * Example: 188, rue de Fontenay,+94300,+Vincennes,+France
  47. * @return string Coordinates
  48. */
  49. function getGeoCoordinatesOfAddress($address)
  50. {
  51. global $conf;
  52. $i=0;
  53. // Desired address
  54. $urladdress = "http://maps.google.com/maps/geo?q=".urlencode($address)."&output=xml&key=".$this->key;
  55. // Retrieve the URL contents
  56. $page = file_get_contents($urladdress);
  57. $code = strstr($page, '<coordinates>');
  58. $code = strstr($code, '>');
  59. $val=strpos($code, "<");
  60. $code = substr($code, 1, $val-1);
  61. //print $code;
  62. //print "<br>";
  63. $latitude = substr($code, 0, strpos($code, ","));
  64. $longitude = substr($code, strpos($code, ",")+1, dol_strlen(strpos($code, ","))-3);
  65. // Output the coordinates
  66. //echo "Longitude: $longitude ',' Latitude: $latitude";
  67. $i++;
  68. }
  69. }