forked from xamarin/Xamarin.Forms
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Maps] Add polylines and polygons (xamarin#6136)
* 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
1 parent
91bc119
commit a494032
Showing
11 changed files
with
957 additions
and
27 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
30 changes: 30 additions & 0 deletions
30
Xamarin.Forms.Controls/GalleryPages/MapElementsGallery.xaml
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 |
---|---|---|
@@ -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
160
Xamarin.Forms.Controls/GalleryPages/MapElementsGallery.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 |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} | ||
} |
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.