Skip to content

Commit

Permalink
Fix Android maps not setting initial state (#10739) Fixes #10394
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuarezruiz committed Oct 17, 2022
1 parent 044f902 commit e7be2f9
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 62 deletions.
80 changes: 19 additions & 61 deletions src/Core/maps/src/Handlers/Map/MapHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Android.Gms.Common.Apis;
using Android.Gms.Maps;
using Android.Gms.Maps.Model;
using Android.OS;
using Java.Lang;
using Java.Util.Logging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Graphics.Platform;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Maps.Platform;
using Microsoft.Maui.Platform;
using ACircle = Android.Gms.Maps.Model.Circle;
using APolygon = Android.Gms.Maps.Model.Polygon;
Expand Down Expand Up @@ -77,56 +80,31 @@ protected override MapView CreatePlatformView()
public static void MapMapType(IMapHandler handler, IMap map)
{
GoogleMap? googleMap = handler?.Map;
if (googleMap == null)
return;

googleMap.MapType = map.MapType switch
{
MapType.Street => GoogleMap.MapTypeNormal,
MapType.Satellite => GoogleMap.MapTypeSatellite,
MapType.Hybrid => GoogleMap.MapTypeHybrid,
_ => throw new ArgumentOutOfRangeException(),
};
googleMap?.UpdateMapType(map);
}

public static void MapIsShowingUser(IMapHandler handler, IMap map)
{
GoogleMap? googleMap = handler?.Map;
if (googleMap == null)
return;

if (handler?.MauiContext?.Context == null)
return;

SetIsShowingUser(handler, map, googleMap).FireAndForget(handler);
googleMap?.UpdateIsShowingUser(map, handler?.MauiContext);
}

public static void MapIsScrollEnabled(IMapHandler handler, IMap map)
{
GoogleMap? googleMap = handler?.Map;
if (googleMap == null)
return;

googleMap.UiSettings.ScrollGesturesEnabled = map.IsScrollEnabled;
googleMap?.UpdateIsScrollEnabled(map);
}

public static void MapIsTrafficEnabled(IMapHandler handler, IMap map)
{
GoogleMap? googleMap = handler?.Map;
if (googleMap == null)
return;

googleMap.TrafficEnabled = map.IsTrafficEnabled;
googleMap?.UpdateIsTrafficEnabled(map);
}

public static void MapIsZoomEnabled(IMapHandler handler, IMap map)
{
GoogleMap? googleMap = handler?.Map;
if (googleMap == null)
return;

googleMap.UiSettings.ZoomControlsEnabled = map.IsZoomEnabled;
googleMap.UiSettings.ZoomGesturesEnabled = map.IsZoomEnabled;
googleMap?.UpdateIsZoomEnabled(map);
}

public static void MapMoveToRegion(IMapHandler handler, IMap map, object? arg)
Expand Down Expand Up @@ -304,9 +282,20 @@ internal void OnMapReady(GoogleMap map)
Map = map;

map.SetOnCameraMoveListener(_mapReady);

map.MarkerClick += OnMarkerClick;
map.InfoWindowClick += OnInfoWindowClick;
map.MapClick += OnMapClick;

if (VirtualView != null)
{
map.UpdateMapType(VirtualView);
map.UpdateIsShowingUser(VirtualView, MauiContext);
map.UpdateIsScrollEnabled(VirtualView);
map.UpdateIsTrafficEnabled(VirtualView);
map.UpdateIsZoomEnabled(VirtualView);
}

InitialUpdate();
}

Expand Down Expand Up @@ -573,37 +562,6 @@ void AddCircle(ICircleMapElement circle)

_circles.Add(nativeCircle);
}

static async Task SetIsShowingUser(IMapHandler handler, IMap map, GoogleMap googleMap)
{
if (map.IsShowingUser)
{
var locationStatus = await ApplicationModel.Permissions.CheckStatusAsync<Microsoft.Maui.ApplicationModel.Permissions.LocationWhenInUse>();

if (locationStatus == ApplicationModel.PermissionStatus.Granted)
{
googleMap.MyLocationEnabled = googleMap.UiSettings.MyLocationButtonEnabled = true;
}
else
{
var locationResult = await Microsoft.Maui.ApplicationModel.Permissions.RequestAsync<Microsoft.Maui.ApplicationModel.Permissions.LocationWhenInUse>();
if (locationResult == ApplicationModel.PermissionStatus.Granted)
{
googleMap.MyLocationEnabled = googleMap.UiSettings.MyLocationButtonEnabled = true;
return;
}
else
{
handler.MauiContext?.Services.GetService<ILogger<MapHandler>>()?.LogWarning("Missing location permissions for IsShowingUser");
googleMap.MyLocationEnabled = googleMap.UiSettings.MyLocationButtonEnabled = false;
}
}
}
else
{
googleMap.MyLocationEnabled = googleMap.UiSettings.MyLocationButtonEnabled = false;
}
}
}

class MapCallbackHandler : Java.Lang.Object, GoogleMap.IOnCameraMoveListener, IOnMapReadyCallback
Expand Down
93 changes: 93 additions & 0 deletions src/Core/maps/src/Platform/Android/MapExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Threading.Tasks;
using Android.Gms.Maps;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.Maps.Handlers;

