Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Map renderer extensibility (#844)
Browse files Browse the repository at this point in the history
* Added extensibility features to Maps

Android MapRenderer:
- Moved map initialization to protected virtual method OnMapReady, called by explicit implementation of IOnMapReady
-  Added protected virtual method CreateMarker for customization of pins

iOS MapRenderer:
- Added protected virtual method CreateAnnotation for customization of pins

Pin:
- Unsealed the Pin class
- Made Label a bindable property

* [Docs] Update docs
  • Loading branch information
jcmanke authored and rmarinho committed Apr 11, 2017
1 parent b832b67 commit dfda41b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 37 deletions.
48 changes: 30 additions & 18 deletions Xamarin.Forms.Maps.Android/MapRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,33 @@ protected override void OnLayout(bool changed, int l, int t, int r, int b)
MoveToRegion(Element.LastMoveToRegion, false);
}
}

protected virtual void OnMapReady(GoogleMap map)
{
if (map == null)
{
return;
}

map.SetOnCameraChangeListener(this);
map.InfoWindowClick += MapOnMarkerClick;

map.UiSettings.ZoomControlsEnabled = Map.HasZoomEnabled;
map.UiSettings.ZoomGesturesEnabled = Map.HasZoomEnabled;
map.UiSettings.ScrollGesturesEnabled = Map.HasScrollEnabled;
map.MyLocationEnabled = map.UiSettings.MyLocationButtonEnabled = Map.IsShowingUser;
SetMapType();
}

protected virtual MarkerOptions CreateMarker(Pin pin)
{
var opts = new MarkerOptions();
opts.SetPosition(new LatLng(pin.Position.Latitude, pin.Position.Longitude));
opts.SetTitle(pin.Label);
opts.SetSnippet(pin.Address);

return opts;
}

void AddPins(IList pins)
{
Expand All @@ -198,10 +225,7 @@ void AddPins(IList pins)
_markers.AddRange(pins.Cast<Pin>().Select(p =>
{
Pin pin = p;
var opts = new MarkerOptions();
opts.SetPosition(new LatLng(pin.Position.Latitude, pin.Position.Longitude));
opts.SetTitle(pin.Label);
opts.SetSnippet(pin.Address);
var opts = CreateMarker(pin);
var marker = map.AddMarker(opts);
// associate pin with marker for later lookup in event handlers
Expand Down Expand Up @@ -367,19 +391,7 @@ void UpdateVisibleRegion(LatLng pos)
void IOnMapReadyCallback.OnMapReady(GoogleMap map)
{
NativeMap = map;
if (map == null)
{
return;
}

map.SetOnCameraChangeListener(this);
map.InfoWindowClick += MapOnMarkerClick;

map.UiSettings.ZoomControlsEnabled = Map.HasZoomEnabled;
map.UiSettings.ZoomGesturesEnabled = Map.HasZoomEnabled;
map.UiSettings.ScrollGesturesEnabled = Map.HasScrollEnabled;
map.MyLocationEnabled = map.UiSettings.MyLocationButtonEnabled = Map.IsShowingUser;
SetMapType();
OnMapReady(map);
}
}
}
}
16 changes: 12 additions & 4 deletions Xamarin.Forms.Maps.iOS/MapRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,16 @@ public override void Layout()
}
#endif

protected virtual IMKAnnotation CreateAnnotation(Pin pin)
{
return new MKPointAnnotation
{
Title = pin.Label,
Subtitle = pin.Address ?? "",
Coordinate = new CLLocationCoordinate2D(pin.Position.Latitude, pin.Position.Longitude)
};
}

void UpdateRegion()
{
if (_shouldUpdateRegion)
Expand All @@ -319,10 +329,8 @@ void AddPins(IList pins)
{
foreach (Pin pin in pins)
{
var annotation = new MKPointAnnotation { Title = pin.Label, Subtitle = pin.Address ?? "" };

var annotation = CreateAnnotation(pin);
pin.Id = annotation;
annotation.SetCoordinate(new CLLocationCoordinate2D(pin.Position.Latitude, pin.Position.Longitude));
((MKMapView)Control).AddAnnotation(annotation);
}
}
Expand Down Expand Up @@ -414,4 +422,4 @@ void UpdateMapType()
}
}
}
}
}
19 changes: 6 additions & 13 deletions Xamarin.Forms.Maps/Pin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

namespace Xamarin.Forms.Maps
{
public sealed class Pin : BindableObject
public class Pin : BindableObject
{
public static readonly BindableProperty TypeProperty = BindableProperty.Create("Type", typeof(PinType), typeof(Pin), default(PinType));

public static readonly BindableProperty PositionProperty = BindableProperty.Create("Position", typeof(Position), typeof(Pin), default(Position));

public static readonly BindableProperty AddressProperty = BindableProperty.Create("Address", typeof(string), typeof(Pin), default(string));

// introduced to store the unique id for Android markers

string _label;
public static readonly BindableProperty LabelProperty = BindableProperty.Create("Label", typeof(string), typeof(Pin), default(string));

public string Address
{
Expand All @@ -22,14 +20,8 @@ public string Address

public string Label
{
get { return _label; }
set
{
if (_label == value)
return;
_label = value;
OnPropertyChanged();
}
get { return (string)GetValue(LabelProperty); }
set { SetValue(LabelProperty, value); }
}

public Position Position
Expand All @@ -44,6 +36,7 @@ public PinType Type
set { SetValue(TypeProperty, value); }
}

// introduced to store the unique id for Android markers
internal object Id { get; set; }

public event EventHandler Clicked;
Expand Down Expand Up @@ -96,4 +89,4 @@ bool Equals(Pin other)
return string.Equals(Label, other.Label) && Equals(Position, other.Position) && Type == other.Type && string.Equals(Address, other.Address);
}
}
}
}
19 changes: 17 additions & 2 deletions docs/Xamarin.Forms.Maps/Xamarin.Forms.Maps/Pin.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Type Name="Pin" FullName="Xamarin.Forms.Maps.Pin">
<TypeSignature Language="C#" Value="public sealed class Pin : Xamarin.Forms.BindableObject" />
<TypeSignature Language="ILAsm" Value=".class public auto ansi sealed beforefieldinit Pin extends Xamarin.Forms.BindableObject" />
<TypeSignature Language="C#" Value="public class Pin : Xamarin.Forms.BindableObject" />
<TypeSignature Language="ILAsm" Value=".class public auto ansi beforefieldinit Pin extends Xamarin.Forms.BindableObject" />
<AssemblyInfo>
<AssemblyName>Xamarin.Forms.Maps</AssemblyName>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
Expand Down Expand Up @@ -191,6 +191,21 @@ public static Page GetMapPage ()
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName="LabelProperty">
<MemberSignature Language="C#" Value="public static readonly Xamarin.Forms.BindableProperty LabelProperty;" />
<MemberSignature Language="ILAsm" Value=".field public static initonly class Xamarin.Forms.BindableProperty LabelProperty" />
<MemberType>Field</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Xamarin.Forms.BindableProperty</ReturnType>
</ReturnValue>
<Docs>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName="op_Equality">
<MemberSignature Language="C#" Value="public static bool op_Equality (Xamarin.Forms.Maps.Pin left, Xamarin.Forms.Maps.Pin right);" />
<MemberSignature Language="ILAsm" Value=".method public static hidebysig specialname bool op_Equality(class Xamarin.Forms.Maps.Pin left, class Xamarin.Forms.Maps.Pin right) cil managed" />
Expand Down

0 comments on commit dfda41b

Please sign in to comment.