-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from guscatalano/milacher/mappicker-v2
Picker is added
- Loading branch information
Showing
8 changed files
with
198 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 14 additions & 14 deletions
28
Input/WlihaInputUI/WlihaInputUI/WlihaInputUI.Android/Properties/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.WlihaInputUI" android:installLocation="auto"> | ||
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" /> | ||
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" /> | ||
<application android:label="WLIHA Evictions"> | ||
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyBFf95FynaLaxo3XFyQFvsTqjGEnnQylAk" /> | ||
<provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true"> | ||
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data> | ||
</provider> | ||
</provider> | ||
</application> | ||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | ||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> | ||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> | ||
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> | ||
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> | ||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> | ||
<uses-permission android:name="android.permission.INTERNET" /> | ||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | ||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> | ||
<uses-permission android:name="android.permission.CAMERA" /> | ||
</manifest> | ||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | ||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> | ||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> | ||
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> | ||
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> | ||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> | ||
<uses-permission android:name="android.permission.INTERNET" /> | ||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | ||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> | ||
<uses-permission android:name="android.permission.CAMERA" /> | ||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 112 additions & 16 deletions
128
Input/WlihaInputUI/WlihaInputUI/WlihaInputUI/AddressPicker.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,182 @@ | ||
using System; | ||
using Plugin.Geolocator; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
using Xamarin.Essentials; | ||
using Xamarin.Forms; | ||
using Xamarin.Forms.Maps; | ||
using Xamarin.Forms.Xaml; | ||
|
||
namespace WlihaInputUI | ||
{ | ||
public class Address | ||
{ | ||
public string streetAndNumber; | ||
public string city; | ||
public string zipcode; | ||
|
||
public override string ToString() | ||
{ | ||
string result = streetAndNumber!=null ? streetAndNumber.Trim() : ""; | ||
if (!String.IsNullOrWhiteSpace(city)) | ||
{ | ||
if (!String.IsNullOrWhiteSpace(result)) result += ", "; | ||
result += city; | ||
} | ||
if (!String.IsNullOrWhiteSpace(zipcode)) | ||
{ | ||
if (!String.IsNullOrWhiteSpace(zipcode)) result += ", "; | ||
result += zipcode; | ||
} | ||
return result; | ||
} | ||
} | ||
|
||
[XamlCompilation(XamlCompilationOptions.Compile)] | ||
public partial class AddressPicker : ContentPage | ||
{ | ||
private ExtendedMap map; | ||
private Pin activePin; | ||
private Geocoder geocoder; | ||
private TaskCompletionSource<string> pickResult; | ||
private TaskCompletionSource<Address> pickResult; | ||
private List<Address> currentCandidates; | ||
|
||
public static async Task<string> ModalPickAddressFromGPS(INavigation where) | ||
public static async Task<Address> ModalPickAddress(INavigation where, Address hint) | ||
{ | ||
var picker = new AddressPicker(); | ||
var picker = new AddressPicker(hint); | ||
|
||
await where.PushModalAsync(picker, true); | ||
var result = await picker.pickResult.Task; | ||
await where.PopModalAsync(); | ||
return result; | ||
} | ||
|
||
public AddressPicker() | ||
public AddressPicker(Address hint) | ||
{ | ||
InitializeComponent(); | ||
|
||
map = new ExtendedMap( | ||
MapSpan.FromCenterAndRadius( | ||
new Position(47.60357, -122.32945), | ||
new Distance(3000) | ||
) | ||
) | ||
{ | ||
MapType = MapType.Street, | ||
IsShowingUser = true | ||
}; | ||
map.Tap += Map_Tap; | ||
layout.Children.Insert(0, map); | ||
|
||
activePin = new Pin(); | ||
geocoder = new Geocoder(); | ||
pickResult = new TaskCompletionSource<string>(); | ||
pickResult = new TaskCompletionSource<Address>(); | ||
|
||
map.Tap += Map_Tap; | ||
if ((hint!=null) && (hint.ToString() != "")) | ||
{ | ||
CrossGeolocator.Current.GetPositionsForAddressAsync(hint.ToString()).ContinueWith( | ||
(result) => Device.BeginInvokeOnMainThread( | ||
() => | ||
{ | ||
var location = result.Result.FirstOrDefault(); | ||
if (location != null) | ||
{ | ||
UpdatePosition( | ||
new Position(location.Latitude, location.Longitude), | ||
new Distance(Math.Max(location.Accuracy, 100)) | ||
); | ||
} | ||
} | ||
) | ||
); | ||
|
||
} | ||
else | ||
{ | ||
CrossGeolocator.Current.GetPositionAsync().ContinueWith( | ||
(result) => Device.BeginInvokeOnMainThread( | ||
() => | ||
{ | ||
if (result.Result != null) | ||
{ | ||
UpdatePosition( | ||
new Position(result.Result.Latitude, result.Result.Longitude), | ||
new Distance(Math.Max(result.Result.Accuracy, 100)) | ||
); | ||
} | ||
} | ||
) | ||
); | ||
} | ||
} | ||
|
||
private void Map_Tap(object sender, TapEventArgs e) | ||
{ | ||
activePin.Position = e.Position; | ||
UpdatePosition(e.Position, map.VisibleRegion != null ? map.VisibleRegion.Radius : new Distance(300)); | ||
} | ||
|
||
private void UpdatePosition(Xamarin.Forms.Maps.Position position, Distance radius) | ||
{ | ||
activePin.Position = position; | ||
activePin.Label = "Fetching address ..."; | ||
activePin.Type = PinType.Place; | ||
|
||
if (!map.Pins.Contains(activePin)) | ||
{ | ||
map.Pins.Add(activePin); | ||
} | ||
geocoder.GetAddressesForPositionAsync(e.Position).ContinueWith( | ||
map.MoveToRegion(MapSpan.FromCenterAndRadius(position,radius)); | ||
|
||
var geoPos = new Plugin.Geolocator.Abstractions.Position(position.Latitude, position.Longitude); | ||
|
||
CrossGeolocator.Current.GetAddressesForPositionAsync(geoPos).ContinueWith( | ||
(result) => Device.BeginInvokeOnMainThread( | ||
() => ResolveComplete(result.Result) | ||
) | ||
); | ||
} | ||
|
||
private void ResolveComplete(IEnumerable<String> addresses) | ||
private void ResolveComplete(IEnumerable<Plugin.Geolocator.Abstractions.Address> addresses) | ||
{ | ||
currentCandidates = new List<Address>(); | ||
alternativePicker.Items.Clear(); | ||
foreach (var addr in addresses) | ||
{ | ||
alternativePicker.Items.Add(addr); | ||
Address a = new Address(); | ||
a.streetAndNumber = ((addr.SubThoroughfare ?? "") + " " + (addr.Thoroughfare ?? "")).Trim(); | ||
a.city = addr.Locality != null ? addr.Locality.Trim() : ""; | ||
a.zipcode = addr.PostalCode != null ? addr.PostalCode.Trim() : ""; | ||
|
||
if ((a.ToString() != "") && (!currentCandidates.Contains(a))) | ||
{ | ||
currentCandidates.Add(a); | ||
alternativePicker.Items.Add(a.ToString()); | ||
} | ||
} | ||
|
||
if (addresses.Count() == 0) | ||
if (currentCandidates.Count() == 0) | ||
{ | ||
alternativePicker.Items.Add("Failed to find address"); | ||
alternativePicker.Title = "Pick from map"; | ||
activePin.Label = "No address found"; | ||
accept.IsEnabled = false; | ||
} | ||
else | ||
{ | ||
activePin.Label = addresses.First(); | ||
alternativePicker.Title = "Select"; | ||
activePin.Label = alternativePicker.Items.First(); | ||
accept.IsEnabled = true; | ||
} | ||
alternativePicker.SelectedIndex = 0; | ||
} | ||
|
||
private void Accept_Clicked(object sender, EventArgs e) | ||
{ | ||
pickResult.SetResult((string)alternativePicker.SelectedItem); | ||
pickResult.SetResult(currentCandidates[alternativePicker.SelectedIndex]); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.