Home > Enterprise >  How to update pins in xamarin forms maps?
How to update pins in xamarin forms maps?

Time:08-09

I have the following code:

private async void setHelpersPins(object sender, ElapsedEventArgs e)
{
    var temp = await firebaseHelper.GetAllHelperUsers();

    if (map.Pins.Count>0)
    {
        map.Pins.Clear();
    }

    foreach (var user in temp)
    {
        Pin pin = new Pin
        {
            Label = user.Nickname,
            Address = "További információkért kattints ide.",
            Type = PinType.Place,
            Position = new Position(user.Latitude, user.Longitude)
        };                
        map.Pins.Add(pin);
    }
}

It runs every 10second by using Timer.Elapsed.

But it makes the map blinks every time this method runs, which is not great for me.

How can I only update those pin coordinates, where the user's nicknames are the same?

CodePudding user response:

to find the elements of a collection, use a LINQ query

using System.Linq;

....

var pins = map.Pins.Where(x => x.Label == "somevalue").ToList();

then you can iterate over the list to update it

foreach (var p in pins)
{
  p.Position = someNewPosition;
}
  • Related