Skip to content

Commit

Permalink
[Maps] Add polylines and polygons (xamarin#6136)
Browse files Browse the repository at this point in the history
* Add Polyline to Map

* Android Polyline rendering

* iOS polyline renderer

* UWP polyline renderer

* Unregister OnPolylineCollectionChanged in Dispose of UWP renderer

* Add MapElement base class

* Update Android MapRenderer with MapElement

* Update UWP MapRenderer with MapElement

* Update iOS MapRenderer with MapElement

* Add polygons

* Tweak functionality of gallery page

* Rename some things in Android renderer

* Made LoadPolyline and LoadPolygon virtual on UWP

* Fix iOS/Mac MapRenderer compile errors
fixes xamarin#1673
fixes xamarin#5773
  • Loading branch information
jcmanke authored and felipebaltazar committed Oct 16, 2019
1 parent 91bc119 commit a494032
Show file tree
Hide file tree
Showing 11 changed files with 957 additions and 27 deletions.
5 changes: 3 additions & 2 deletions Xamarin.Forms.Controls/CoreGallery.cs
Expand Up @@ -383,6 +383,7 @@ public override string ToString()
new GalleryPageFactory(() => new ListViewDemoPage(), "ListView Demo Gallery - Legacy"),
new GalleryPageFactory(() => new MapGallery(), "Map Gallery - Legacy"),
new GalleryPageFactory(() => new MapWithItemsSourceGallery(), "Map With ItemsSource Gallery - Legacy"),
new GalleryPageFactory(() => new MapElementsGallery(), "Map Elements Gallery - Legacy"),
new GalleryPageFactory(() => new MinimumSizeGallery(), "MinimumSize Gallery - Legacy"),
new GalleryPageFactory(() => new MultiGallery(), "Multi Gallery - Legacy"),
new GalleryPageFactory(() => new NavigationPropertiesGallery(), "Navigation Properties"),
Expand Down Expand Up @@ -423,9 +424,9 @@ public CorePageView(Page rootPage, NavigationBehavior navigationBehavior = Navig
_titleToPage = _pages.ToDictionary(o => o.Title);

// avoid NRE for root pages without NavigationBar
if (navigationBehavior == NavigationBehavior.PushAsync && rootPage.GetType () == typeof (CoreNavigationPage))
if (navigationBehavior == NavigationBehavior.PushAsync && rootPage.GetType() == typeof(CoreNavigationPage))
{
_pages.Insert (0, new GalleryPageFactory(() => new NavigationBarGallery((NavigationPage)rootPage), "NavigationBar Gallery - Legacy"));
_pages.Insert(0, new GalleryPageFactory(() => new NavigationBarGallery((NavigationPage)rootPage), "NavigationBar Gallery - Legacy"));
_pages.Insert(1, new GalleryPageFactory(() => new TitleView(true), "TitleView"));
}

Expand Down
30 changes: 30 additions & 0 deletions Xamarin.Forms.Controls/GalleryPages/MapElementsGallery.xaml
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="Xamarin.Forms.Controls.GalleryPages.MapElementsGallery"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:maps="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps">
<ContentPage.Content>
<StackLayout>
<maps:Map x:Name="Map"
MapClicked="MapClicked" />
<Picker x:Name="ElementPicker"
Title="MapElement"
SelectedIndexChanged="PickerSelectionChanged">
<Picker.Items>
<x:String>Polyline</x:String>
<x:String>Polygon</x:String>
</Picker.Items>
</Picker>
<Button Clicked="AddClicked"
Text="Add New Element" />
<Button Clicked="RemoveClicked"
Text="Remove Current Element" />
<Button Clicked="ChangeColorClicked"
Text="Change Stroke Color" />
<Button Clicked="ChangeWidthClicked"
Text="Change Stroke Width" />
<Button Clicked="ChangeFillClicked"
Text="Change Fill Color" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
160 changes: 160 additions & 0 deletions Xamarin.Forms.Controls/GalleryPages/MapElementsGallery.xaml.cs
@@ -0,0 +1,160 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Maps;
using Xamarin.Forms.Xaml;

namespace Xamarin.Forms.Controls.GalleryPages
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MapElementsGallery : ContentPage
{
enum SelectedElementType
{
Polyline,
Polygon
}

SelectedElementType _selectedType;

Polyline _polyline;
Polygon _polygon;

Random _random = new Random();

public MapElementsGallery()
{
InitializeComponent();

Map.MoveToRegion(
MapSpan.FromCenterAndRadius(
new Position(39.828152, -98.569817),
Distance.FromMiles(1681)));

_polyline = new Polyline
{
Geopath =
{
new Position(47.641944, -122.127222),
new Position(37.411625, -122.071327),
new Position(35.138901, -80.922623)
}
};

_polygon = new Polygon
{
StrokeColor = Color.FromHex("#002868"),
FillColor = Color.FromHex("#88BF0A30"),
Geopath =
{
new Position(37, -102.05),
new Position(37, -109.05),
new Position(41, -109.05),
new Position(41, -102.05)
}
};

Map.MapElements.Add(_polyline);
Map.MapElements.Add(_polygon);

ElementPicker.SelectedIndex = 0;
}

void MapClicked(object sender, MapClickedEventArgs e)
{
switch (_selectedType)
{
case SelectedElementType.Polyline:
_polyline.Geopath.Add(e.Position);
break;
case SelectedElementType.Polygon:
_polygon.Geopath.Add(e.Position);
break;
}
}

void PickerSelectionChanged(object sender, EventArgs e)
{
Enum.TryParse((string)ElementPicker.SelectedItem, out _selectedType);
}

void AddClicked(object sender, EventArgs e)
{
switch (_selectedType)
{
case SelectedElementType.Polyline:
Map.MapElements.Add(_polyline = new Polyline());
break;
case SelectedElementType.Polygon:
Map.MapElements.Add(_polygon = new Polygon());
break;
}
}

void RemoveClicked(object sender, EventArgs e)
{
switch (_selectedType)
{
case SelectedElementType.Polyline:
Map.MapElements.Remove(_polyline);
_polyline = Map.MapElements.OfType<Polyline>().LastOrDefault();

if (_polyline == null)
Map.MapElements.Add(_polyline = new Polyline());

break;
case SelectedElementType.Polygon:
Map.MapElements.Remove(_polygon);
_polygon = Map.MapElements.OfType<Polygon>().LastOrDefault();

if (_polygon == null)
Map.MapElements.Add(_polygon = new Polygon());

break;
}
}

void ChangeColorClicked(object sender, EventArgs e)
{
var newColor = new Color(_random.NextDouble(), _random.NextDouble(), _random.NextDouble());
switch (_selectedType)
{
case SelectedElementType.Polyline:
_polyline.StrokeColor = newColor;
break;
case SelectedElementType.Polygon:
_polygon.StrokeColor = newColor;
break;
}
}

void ChangeWidthClicked(object sender, EventArgs e)
{
var newWidth = _random.Next(1, 50);
switch (_selectedType)
{
case SelectedElementType.Polyline:
_polyline.StrokeWidth = newWidth;
break;
case SelectedElementType.Polygon:
_polygon.StrokeWidth = newWidth;
break;
}
}

void ChangeFillClicked(object sender, EventArgs e)
{
var newColor = new Color(_random.NextDouble(), _random.NextDouble(), _random.NextDouble(), _random.NextDouble());
switch (_selectedType)
{
case SelectedElementType.Polygon:
_polygon.FillColor = newColor;
break;
}
}
}
}
3 changes: 3 additions & 0 deletions Xamarin.Forms.Controls/Xamarin.Forms.Controls.csproj
Expand Up @@ -83,6 +83,9 @@
<EmbeddedResource Update="GalleryPages\CollectionViewGalleries\SelectionGalleries\SelectionChangedCommandParameter.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="GalleryPages\MapElementsGallery.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="GalleryPages\MapGallery.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
Expand Down

0 comments on commit a494032

Please sign in to comment.