Added method for reverse geocoding
authorAlexander Ebert <ebert@woltlab.com>
Sat, 1 Feb 2014 12:36:49 +0000 (13:36 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Sat, 1 Feb 2014 12:36:49 +0000 (13:36 +0100)
wcfsetup/install/files/js/WCF.Location.js

index c9f6627b17c26f685e0832c12e4d7e89b3e962b3..68cc0593938c40c1deec8b3c0880d339d218158b 100644 (file)
@@ -537,6 +537,12 @@ WCF.Location.GoogleMaps.LocationInput = Class.extend({
  * Provides utility functions for Google Maps maps.
  */
 WCF.Location.GoogleMaps.Util = {
+       /**
+        * geocoder instance
+        * @var google.maps.Geocoder
+        */
+       _geocoder: null,
+       
        /**
         * Focuses the given marker's map on the marker.
         * 
@@ -567,5 +573,35 @@ WCF.Location.GoogleMaps.Util = {
         */
        moveMarker: function(marker, latitude, longitude) {
                marker.setPosition(new google.maps.LatLng(latitude, longitude));
+       },
+       
+       /**
+        * Performs a reverse geocoding request.
+        * 
+        * @param       object                  callback
+        * @param       google.maps.Marker      marker
+        * @param       string                  latitude
+        * @param       string                  longitude
+        * @param       boolean                 fullResult
+        */
+       reverseGeocoding: function(callback, marker, latitude, longitude, fullResult) {
+               if (marker) {
+                       latitude = marker.getPosition().lat();
+                       longitude = marker.getPosition().lng();
+               }
+               
+               if (this._geocoder === null) {
+                       this._geocoder = new google.maps.Geocoder();
+               }
+               
+               var $latLng = new google.maps.LatLng(latitude, longitude);
+               this._geocoder.geocode({ latLng: $latLng }, function(results, status) {
+                       if (status == google.maps.GeocoderStatus.OK) {
+                               callback((fullResult ? results : results[0].formatted_address));
+                       }
+                       else {
+                               callback(null);
+                       }
+               });
        }
 };