Skip to content

Commit ebf2749

Browse files
authored
Merge pull request amay077#506 from amay077/issue_487
Rename: Map.TopLeft/TopRight/BottomLet/BottomRight to Map.Region
2 parents 2c5e6be + 4d0a6f7 commit ebf2749

File tree

10 files changed

+145
-68
lines changed

10 files changed

+145
-68
lines changed

XFGoogleMapSample/XFGoogleMapSample/BoundsTestPage.xaml.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -79,28 +79,28 @@ private void MainMap_CameraIdled(object sender, CameraIdledEventArgs e)
7979
_map.Pins.Add(new Pin()
8080
{
8181
Label = "TL",
82-
Position = _map.TopLeft,
82+
Position = _map.Region.FarLeft,
8383
Rotation = 135f
8484
});
8585

8686
_map.Pins.Add(new Pin()
8787
{
8888
Label = "TR",
89-
Position = _map.TopRight,
89+
Position = _map.Region.FarRight,
9090
Rotation = -135f
9191
});
9292

9393
_map.Pins.Add(new Pin()
9494
{
9595
Label = "BL",
96-
Position = _map.BottomLeft,
96+
Position = _map.Region.NearLeft,
9797
Rotation = 45f
9898
});
9999

100100
_map.Pins.Add(new Pin()
101101
{
102102
Label = "BR",
103-
Position = _map.BottomRight,
103+
Position = _map.Region.NearRight,
104104
Rotation = -45f
105105
});
106106

@@ -114,11 +114,11 @@ private void MainMap_CameraIdled(object sender, CameraIdledEventArgs e)
114114
Icon = BitmapDescriptorFactory.DefaultMarker(Color.Blue)
115115
});
116116

117-
double yMerTl = MercatorProjection.latToY(_map.TopLeft.Latitude);
118-
double xMerTl = MercatorProjection.lonToX(_map.TopLeft.Longitude);
117+
double yMerTl = MercatorProjection.latToY(_map.Region.FarLeft.Latitude);
118+
double xMerTl = MercatorProjection.lonToX(_map.Region.FarLeft.Longitude);
119119

120-
double yMerBr = MercatorProjection.latToY(_map.BottomRight.Latitude);
121-
double xMerBr = MercatorProjection.lonToX(_map.BottomRight.Longitude);
120+
double yMerBr = MercatorProjection.latToY(_map.Region.NearRight.Latitude);
121+
double xMerBr = MercatorProjection.lonToX(_map.Region.NearRight.Longitude);
122122

123123
double xRome = MercatorProjection.lonToX(Rome.Longitude);
124124
double yRome = MercatorProjection.latToY(Rome.Latitude);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using Android.Gms.Maps.Model;
3+
using Xamarin.Forms.GoogleMaps.Android.Extensions;
4+
5+
namespace Xamarin.Forms.GoogleMaps.Android
6+
{
7+
internal static class VisibleRegionExtensions
8+
{
9+
public static MapRegion ToRegion(this VisibleRegion visibleRegion)
10+
{
11+
return new MapRegion(
12+
visibleRegion.NearLeft.ToPosition(),
13+
visibleRegion.NearRight.ToPosition(),
14+
visibleRegion.FarLeft.ToPosition(),
15+
visibleRegion.FarRight.ToPosition()
16+
);
17+
}
18+
}
19+
}

Xamarin.Forms.GoogleMaps/Xamarin.Forms.GoogleMaps.Android/MapRenderer.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -410,12 +410,14 @@ void UpdateVisibleRegion(LatLng pos)
410410
dlat,
411411
dlong
412412
);
413-
// Simone Marra
414-
((Map)Element).TopLeft = new Position(ul.Latitude, ul.Longitude);
415-
((Map)Element).TopRight = new Position(ur.Latitude, ur.Longitude);
416-
((Map)Element).BottomLeft = new Position(ll.Latitude, ll.Longitude);
417-
((Map)Element).BottomRight = new Position(lr.Latitude, lr.Longitude);
418-
// End Simone Marra
413+
//// Simone Marra
414+
//((Map)Element).TopLeft = new Position(ul.Latitude, ul.Longitude);
415+
//((Map)Element).TopRight = new Position(ur.Latitude, ur.Longitude);
416+
//((Map)Element).BottomLeft = new Position(ll.Latitude, ll.Longitude);
417+
//((Map)Element).BottomRight = new Position(lr.Latitude, lr.Longitude);
418+
//// End Simone Marra
419+
///
420+
((Map)Element).Region = projection.VisibleRegion.ToRegion();
419421
}
420422

