Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
Text="Add Pin"
Clicked="OnAddPinClicked" />
<Button
Text="Move Pin"
Clicked="OnMovePinClicked"/>
<Button
Text="Remove Pin"
Clicked="OnRemovePinClicked" />
<Button
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System;
using Microsoft.Maui.Controls.Maps;
using Microsoft.Maui.Controls.Xaml;
using Microsoft.Maui.Maps;
using Position = Microsoft.Maui.Devices.Sensors.Location;

namespace Maui.Controls.Sample.Pages.MapsGalleries
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MapPinsGallery
{
const double DefaultMapRadiusKm = 5.0;
readonly Random _locationRandomSeed = new();
int _locationIncrement = 0;

Expand Down Expand Up @@ -81,6 +83,11 @@ void OnAddPinClicked(object sender, EventArgs e)
AddPin();
}

void OnMovePinClicked(object sender, EventArgs e)
{
MovePin();
}

void OnRemovePinClicked(object sender, EventArgs e)
{
if (pinsMap.Pins.Count > 0)
Expand All @@ -100,13 +107,34 @@ void OnAdd10PinsClicked(object sender, EventArgs e)

void AddPin()
{
pinsMap.Pins.Add(new Pin()
var randomLocation = GetRandomLocation();
var pin = new Pin
{
Label = $"Location {_locationIncrement++}",
Location = _randomLocations[_locationRandomSeed.Next(0, _randomLocations.Length)],
});
Location = randomLocation,
};
pinsMap.Pins.Add(pin);
MoveMapTo(randomLocation);
}

void MovePin()
{
if (pinsMap.Pins.Count == 0)
{
return;
}

var randomLocation = GetRandomLocation();
pinsMap.Pins[0].Location = randomLocation;
MoveMapTo(randomLocation);
}

Position GetRandomLocation() =>
_randomLocations[_locationRandomSeed.Next(_randomLocations.Length)];

void MoveMapTo(Position location) =>
pinsMap.MoveToRegion(MapSpan.FromCenterAndRadius(location, Distance.FromKilometers(DefaultMapRadiusKm)));

void OnMapClicked(object sender, MapClickedEventArgs e)
{
DisplayAlertAsync("Map", $"Map {e.Location.Latitude}, {e.Location.Longitude} clicked.", "Ok");
Expand Down
81 changes: 73 additions & 8 deletions src/Core/maps/src/Handlers/Map/MapHandler.Android.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Android.Gms.Common.Apis;
Expand Down Expand Up @@ -56,6 +57,8 @@ protected override void ConnectHandler(MapView platformView)

protected override void DisconnectHandler(MapView platformView)
{
DisconnectPins();

base.DisconnectHandler(platformView);
platformView.LayoutChange -= MapViewLayoutChange;

Expand Down Expand Up @@ -260,10 +263,14 @@ public static void MapPins(IMapHandler handler, IMap map)
{
if (handler is MapHandler mapHandler)
{
mapHandler.DisconnectPins();

if (mapHandler._markers != null)
{
for (int i = 0; i < mapHandler._markers.Count; i++)
{
mapHandler._markers[i].Remove();
}

mapHandler._markers = null;
}
Expand Down Expand Up @@ -419,8 +426,7 @@ void AddPins(IList pins)
if (Map == null || MauiContext == null)
return;

if (_markers == null)
_markers = new List<Marker>();
_markers ??= new List<Marker>();

foreach (var p in pins)
{
Expand All @@ -430,20 +436,79 @@ void AddPins(IList pins)
var pinHandler = pin.ToHandler(MauiContext);
if (pinHandler is IMapPinHandler iMapPinHandler)
{
marker = Map.AddMarker(iMapPinHandler.PlatformView);
if (marker == null)
{
throw new System.Exception("Map.AddMarker returned null");
}
marker = Map.AddMarker(iMapPinHandler.PlatformView) ?? throw new System.Exception("Map.AddMarker returned null");
// associate pin with marker for later lookup in event handlers
pin.MarkerId = marker.Id;
_markers.Add(marker!);
}

if (pin is INotifyPropertyChanged observable)
{
observable.PropertyChanged += PinOnPropertyChanged;
}
}
_pins = null;
}

void PinOnPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (sender is not IMapPin pin || _markers == null)
{
return;
}

Marker? marker = null;
if (pin.MarkerId is string markerId)
{
for (int i = 0; i < _markers.Count; i++)
{
if (_markers[i].Id == markerId)
{
marker = _markers[i];
break;
}
}
}

if (marker is null)
{
return;
}

switch (e.PropertyName)
{
case nameof(IMapPin.Location):
if (pin.Location != null)
{
marker.Position = new LatLng(pin.Location.Latitude, pin.Location.Longitude);
}

break;
case nameof(IMapPin.Label):
marker.Title = pin.Label;
break;
case nameof(IMapPin.Address):
marker.Snippet = pin.Address;
break;
}
}

void DisconnectPins()
{
if (VirtualView == null)
return;

for (int i = 0; i < VirtualView.Pins.Count; i++)
{
var pin = VirtualView.Pins[i];
if (pin is INotifyPropertyChanged observable)
{
observable.PropertyChanged -= PinOnPropertyChanged;
}
pin?.Handler?.DisconnectHandler();
}
}

protected IMapPin? GetPinForMarker(Marker marker)
{
IMapPin? targetPin = null;
Expand Down Expand Up @@ -618,4 +683,4 @@ protected override void Dispose(bool disposing)
}
}

}
}
6 changes: 4 additions & 2 deletions src/Core/maps/src/Handlers/MapPin/MapPinHandler.Android.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Android.Gms.Maps;
using Android.Gms.Maps;
using Android.Gms.Maps.Model;
using Microsoft.Maui.Handlers;

Expand All @@ -10,8 +10,10 @@ public partial class MapPinHandler : ElementHandler<IMapPin, MarkerOptions>

public static void MapLocation(IMapPinHandler handler, IMapPin mapPin)
{
if (mapPin.Location != null)
if (mapPin.Location is not null)
{
handler.PlatformView.SetPosition(new LatLng(mapPin.Location.Latitude, mapPin.Location.Longitude));
}
}

public static void MapLabel(IMapPinHandler handler, IMapPin mapPin)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#nullable enable
#nullable enable
Loading