google.class.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 <https://www.gnu.org/licenses/>.
  16. * or see https://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. public 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. public 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=".urlencode($this->key);
  61. // Retrieve the URL contents
  62. require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
  63. $pagearray = getURLContent($urladdress, 'GET');
  64. $page = $pagearray['content'];
  65. $code = strstr($page, '<coordinates>');
  66. $code = strstr($code, '>');
  67. $val = strpos($code, "<");
  68. $code = substr($code, 1, $val - 1);
  69. //print $code;
  70. //print "<br>";
  71. $latitude = substr($code, 0, strpos($code, ","));
  72. $longitude = substr($code, strpos($code, ",") + 1, dol_strlen(strpos($code, ",")) - 3);
  73. // Output the coordinates
  74. //echo "Longitude: $longitude ',' Latitude: $latitude";
  75. $i++;
  76. }
  77. }