421423
#region Overridable Members

Xamarin.Forms.GoogleMaps/Xamarin.Forms.GoogleMaps.Android/Xamarin.Forms.GoogleMaps.Android.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
<Compile Include="Logics\DelegateCancelableCallback.cs" />
147147
<Compile Include="Logics\UiSettingsLogic.cs" />
148148
<Compile Include="Extensions\ActivityExtensions.cs" />
149+
<Compile Include="Extensions\VisibleRegionExtensions.cs" />
149150
</ItemGroup>
150151
<ItemGroup>
151152
<None Include="packages.config" />

Xamarin.Forms.GoogleMaps/Xamarin.Forms.GoogleMaps.UWP/MapRenderer.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,12 @@ private void UpdateCornersBounds(MapControl map)
340340

341341
if((topLeft != null) && (topRight != null) && (bottomLeft != null) && (bottomRight != null))
342342
{
343-
Element.TopLeft = new Position(topLeft.Position.Latitude, topLeft.Position.Longitude);
344-
Element.TopRight = new Position(topRight.Position.Latitude, topRight.Position.Longitude);
345-
Element.BottomLeft = new Position(bottomLeft.Position.Latitude, bottomLeft.Position.Longitude);
346-
Element.BottomRight = new Position(bottomRight.Position.Latitude, bottomRight.Position.Longitude);
343+
var farLeft = new Position(topLeft.Position.Latitude, topLeft.Position.Longitude);
344+
var farRight = new Position(topRight.Position.Latitude, topRight.Position.Longitude);
345+
var nearLeft = new Position(bottomLeft.Position.Latitude, bottomLeft.Position.Longitude);
346+
var nearRight = new Position(bottomRight.Position.Latitude, bottomRight.Position.Longitude);
347+
348+
Element.Region = new MapRegion(nearLeft, nearRight, farLeft, farRight);
347349
}
348350
}
349351
// End Simone Marra
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using Google.Maps;
3+
4+
namespace Xamarin.Forms.GoogleMaps.iOS.Extensions
5+
{
6+
internal static class VisibleRegionExtensions
7+
{
8+
public static MapRegion ToRegion(this VisibleRegion visibleRegion)
9+
{
10+
return new MapRegion(
11+
visibleRegion.NearLeft.ToPosition(),
12+
visibleRegion.NearRight.ToPosition(),
13+
visibleRegion.FarLeft.ToPosition(),
14+
visibleRegion.FarRight.ToPosition()
15+
);
16+
}
17+
}
18+
}

Xamarin.Forms.GoogleMaps/Xamarin.Forms.GoogleMaps.iOS/MapRenderer.cs

+26-24
Original file line numberDiff line numberDiff line change
@@ -269,30 +269,32 @@ void OnCameraPositionChanged(GCameraPosition pos)
269269
var maxLon = Math.Max(Math.Max(Math.Max(region.NearLeft.Longitude, region.NearRight.Longitude), region.FarLeft.Longitude), region.FarRight.Longitude);
270270
mapModel.VisibleRegion = new MapSpan(pos.Target.ToPosition(), maxLat - minLat, maxLon - minLon);
271271

