Map Center Position Xamarin.Forms

Map Center Position Xamarin.Forms

Hey all after long time without publish here a new post hope it help you.



As you know in nowadays is common that the apps like Uber get the center position of the map view but the Xamarin.Forms standard map don't have a way to get this, so in this case I'll show a good way to get the map center position using a map renderer

ExtendedMap.cs Code

using System;
using Xamarin.Forms.Maps;

namespace MapTest.Controls
{
 public class ExtendedMap:Map
 {
  public ExtendedMap ()
  {
   
  }

  internal Func<Position> NativeGetMapCenterLocation{ get; set; }

  public Position GetMapCenterLocation()
  {
   if (NativeGetMapCenterLocation != null) {
    return NativeGetMapCenterLocation ();
   } else {
    return new Position(0,0);
   }
  }
 }
}

Android ExtendedMapRenderer.cs

using System;
using Xamarin.Forms.Maps.Android;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
using MapTest;
using MapTest.Droid.Renderers;
using MapTest.Controls;

[assembly: ExportRenderer(typeof(ExtendedMap), typeof(ExtendedMapRenderer))]
namespace MapTest.Droid.Renderers
{
 public class ExtendedMapRenderer:MapRenderer
 {
  public ExtendedMapRenderer ()
  {
  }

  protected override void OnElementChanged (ElementChangedEventArgs<view> e)
  {
   base.OnElementChanged (e);
   if (Element == null || Control == null)
    return;

   if (e.OldElement != null) {
    (e.OldElement as ExtendedMap).NativeGetMapCenterLocation = null;
   }

   if (e.NewElement != null) {
    (e.NewElement as ExtendedMap).NativeGetMapCenterLocation = new Func<Position> (GetMapCenterLocation);
   }
  }

  internal Position GetMapCenterLocation()
  {
   //METHOD 1 FORM CameraPosition
   var centerPosition = (Control as global::Android.Gms.Maps.MapView).Map.CameraPosition.Target;

   //METHOD 2 CALCULATE PROJECTION
   var visibleRegion = (Control as global::Android.Gms.Maps.MapView).Map.Projection.VisibleRegion;
   var x = (Control as global::Android.Gms.Maps.MapView).Map.Projection.ToScreenLocation(
    visibleRegion.FarRight);
   var y = (Control as global::Android.Gms.Maps.MapView).Map.Projection.ToScreenLocation(
    visibleRegion.NearLeft);
   var centerPoint = new Android.Graphics.Point(x.X / 2, y.Y / 2);
   var centerFromPoint = (Control as global::Android.Gms.Maps.MapView).Map.Projection.FromScreenLocation(
    centerPoint);

   //BOTH RETURNS THE SAME RESULT
   
   return new Position (centerPosition.Latitude, centerPosition.Longitude);
  }
 }
}

iOS ExtendedMapRenderer.cs

using System;
using Xamarin.Forms.Maps.iOS;
using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
using MapTest;
using MapTest.iOS.Renderers;
using MapTest.Controls;

[assembly: ExportRenderer(typeof(ExtendedMap), typeof(ExtendedMapRenderer))]
namespace MapTest.iOS.Renderers
{
 public class ExtendedMapRenderer:MapRenderer
 {
  public ExtendedMapRenderer ()
  {
  }

  protected override void OnElementChanged (ElementChangedEventArgs<View> e)
  {
   base.OnElementChanged (e);
   if (Element == null || Control == null)
    return;

   if (e.OldElement != null) {
    (e.OldElement as ExtendedMap).NativeGetMapCenterLocation = null;
   }

   if (e.NewElement != null) {
    (e.NewElement as ExtendedMap).NativeGetMapCenterLocation = new Func<Position> (GetMapCenterLocation);
   }
  }

  internal Position GetMapCenterLocation()
  {
   var centerPosition = (Control as MapKit.MKMapView).CenterCoordinate;
   return new Position (centerPosition.Latitude, centerPosition.Longitude);
  }
 }
}

After this now you only need to implement it here an small implementation

public ExtendedMap Map { get; set; }
var CenterPos = Map.GetMapCenterLocation ();
MainPage.DisplayAlert ("Map",$"Center Position\nLat:{CenterPos.Latitude}\nLgn:{CenterPos.Longitude}","Ok");
Map.Pins.Add (new Xamarin.Forms.Maps.Pin () {
 Position = CenterPos,
 Address = "Address",
 Label = "Center Position"
});

Here the result:



If you want to see a full example take a look to my Github: https://github.com/AlejandroRuiz/FormsMapCenter

Hope this will help you see you in another post.

Now you can drink a beer and listen this awesome song :)

Comentarios

  1. I got an error on android renderer, It says:
    'ExtendedMapRenderer.OnElementChanged(ElementChangedEventArgs)': no suitable method found to override

    Any solution?

    ResponderEliminar
    Respuestas
    1. use this:
      var CenterPos = new Position(MyMap.VisibleRegion.Center.Latitude, MyMap.VisibleRegion.Center.Longitude);

      Eliminar
  2. Excelente hace dos años, por si otra persona lo necesita existe:
    var CenterPos = new Position(MyMap.VisibleRegion.Center.Latitude, MyMap.VisibleRegion.Center.Longitude);

    ResponderEliminar
  3. Hi Author just now i found your blog its really awesome. Keep this work. It will more helpful for xamarin app developers.
    Hire affordable Xamarin Developer

    ResponderEliminar

Publicar un comentario

Entradas populares