How do i add multiple markers in xamarin using for each loop with a list model

234 Views Asked by At

I am trying to add pins on my Xamarin android application. I have already set up the map and I need to create pins from a list array that contains the latitudes and longitudes. I know it's quite simple but I'm stuck here.

Below is my sample code.

public class Farmers
    {
        public int farmers_id { get; set; }
        public string Farmer_name { get; set; }

        public double Long { get; set; }
        public double Lat { get; set; }
      }  

        private void AddMarker()
        {
            List<Farmers> farmers =GetFarmers();


            foreach (Farmers p in farmers)
            {
                //HOW TO ADD NEW PINS FOR EACH FARMER ON THE MAP WITH FARMER NAME AS LABEL

            }
        }

I'm using Xamarin.Forms.Maps. The issue is more of how to loop.

1

There are 1 best solutions below

1
On

create a pin and add it to the map

foreach (Farmers p in farmers)
{
  var pin = new Pin();

  pin.Label = p.Farmer_name;
  pin.Type = PinType.Place;
  pin.Position = new Position(p.Lat,p.Long);

  myMap.Pins.Add(pin);
}