272-
// Simone Marra
273-
CoreGraphics.CGPoint topLeftPoint = new CoreGraphics.CGPoint(0, 0);
274-
CoreLocation.CLLocationCoordinate2D topLeftLocation = mkMapView.Projection.CoordinateForPoint(topLeftPoint);
275-
276-
CoreGraphics.CGPoint bottomRightPoint = new CoreGraphics.CGPoint(mkMapView.Frame.Size.Width, mkMapView.Frame.Size.Height);
277-
CoreLocation.CLLocationCoordinate2D bottomRightLocation = mkMapView.Projection.CoordinateForPoint(bottomRightPoint);
278-
279-
CoreGraphics.CGPoint topRightPoint = new CoreGraphics.CGPoint(mkMapView.Frame.Size.Width, 0);
280-
CoreLocation.CLLocationCoordinate2D topRightLocation = mkMapView.Projection.CoordinateForPoint(topRightPoint);
281-
282-
CoreGraphics.CGPoint bottomLeftPoint = new CoreGraphics.CGPoint(0, mkMapView.Frame.Size.Height);
283-
CoreLocation.CLLocationCoordinate2D bottomLeftLocation = mkMapView.Projection.CoordinateForPoint(bottomLeftPoint);
284-
285-
VisibleRegion realVisibleRegion = new VisibleRegion();
286-
realVisibleRegion.FarLeft = topLeftLocation;
287-
realVisibleRegion.FarRight = topRightLocation;
288-
realVisibleRegion.NearLeft = bottomLeftLocation;
289-
realVisibleRegion.NearRight = bottomRightLocation;
290-
291-
((Map)Element).TopLeft = new Position(topLeftLocation.Latitude, topLeftLocation.Longitude);
292-
((Map)Element).TopRight = new Position(topRightLocation.Latitude, topRightLocation.Longitude);
293-
((Map)Element).BottomLeft = new Position(bottomLeftLocation.Latitude, bottomLeftLocation.Longitude);
294-
((Map)Element).BottomRight = new Position(bottomRightLocation.Latitude, bottomRightLocation.Longitude);
295-
// End Simone Marra
272+
//// Simone Marra
273+
//CoreGraphics.CGPoint topLeftPoint = new CoreGraphics.CGPoint(0, 0);
274+
//CoreLocation.CLLocationCoordinate2D topLeftLocation = mkMapView.Projection.CoordinateForPoint(topLeftPoint);
275+
276+
//CoreGraphics.CGPoint bottomRightPoint = new CoreGraphics.CGPoint(mkMapView.Frame.Size.Width, mkMapView.Frame.Size.Height);
277+
//CoreLocation.CLLocationCoordinate2D bottomRightLocation = mkMapView.Projection.CoordinateForPoint(bottomRightPoint);
278+
279+
//CoreGraphics.CGPoint topRightPoint = new CoreGraphics.CGPoint(mkMapView.Frame.Size.Width, 0);
280+
//CoreLocation.CLLocationCoordinate2D topRightLocation = mkMapView.Projection.CoordinateForPoint(topRightPoint);
281+
282+
//CoreGraphics.CGPoint bottomLeftPoint = new CoreGraphics.CGPoint(0, mkMapView.Frame.Size.Height);
283+
//CoreLocation.CLLocationCoordinate2D bottomLeftLocation = mkMapView.Projection.CoordinateForPoint(bottomLeftPoint);
284+
285+
//VisibleRegion realVisibleRegion = new VisibleRegion();
286+
//realVisibleRegion.FarLeft = topLeftLocation;
287+
//realVisibleRegion.FarRight = topRightLocation;
288+
//realVisibleRegion.NearLeft = bottomLeftLocation;
289+
//realVisibleRegion.NearRight = bottomRightLocation;
290+
291+
//((Map)Element).TopLeft = new Position(topLeftLocation.Latitude, topLeftLocation.Longitude);
292+
//((Map)Element).TopRight = new Position(topRightLocation.Latitude, topRightLocation.Longitude);
293+
//((Map)Element).BottomLeft = new Position(bottomLeftLocation.Latitude, bottomLeftLocation.Longitude);
294+
//((Map)Element).BottomRight = new Position(bottomRightLocation.Latitude, bottomRightLocation.Longitude);
295+
//// End Simone Marra
296+
297+
Map.Region = mkMapView.Projection.VisibleRegion.ToRegion();
296298

297299
var camera = pos.ToXamarinForms();
298300
Map.CameraPosition = camera;

