1
|
|
|
<?php |
2
|
|
|
namespace TNM\GolfCourses\Service; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2017 Tomas Norre Mikkelsen <[email protected]> |
8
|
|
|
* |
9
|
|
|
* All rights reserved |
10
|
|
|
* |
11
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
12
|
|
|
* free software; you can redistribute it and/or modify |
13
|
|
|
* it under the terms of the GNU General Public License as published by |
14
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
15
|
|
|
* (at your option) any later version. |
16
|
|
|
* |
17
|
|
|
* The GNU General Public License can be found at |
18
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
19
|
|
|
* |
20
|
|
|
* This script is distributed in the hope that it will be useful, |
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23
|
|
|
* GNU General Public License for more details. |
24
|
|
|
* |
25
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
26
|
|
|
***************************************************************/ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class GoogleCoordinatesService |
30
|
|
|
* |
31
|
|
|
* @package TNM\GolfCourses\Service |
32
|
|
|
*/ |
33
|
|
|
class GoogleCoordinatesService |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @param $address |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
* @codeCoverageIgnore |
40
|
|
|
*/ |
41
|
|
|
public static function getCoordinates($address){ |
42
|
|
|
|
43
|
|
|
if(empty($address)) { |
44
|
|
|
return []; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// replace all the white space with "+" sign to match with google search pattern |
48
|
|
|
$address = str_replace(" ", "+", $address); |
|
|
|
|
49
|
|
|
|
50
|
|
|
$url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=$address"; |
|
|
|
|
51
|
|
|
|
52
|
|
|
$response = file_get_contents($url); |
53
|
|
|
|
54
|
|
|
//generate array object from the response from the web |
55
|
|
|
$json = json_decode($response,TRUE); |
56
|
|
|
|
57
|
|
|
return [ |
58
|
|
|
'longitude' => $json['results'][0]['geometry']['location']['lng'], |
59
|
|
|
'latitude' => $json['results'][0]['geometry']['location']['lat'] |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
} |
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.