0

I'm trying to change MapView in my Xamarin Forms app (with Mapsui and Prism), as I need separate view to store Pins. First MapView (let's call it default) is for displaying all pins from list. Second MapView (history) is for displaying new pins, which are removed when leaving Page.

I have Dictionary for storing my MapViews to have easy way to access any view I want. Every created MapView has the same instance of Map. I want to change from default to history which should hide pins that are added in default view.

I change view with this:

public void ChangeView(string name)
        {
            var prev = MapView;
            loggerService.Info($"Pins: {prev.Pins.Count}");
            MapView = GetView(name); // Current MapView in XAML, returns instance of MapView
            loggerService.Info($"New view pins: {MapView.Pins.Count}");

            Task.Factory.StartNew(() => 
            {
                Task.Delay(2000).Wait();

                loggerService.Info($"View pins: {MapView.Pins.Count}");
            });

            MapView.Refresh();
        }

extra code with log is for debugging purposes - it shows correct values (prev pins = 7, new pins = 0, view pins = 0).

I've added history view without Zoom buttons to make sure it is changing - and it is (so there is no need to post XAML, I think).

How should I change MapView to hide pins when navigating to history and show them when I switch back to default view? Or is there a better way to 'group' Pins and hide/show them basing on name?

Update I think this may be important to mention:

public Pin AddMarker(Position point)
        {
            var pin = new Pin(MapView)
            {
                Label = "PinType.Pin",
                Position = point,
                Type = PinType.Pin,
                Transparency = 0.5f,
                Color = Xamarin.Forms.Color.FromRgb(2, 144, 210),
                Scale = 0.5f,
            };

            MapView.Pins.Add(pin);

            return pin;
        }

This is the way I add Pins to view. This is the same MapView as above (all the code is from MapService).

1 Answers1

0

Some options

For the lower level functionality you may need to the samples to get you on your way: https://mapsui.com/documentation/samples.html

pauldendulk
  • 1,378
  • 12
  • 23
  • Thanks for detailed answer. I think the separate layer is the most suitable for my case. But how am I able to hide pins from layer? As I can see, PointsSample covers "points" as bitmap, not built in Pins feature, am I right? – Szymon Jabłoński Apr 02 '19 at 19:52
  • I dived in MapView code and found out that `Pins` is nothing than predefined layer with `ObservableCollectionProvider` containing Pins. I think I will figure the rest out. Thanks a lot! – Szymon Jabłoński Apr 03 '19 at 18:48
  • It is always an option to copy the MapView code to your own project and modify it to your needs. – pauldendulk Apr 05 '19 at 11:27
  • Finally I've changed switching MapViews to switching Layers. Now it's working as expected. – Szymon Jabłoński Apr 12 '19 at 14:24