Skip to content

Commit

Permalink
Merge pull request #15 from guscatalano/milacher/mappicker-v2
Browse files Browse the repository at this point in the history
Picker is added
  • Loading branch information
mucki-at authored Jul 25, 2019
2 parents 0a34026 + 434e568 commit 531d7ad
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using Android.Views;
using Android.Widget;
using Android.OS;
using Plugin.CurrentActivity;

using Android.Support.V4.Content;
using Android.Support.V4.App;

Expand All @@ -32,18 +34,15 @@ protected override void OnCreate(Bundle savedInstanceState)

Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
CrossCurrentActivity.Current.Init(this, savedInstanceState);
Xamarin.FormsMaps.Init(this, savedInstanceState);
LoadApplication(new App());
}
/*public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}*/
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
{
Plugin.Permissions.PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
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>
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@
<Reference Include="System.Numerics.Vectors" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Plugin.CurrentActivity">
<Version>2.1.0.4</Version>
</PackageReference>
<PackageReference Include="Plugin.Permissions">
<Version>3.0.0.12</Version>
</PackageReference>
<PackageReference Include="Xam.Plugin.Geolocator">
<Version>4.5.0.6</Version>
</PackageReference>
<PackageReference Include="Xam.Plugin.Media">
<Version>4.0.1.5</Version>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
x:Class="WlihaInputUI.AddressPicker"
>
<ContentPage.Content>
<StackLayout VerticalOptions="FillAndExpand">
<Label x:Name="prompt" Text="Pick address on map" />
<local:ExtendedMap x:Name="map" MapType="Street" IsShowingUser="True"/>
<Picker x:Name="alternativePicker" Title="Pick address" IsVisible="True" />
<StackLayout x:Name="layout" VerticalOptions="FillAndExpand">
<Picker x:Name="alternativePicker" Title="Pick from map" IsVisible="True" />
<Button x:Name="accept" Text="Accept" IsEnabled="False" Clicked="Accept_Clicked"/>
</StackLayout>
</ContentPage.Content>
Expand Down
128 changes: 112 additions & 16 deletions Input/WlihaInputUI/WlihaInputUI/WlihaInputUI/AddressPicker.xaml.cs
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]);
}

}
}
24 changes: 7 additions & 17 deletions Input/WlihaInputUI/WlihaInputUI/WlihaInputUI/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,20 @@
FontSize = "20"
FontAttributes="Bold" />
<Entry x:Name="addrStreet"
Placeholder="Number and Street" />
Placeholder="Number and Street"/>
<Entry x:Name="addrUnit"
Placeholder="Unit" />
Placeholder="Unit"/>
<Entry x:Name="addrCity"
Placeholder="City" />
Placeholder="City"/>
<Entry x:Name="addrCounty"
Placeholder="County" />
Placeholder="County"/>
<Entry x:Name="addrZip"
Placeholder="Zip" />
<!--<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Text="Save"
Clicked="OnSaveButtonClicked" />
<Button Grid.Column="1"
Text="Delete"
Clicked="OnDeleteButtonClicked"/>
</Grid>-->
Placeholder="Zip"/>
</StackLayout>
<StackLayout VerticalOptions="CenterAndExpand">
</StackLayout>
<StackLayout VerticalOptions="End">
<StackLayout FlowDirection="LeftToRight" VerticalOptions="End">
<Button Text="Use Map" x:Name="mapButton" HorizontalOptions="Start" Clicked="MapButton_Clicked"/>
<Button Text="Next" x:Name="NextPageAddress" HorizontalOptions="End" Clicked="NextPageAddress_Clicked"/>
</StackLayout>
</StackLayout>
Expand Down
Loading

0 comments on commit 531d7ad

Please sign in to comment.