summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Maps.iOS/GeocoderBackend.cs
diff options
context:
space:
mode:
authorJason Smith <jason.smith@xamarin.com>2016-03-22 20:02:25 (GMT)
committerJason Smith <jason.smith@xamarin.com>2016-03-22 23:13:41 (GMT)
commit17fdde66d94155fc62a034fa6658995bef6fd6e5 (patch)
treeb5e5073a2a7b15cdbe826faa5c763e270a505729 /Xamarin.Forms.Maps.iOS/GeocoderBackend.cs
downloadxamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.zip
xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.gz
xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.bz2
Initial import
Diffstat (limited to 'Xamarin.Forms.Maps.iOS/GeocoderBackend.cs')
-rw-r--r--Xamarin.Forms.Maps.iOS/GeocoderBackend.cs61
1 files changed, 61 insertions, 0 deletions
diff --git a/Xamarin.Forms.Maps.iOS/GeocoderBackend.cs b/Xamarin.Forms.Maps.iOS/GeocoderBackend.cs
new file mode 100644
index 0000000..42b9645
--- /dev/null
+++ b/Xamarin.Forms.Maps.iOS/GeocoderBackend.cs
@@ -0,0 +1,61 @@
+´╗┐using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+#if __UNIFIED__
+using CoreLocation;
+using AddressBookUI;
+#else
+using MonoTouch.AddressBookUI;
+using MonoTouch.CoreLocation;
+#endif
+#if __UNIFIED__
+using RectangleF = CoreGraphics.CGRect;
+using SizeF = CoreGraphics.CGSize;
+using PointF = CoreGraphics.CGPoint;
+
+#else
+using nfloat=global::System.Single;
+using nint=global::System.Int32;
+using nuint=global::System.UInt32;
+#endif
+
+namespace Xamarin.Forms.Maps.iOS
+{
+ internal class GeocoderBackend
+ {
+ public static void Register()
+ {
+ Geocoder.GetPositionsForAddressAsyncFunc = GetPositionsForAddressAsync;
+ Geocoder.GetAddressesForPositionFuncAsync = GetAddressesForPositionAsync;
+ }
+
+ static Task<IEnumerable<string>> GetAddressesForPositionAsync(Position position)
+ {
+ var location = new CLLocation(position.Latitude, position.Longitude);
+ var geocoder = new CLGeocoder();
+ var source = new TaskCompletionSource<IEnumerable<string>>();
+ geocoder.ReverseGeocodeLocation(location, (placemarks, error) =>
+ {
+ if (placemarks == null)
+ placemarks = new CLPlacemark[0];
+ IEnumerable<string> addresses = placemarks.Select(p => ABAddressFormatting.ToString(p.AddressDictionary, false));
+ source.SetResult(addresses);
+ });
+ return source.Task;
+ }
+
+ static Task<IEnumerable<Position>> GetPositionsForAddressAsync(string address)
+ {
+ var geocoder = new CLGeocoder();
+ var source = new TaskCompletionSource<IEnumerable<Position>>();
+ geocoder.GeocodeAddress(address, (placemarks, error) =>
+ {
+ if (placemarks == null)
+ placemarks = new CLPlacemark[0];
+ IEnumerable<Position> positions = placemarks.Select(p => new Position(p.Location.Coordinate.Latitude, p.Location.Coordinate.Longitude));
+ source.SetResult(positions);
+ });
+ return source.Task;
+ }
+ }
+} \ No newline at end of file