Add XAML TypeConverters for Map coordinates (Location, MapSpan, Map.Region)#33995
Add XAML TypeConverters for Map coordinates (Location, MapSpan, Map.Region)#33995jfversluis merged 1 commit intonet11.0from
Conversation
Add TypeConverters enabling concise XAML syntax for map coordinates: - LocationTypeConverter: "lat,lon" → Location Enables: <maps:Pin Location="36.9628,-122.0195" /> - MapSpanTypeConverter: "lat,lon,latDeg,lonDeg" → MapSpan Enables: <maps:Map Region="36.9628,-122.0195,0.05,0.05" /> Add Map.Region bindable property (type MapSpan) so the map's visible region can be set declaratively in XAML. Setting Region calls MoveToRegion. Includes 19 unit tests (11 Location + 8 MapSpan) covering round-trip, edge cases, and error handling. Verified on iOS with Appium.
There was a problem hiding this comment.
Pull request overview
This PR improves the .NET MAUI Maps/XAML authoring experience by adding TypeConverters for map coordinate types and introducing a bindable Map.Region property so regions can be set declaratively via XAML attributes.
Changes:
- Add
LocationTypeConverterin Essentials and attach it toMicrosoft.Maui.Devices.Sensors.Locationvia[TypeConverter]. - Add
MapSpanTypeConverterin Core Maps and attach it toMicrosoft.Maui.Maps.MapSpanvia[TypeConverter](plus unit tests). - Add
Microsoft.Maui.Controls.Maps.Map.Regionbindable property that callsMoveToRegion()when set, enablingRegion="lat,lon,latDeg,lonDeg"in XAML.
Reviewed changes
Copilot reviewed 28 out of 28 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/Essentials/test/UnitTests/LocationTypeConverter_Tests.cs | Adds unit tests for LocationTypeConverter parsing/round-tripping. |
| src/Essentials/src/Types/LocationTypeConverter.shared.cs | Introduces LocationTypeConverter implementation. |
| src/Essentials/src/Types/Location.shared.cs | Applies [TypeConverter(typeof(LocationTypeConverter))] to Location. |
| src/Essentials/src/PublicAPI/netstandard/PublicAPI.Unshipped.txt | Declares new public API for LocationTypeConverter. |
| src/Essentials/src/PublicAPI/net/PublicAPI.Unshipped.txt | Declares new public API for LocationTypeConverter. |
| src/Essentials/src/PublicAPI/net-windows/PublicAPI.Unshipped.txt | Declares new public API for LocationTypeConverter. |
| src/Essentials/src/PublicAPI/net-tizen/PublicAPI.Unshipped.txt | Declares new public API for LocationTypeConverter. |
| src/Essentials/src/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt | Declares new public API for LocationTypeConverter. |
| src/Essentials/src/PublicAPI/net-ios/PublicAPI.Unshipped.txt | Declares new public API for LocationTypeConverter. |
| src/Essentials/src/PublicAPI/net-android/PublicAPI.Unshipped.txt | Declares new public API for LocationTypeConverter. |
| src/Core/maps/src/Converters/MapSpanTypeConverter.cs | Introduces MapSpanTypeConverter implementation. |
| src/Core/maps/src/Primitives/MapSpan.cs | Applies [TypeConverter(typeof(MapSpanTypeConverter))] to MapSpan. |
| src/Core/maps/src/PublicAPI/netstandard/PublicAPI.Unshipped.txt | Declares new public API for MapSpanTypeConverter. |
| src/Core/maps/src/PublicAPI/net/PublicAPI.Unshipped.txt | Declares new public API for MapSpanTypeConverter. |
| src/Core/maps/src/PublicAPI/net-windows/PublicAPI.Unshipped.txt | Declares new public API for MapSpanTypeConverter. |
| src/Core/maps/src/PublicAPI/net-tizen/PublicAPI.Unshipped.txt | Declares new public API for MapSpanTypeConverter. |
| src/Core/maps/src/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt | Declares new public API for MapSpanTypeConverter. |
| src/Core/maps/src/PublicAPI/net-ios/PublicAPI.Unshipped.txt | Declares new public API for MapSpanTypeConverter. |
| src/Core/maps/src/PublicAPI/net-android/PublicAPI.Unshipped.txt | Declares new public API for MapSpanTypeConverter. |
| src/Controls/tests/Core.UnitTests/MapSpanTypeConverterTests.cs | Adds unit tests for MapSpanTypeConverter parsing/round-tripping. |
| src/Controls/Maps/src/Map.cs | Adds new bindable Region property which calls MoveToRegion() on change. |
| src/Controls/Maps/src/PublicAPI/netstandard/PublicAPI.Unshipped.txt | Declares new public API for Map.Region. |
| src/Controls/Maps/src/PublicAPI/net/PublicAPI.Unshipped.txt | Declares new public API for Map.Region. |
| src/Controls/Maps/src/PublicAPI/net-windows/PublicAPI.Unshipped.txt | Declares new public API for Map.Region. |
| src/Controls/Maps/src/PublicAPI/net-tizen/PublicAPI.Unshipped.txt | Declares new public API for Map.Region. |
| src/Controls/Maps/src/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt | Declares new public API for Map.Region. |
| src/Controls/Maps/src/PublicAPI/net-ios/PublicAPI.Unshipped.txt | Declares new public API for Map.Region. |
| src/Controls/Maps/src/PublicAPI/net-android/PublicAPI.Unshipped.txt | Declares new public API for Map.Region. |
| /// <summary> | ||
| /// Gets or sets the region displayed by the map. Setting this property moves the map to the specified region. | ||
| /// This is a bindable property. | ||
| /// </summary> | ||
| public MapSpan? Region | ||
| { | ||
| get { return (MapSpan?)GetValue(RegionProperty); } | ||
| set { SetValue(RegionProperty, value); } |
There was a problem hiding this comment.
Region is described as “the region displayed by the map”, but it is only used as an input to call MoveToRegion() when set. It is not kept in sync with user panning/zooming (that state is exposed via VisibleRegion). This makes the public API contract misleading—please clarify the XML docs (or consider syncing Region from VisibleRegion without re-invoking MoveToRegion to avoid feedback loops).
|
🚨 API change(s) detected @davidortinau FYI |
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
Description
Adds TypeConverters that enable concise XAML syntax for map coordinates, eliminating the need for verbose
x:Argumentsmarkup.Before (verbose):
After (concise):
Changes
LocationTypeConverter- Converts"latitude,longitude"string toLocationobject[TypeConverter]attribute toLocationclassLocation)MapSpanTypeConverter- Converts"lat,lon,latDeg,lonDeg"string toMapSpanobject[TypeConverter]attribute toMapSpanclassMap.Regionbindable property - NewMapSpanproperty onMapcontrolMoveToRegion()automaticallyTesting
LocationTypeConverter, 8 forMapSpanTypeConverter)Part of #33787 (Maps Epic)