How To implement Move to Region in xamarin forms google map MVVM

amr_kamalamr_kamal Member ✭✭

I want to Update map position and move to region when user select specific city
I do that but no thing changed

this is my code

Location.xaml.cs

 public partial class LocationPage : ContentPage
    {
        public static BindableMap StoresMap;

        public LocationPage()
        {
            StoresMap = LocationMap;
            InitializeComponent();
        }
}

Note : BindableMap is Custom Render form Map and i Used Google Map for this

and in view model

                             MabPosition = new Position(24.774265, 46.738586);
                             var Loc = StoresAddresses.FirstOrDefault();
                             LocationPage.StoresMap.MoveToRegion(new MapSpan(MabPosition, 24.774265, 46.738586));

what's the problem ?

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    Would you mind sharing us more code ? Why do you use a static variable here?

    Check the value of LocationPage.StoresMap to see if it is null or not .

  • amr_kamalamr_kamal Member ✭✭

    @ColeX static instance from view to make me use it in view model

  • seanydaseanyda GBMember ✭✭✭✭✭

    @amr_kamal said:
    @ColeX static instance from view to make me use it in view model

    This is breaking the MVVM pattern. Are you setting a BindingContext on the page so you can do binding from the ViewModel?

  • amr_kamalamr_kamal Member ✭✭

    @seanyda I used prism for this .. can i do that in better way ?

  • amr_kamalamr_kamal Member ✭✭

    @ColeX You're right it's null .. how can i assign it ? code doesn't crash but always null and when i assign poistion in view.cs it always crash

  • amr_kamalamr_kamal Member ✭✭

    @ColeX I want to access move to region method this reason which make me do that

  • amr_kamalamr_kamal Member ✭✭

    BindableMap Code

      public class BindableMap : Map
        {
            public static readonly BindableProperty MapPinsProperty = BindableProperty.Create(
                     nameof(Pins),
                     typeof(ObservableCollection<Pin>),
                     typeof(BindableMap),
                     new ObservableCollection<Pin>(),
                     propertyChanged: (b, o, n) =>
                     {
                         var bindable = (BindableMap)b;
                         bindable.Pins.Clear();
    
                         var collection = (ObservableCollection<Pin>)n;
                         foreach (var item in collection)
                             bindable.Pins.Add(item);
                         collection.CollectionChanged += (sender, e) =>
                         {
                             Device.BeginInvokeOnMainThread(() =>
                             {
                                 switch (e.Action)
                                 {
                                     case NotifyCollectionChangedAction.Add:
                                     case NotifyCollectionChangedAction.Replace:
                                     case NotifyCollectionChangedAction.Remove:
                                         if (e.OldItems != null)
                                             foreach (var item in e.OldItems)
                                                 bindable.Pins.Remove((Pin)item);
                                         if (e.NewItems != null)
                                             foreach (var item in e.NewItems)
                                                 bindable.Pins.Add((Pin)item);
                                         break;
                                     case NotifyCollectionChangedAction.Reset:
                                         bindable.Pins.Clear();
                                         break;
                                 }
                             });
                         };
                     });
            public IList<Pin> MapPins { get; set; }
    
            public static readonly BindableProperty MapPositionProperty = BindableProperty.Create(
                     nameof(MapPosition),
                     typeof(Position),
                     typeof(BindableMap),
                     new Position(0, 0),
                     propertyChanged: (b, o, n) =>
                     {
                         ((BindableMap)b).MoveToRegion(MapSpan.FromCenterAndRadius(
                              (Position)n,
                              Distance.FromMiles(1)));
                     });
    
            public Position MapPosition { get; set; }
    
            public float CornerRaduis { get; set; } = 10;//default
    
    
        }
    
  • amr_kamalamr_kamal Member ✭✭

    any solution ?

  • MuhammadAbuMandourMuhammadAbuMandour USMember ✭✭

    i am trying message center it work but map freeze, am working in it when i reach to solution i will tell you

  • amr_kamalamr_kamal Member ✭✭

    Thank you muhammad :) please let me know if you have any update

  • MuhammadAbuMandourMuhammadAbuMandour USMember ✭✭

    its work, you can use message center to move map visibility region in mvvm without problem

    @amr_kamal said:
    Thank you muhammad :) please let me know if you have any update

  • amr_kamalamr_kamal Member ✭✭

    can you post sample please ?

Sign In or Register to comment.