Xamarin.Forms.GoogleMaps/Xamarin.Forms.GoogleMaps.iOS/Xamarin.Forms.GoogleMaps.iOS.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
<Compile Include="Extensions\PointExtensions.cs" />
9292
<Compile Include="Extensions\CameraPositionExtensions.cs" />
9393
<Compile Include="Extensions\ThicknessExtension.cs" />
94+
<Compile Include="Extensions\VisibleRegionExtensions.cs" />
9495
<Compile Include="Logics\UiSettingsLogic.cs" />
9596
</ItemGroup>
9697
<ItemGroup>

Xamarin.Forms.GoogleMaps/Xamarin.Forms.GoogleMaps/Map.cs

+20-26
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,14 @@ public class Map : View, IEnumerable<Pin>
8888
internal Action<TakeSnapshotMessage> OnSnapshot{ get; set; }
8989

9090
MapSpan _visibleRegion;
91+
MapRegion _region;
9192

92-
// Simone Marra
93-
public static Position _TopLeft = new Position();
94-
public static Position _TopRight = new Position();
95-
public static Position _BottomLeft = new Position();
96-
public static Position _BottomRight = new Position();
97-
// End Simone Marra
93+
//// Simone Marra
94+
//public static Position _TopLeft = new Position();
95+
//public static Position _TopRight = new Position();
96+
//public static Position _BottomLeft = new Position();
97+
//public static Position _BottomRight = new Position();
98+
//// End Simone Marra
9899

99100
public Map()
100101
{
@@ -221,6 +222,7 @@ public IList<GroundOverlay> GroundOverlays
221222
get { return _groundOverlays; }
222223
}
223224

225+
[Obsolete("Please use Map.Region instead of this")]
224226
public MapSpan VisibleRegion
225227
{
226228
get { return _visibleRegion; }
@@ -236,28 +238,20 @@ internal set
236238
}
237239
}
238240

239-
// Simone Marra
240-
public Position TopLeft
241+
public MapRegion Region
241242
{
242-
get { return _TopLeft; }
243-
internal set { _TopLeft = value; }
244-
}
245-
public Position TopRight
246-
{
247-
get { return _TopRight; }
248-
internal set { _TopRight = value; }
249-
}
250-
public Position BottomLeft
251-
{
252-
get { return _BottomLeft; }
253-
internal set { _BottomLeft = value; }
254-
}
255-
public Position BottomRight
256-
{
257-
get { return _BottomRight; }
258-
internal set { _BottomRight = value; }
243+
get { return _region; }
244+
internal set
245+
{
246+
if (_region == value)
247+
return;
248+
if (value == null)
249+
throw new ArgumentNullException(nameof(value));
250+
OnPropertyChanging();
251+
_region = value;
252+
OnPropertyChanged();
253+
}
259254
}
260-
// End Simone Marra
261255

262256
public UiSettings UiSettings { get; } = new UiSettings();
263257

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
namespace Xamarin.Forms.GoogleMaps
3+
{
4+
public sealed class MapRegion : IEquatable<MapRegion>
5+
{
6+
public Position NearLeft { get; }
7+
public Position NearRight { get; }
8+
public Position FarLeft { get; }
9+
public Position FarRight { get; }
10+
11+
internal MapRegion(Position nearLeft, Position nearRight, Position farLeft, Position farRight)
12+
{
13+
this.NearLeft = nearLeft;
14+
this.NearRight = nearRight;
15+
this.FarLeft = farLeft;
16+
this.FarRight = farRight;
17+
}
18+
19+
public bool Equals(MapRegion other)
20+
{
21+
if (other == null)
22+
{
23+
return false;
24+
}
25+
26+
return NearLeft.Equals(other.NearLeft)
27+
&& NearRight.Equals(other.NearRight)
28+
&& FarLeft.Equals(other.FarLeft)
29+
&& FarRight.Equals(other.FarRight);
30+
}
31+
32+
public override int GetHashCode()
33+
{
34+
return NearLeft.GetHashCode() ^ NearRight.GetHashCode()
35+
^ FarLeft.GetHashCode() ^ FarRight.GetHashCode();
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)