summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Maps.UWP/GeocoderBackend.cs
blob: 10f624a6c49b6c01f2123b96dad5481c01ba08f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
´╗┐using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.Devices.Geolocation;
using Windows.Services.Maps;

#if WINDOWS_UWP

namespace Xamarin.Forms.Maps.UWP
#else

namespace Xamarin.Forms.Maps.WinRT
#endif
{
	internal class GeocoderBackend
	{
		public static void Register()
		{
			Geocoder.GetPositionsForAddressAsyncFunc = GetPositionsForAddress;
			Geocoder.GetAddressesForPositionFuncAsync = GetAddressesForPositionAsync;
		}

		static string AddressToString(MapAddress address)
		{
			string building = "", house = "", city = "", country = "";

			var bldg = new List<string>();
			if (!"".Equals(address.BuildingRoom))
				bldg.Add(address.BuildingRoom);
			if (!"".Equals(address.BuildingFloor))
				bldg.Add(address.BuildingFloor);
			if (!"".Equals(address.BuildingName))
				bldg.Add(address.BuildingName);
			if (!"".Equals(address.BuildingWing))
				bldg.Add(address.BuildingWing);
			if (bldg.Count > 0)
				building = string.Join(" ", bldg) + Environment.NewLine;

			var hs = new List<string>();
			if (!"".Equals(address.StreetNumber))
				hs.Add(address.StreetNumber);
			if (!"".Equals(address.Street))
				hs.Add(address.Street);
			if (hs.Count > 0)
				house = string.Join(" ", hs) + Environment.NewLine;

			var cs = new List<string>();
			if (!"".Equals(address.Town))
				cs.Add(address.Town);
			if (!"".Equals(address.Neighborhood))
				cs.Add(address.Neighborhood);
			else if (!"".Equals(address.Region))
				cs.Add(address.Region);
			if (!"".Equals(address.PostCode))
				cs.Add(address.PostCode);
			if (cs.Count > 0)
				city = string.Join(" ", cs) + Environment.NewLine;

			if (!"".Equals(address.Country))
				country = address.Country;
			return building + house + city + country;
		}

		static async Task<IEnumerable<string>> GetAddressesForPositionAsync(Position position)
		{
			var queryResults =
				await
					MapLocationFinder.FindLocationsAtAsync(
						new Geopoint(new BasicGeoposition { Latitude = position.Latitude, Longitude = position.Longitude }));
			var addresses = new List<string>();
			foreach (var result in queryResults?.Locations)
				addresses.Add(AddressToString(result.Address));

			return addresses;
		}

		static async Task<IEnumerable<Position>> GetPositionsForAddress(string address)
		{
			var queryResults = await MapLocationFinder.FindLocationsAsync(address, null, 10);
			var positions = new List<Position>();
			foreach (var result in queryResults?.Locations)
				positions.Add(new Position(result.Point.Position.Latitude, result.Point.Position.Longitude));
			return positions;
		}
	}
}