namespace Microsoft.Maui.Maps.Platform
{
public static class MapExtensions
{
public static void UpdateMapType(this GoogleMap googleMap, IMap map)
{
if (googleMap == null)
return;

googleMap.MapType = map.MapType switch
{
MapType.Street => GoogleMap.MapTypeNormal,
MapType.Satellite => GoogleMap.MapTypeSatellite,
MapType.Hybrid => GoogleMap.MapTypeHybrid,
_ => throw new ArgumentOutOfRangeException(),
};
}

public static void UpdateIsShowingUser(this GoogleMap googleMap, IMap map, IMauiContext? mauiContext)
{
if (googleMap == null)
return;

if (mauiContext?.Context == null)
return;

googleMap.SetIsShowingUser(map, mauiContext).FireAndForget();
}

public static void UpdateIsScrollEnabled(this GoogleMap googleMap, IMap map)
{
if (googleMap == null)
return;

googleMap.UiSettings.ScrollGesturesEnabled = map.IsScrollEnabled;
}

public static void UpdateIsTrafficEnabled(this GoogleMap googleMap, IMap map)
{
if (googleMap == null)
return;

googleMap.TrafficEnabled = map.IsTrafficEnabled;
}

public static void UpdateIsZoomEnabled(this GoogleMap googleMap, IMap map)
{
if (googleMap == null)
return;

googleMap.UiSettings.ZoomControlsEnabled = map.IsZoomEnabled;
googleMap.UiSettings.ZoomGesturesEnabled = map.IsZoomEnabled;
}

internal static async Task SetIsShowingUser(this GoogleMap googleMap, IMap map, IMauiContext? mauiContext)
{
if (map.IsShowingUser)
{
var locationStatus = await ApplicationModel.Permissions.CheckStatusAsync<ApplicationModel.Permissions.LocationWhenInUse>();

if (locationStatus == ApplicationModel.PermissionStatus.Granted)
{
googleMap.MyLocationEnabled = googleMap.UiSettings.MyLocationButtonEnabled = true;
}
else
{
var locationResult = await ApplicationModel.Permissions.RequestAsync<ApplicationModel.Permissions.LocationWhenInUse>();
if (locationResult == ApplicationModel.PermissionStatus.Granted)
{
googleMap.MyLocationEnabled = googleMap.UiSettings.MyLocationButtonEnabled = true;
return;
}
else
{
mauiContext?.Services.GetService<ILogger<MapHandler>>()?.LogWarning("Missing location permissions for IsShowingUser");
googleMap.MyLocationEnabled = googleMap.UiSettings.MyLocationButtonEnabled = false;
}
}
}
else
{
googleMap.MyLocationEnabled = googleMap.UiSettings.MyLocationButtonEnabled = false;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Microsoft.Maui.Maps.MapType
Microsoft.Maui.Maps.MapType.Hybrid = 2 -> Microsoft.Maui.Maps.MapType
Microsoft.Maui.Maps.MapType.Satellite = 1 -> Microsoft.Maui.Maps.MapType
Microsoft.Maui.Maps.MapType.Street = 0 -> Microsoft.Maui.Maps.MapType
Microsoft.Maui.Maps.Platform.MapExtensions
override Microsoft.Maui.Maps.Distance.Equals(object? obj) -> bool
override Microsoft.Maui.Maps.Distance.GetHashCode() -> int
override Microsoft.Maui.Maps.Handlers.MapElementHandler.CreatePlatformElement() -> Java.Lang.Object!
Expand Down Expand Up @@ -123,4 +124,9 @@ static Microsoft.Maui.Maps.Handlers.MapPinHandler.MapLocation(Microsoft.Maui.Map
static Microsoft.Maui.Maps.Handlers.MapPinHandler.Mapper -> Microsoft.Maui.IPropertyMapper<Microsoft.Maui.Maps.IMapPin!, Microsoft.Maui.Maps.Handlers.IMapPinHandler!>!
static Microsoft.Maui.Maps.MapSpan.FromCenterAndRadius(Microsoft.Maui.Devices.Sensors.Location! center, Microsoft.Maui.Maps.Distance radius) -> Microsoft.Maui.Maps.MapSpan!
static Microsoft.Maui.Maps.MapSpan.operator !=(Microsoft.Maui.Maps.MapSpan? left, Microsoft.Maui.Maps.MapSpan? right) -> bool
static Microsoft.Maui.Maps.MapSpan.operator ==(Microsoft.Maui.Maps.MapSpan? left, Microsoft.Maui.Maps.MapSpan? right) -> bool
static Microsoft.Maui.Maps.MapSpan.operator ==(Microsoft.Maui.Maps.MapSpan? left, Microsoft.Maui.Maps.MapSpan? right) -> bool
static Microsoft.Maui.Maps.Platform.MapExtensions.UpdateIsScrollEnabled(this Android.Gms.Maps.GoogleMap! googleMap, Microsoft.Maui.Maps.IMap! map) -> void
static Microsoft.Maui.Maps.Platform.MapExtensions.UpdateIsShowingUser(this Android.Gms.Maps.GoogleMap! googleMap, Microsoft.Maui.Maps.IMap! map, Microsoft.Maui.IMauiContext? mauiContext) -> void
static Microsoft.Maui.Maps.Platform.MapExtensions.UpdateIsTrafficEnabled(this Android.Gms.Maps.GoogleMap! googleMap, Microsoft.Maui.Maps.IMap! map) -> void
static Microsoft.Maui.Maps.Platform.MapExtensions.UpdateIsZoomEnabled(this Android.Gms.Maps.GoogleMap! googleMap, Microsoft.Maui.Maps.IMap! map) -> void
static Microsoft.Maui.Maps.Platform.MapExtensions.UpdateMapType(this Android.Gms.Maps.GoogleMap! googleMap, Microsoft.Maui.Maps.IMap! map) -> void

0 comments on commit e7be2f9

Please sign in to comment.