Listing 1 function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(success, error); } else { alert('Ihr Browser unterstützt keine Geolocation.'); } } function success(position) { var text = "Längengrad : " + position.coords.longitude + "\n" + "Breitengrad : " + position.coords.latitude + "\n" + "Genauigkeit : " + position.coords.accuracy; alert(text); } function error(error) { var text = "Fehlercode : " + error.code + "\n" + "Message : " + error.message; alert(text); } Listing 2 Geolocation Example

Geolocation Beispiel

Wo bin ich?

Listing 3 function success(position) { // bisheriger Code der success-Funktion var geocoder = new google.maps.Geocoder(); geocoder.geocode({'latLng': latlng}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { if (results[0]) { var infowindow = new google.maps.InfoWindow(); infowindow.setContent(results[0].formatted_address); infowindow.open(map, marker); } } else { alert("Adresse konnte nicht ermittelt werden: " + status); } }); } Listing 4 { "results" : [ { "address_components" : [ { "long_name" : "Brandenburger Tor", "short_name" : "Brandenburger Tor", "types" : [ "transit_station" ] }, { "long_name" : "Mitte", "short_name" : "Mitte", "types" : [ "sublocality", "political" ] }, ... ], "formatted_address" : "Brandenburger Tor, 10117 Berlin, Allemagne", "geometry" : { "location" : { "lat" : 52.5163890, "lng" : 13.3808330 }, "location_type" : "APPROXIMATE", "viewport" : {...}, "types" : [ "transit_station" ] } ], "status" : "OK" } Listing 5 class Geocoder { public function geocodeAddress($street,$streetNo, $zip, $city) { $address = "$street $streetNo, $zip $city, Deutschland"; $url = sprintf('http://maps.google.com/maps/api/geocode/json?address=%s&sensor=false', urlencode($address)); $response = $this->_fetch($url); if ($response->status === 'OK') { return $response->results[0]->geometry->location; } else { throw new Exception($response->status); } } protected function _fetch($url) { $json = file_get_contents($url); $response = json_decode($json); return $response; } } Listing 6 class Geocoder { // bisherige Methoden public function geocodeReverse($lat, $long) { $url = sprintf('http://maps.google.com/maps/api/geocode/json?latlng=%s,%s&sensor=false', $lat, $long); $response = $this->_fetch($url); if ($response->status === 'OK') { return $response->results; } else { throw new Exception($response->status); } } } Listing 7 require_once 'Geocoder.php'; if (isset($_GET['lat']) && isset($_GET['long'])) { printf('

Längengrad: %s

', $_GET['long']); printf('

Breitengrad: %s

', $_GET['lat']); try { $geocoder = new Geocoder(); $results = $geocoder->geocodeReverse($_GET['lat'], $_GET['long']); echo ""; } catch (Exception $e) { echo "Geocodierung ist fehlgeschlagen: " . $e->getMessage(); } } else { print '

Wo bin ich?

'; } Listing 8 CREATE TABLE places ( id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(32) NOT NULL, street VARCHAR(100) NOT NULL, streetNo VARCHAR(10) NOT NULL, zip VARCHAR(5) NOT NULL, city VARCHAR(100) NOT NULL, long REAL NOT NULL, lat REAL NOT NULL ); Listing 11 $geocoder = new Geocoder(); try { $location = $geocoder->geocodeAddress($_GET['street'], $_GET['streetNo'], $_GET['zip'], $_GET['city']); $long = $location->lng; $lat = $location->lat; } catch (Exception $e) { echo "Geocodierung ist fehlgeschlagen: " . $e->getMessage(); } Listing 12 $fields = array( "name", "lat", "long", "( 6371 * ACOS( COS(RADIANS($lat)) * COS(RADIANS(lat)) * COS(RADIANS(long) - RADIANS($long)) + SIN(RADIANS($lat)) * SIN(RADIANS(lat)) ) ) AS distance" ); $query = "SELECT ".implode(',', $fields)." FROM places WHERE distance < 50 ORDER BY distance ASC"; Listing 13 $result = $db->query($query); $places = $result->fetchAll(); printf("

Orte in der Nähe von %s %s, %s %s:

", $_GET['street'], $_GET['streetNo'], $_GET['zip'], $_GET['city']); echo ""; foreach ($places as $place) { printf("latlng = new google.maps.LatLng(%s,%s);\n", place['lat'], $place['long']); printf("marker = new google.maps.Marker({ position: latlng, map: map, title:'%s' });", $place['name']); }