Skip to content

Commit

Permalink
Fix #42 implement Geocoder in iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
amay077 committed Jul 19, 2016
1 parent 590b881 commit 26f30a4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using System;
using Google.Maps;
using Xamarin.Forms.GoogleMaps.iOS;

namespace Xamarin
{
public static class FormsGoogleMaps
{
public static void Init(string apiKey)
{
MapServices.ProvideAPIKey(apiKey);
GeocoderBackend.Register();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.GoogleMaps.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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<Compile Include="TouchAsyncTileLayer.cs" />
<Compile Include="TouchSyncTileLayer.cs" />
<Compile Include="Extensions\BitmapDescriptorExtensions.cs" />
<Compile Include="GeocoderBackend.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down

0 comments on commit 26f30a4

Please sign in to comment.