Home > Software engineering >  When comparing coordinates they are not equal and not displaying pins on the map - xamarin.forms.map
When comparing coordinates they are not equal and not displaying pins on the map - xamarin.forms.map

Time:03-26

I have 1919 coordinates in the class:

public class HardcodedLocations
{
    public static List<Position> Positions = new List<Position>
    {
           new Position(41.19197,   25.33719 ),
           new Position(41.26352,   25.1471 ),
           new Position(41.26365,   25.24215 ),
           new Position(41.26369,   25.33719 ),
     }
}

I adding the coordinates in the constructor page:

public AboutPage()
    {
        InitializeComponent();

        List<CustomPin> pins = new List<CustomPin>();

        for (int i = 0; i < HardcodedLocations.Positions.Count; i  )
        {
            CustomPin pin = new CustomPin
            {
                Type = PinType.Place,
                Position = HardcodedLocations.Positions[i],
                Label = "Xamarin San Francisco Office",
                Address = "394 Pacific Ave, San Francisco CA",
                Name = "Xamarin",
                Url = "http://xamarin.com/about/"
            };

            pins.Add(pin);
            customMap.Pins.Add(pin);
            customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(42.8742, 25.3187), Distance.FromMiles(1.0)));
        }
        customMap.CustomPins = pins;

But the markers not displaying because in this method the are not equals:

 CustomPin GetCustomPin(MKPointAnnotation annotation)
    {

        var position = new Position(annotation.Coordinate.Latitude, annotation.Coordinate.Longitude);

        foreach (var pin in customPins)
        {
            if (pin.Position.Latitude == position.Latitude && pin.Position.Longitude == position.Longitude)
            {
                return pin;
            }
        }
        return null;
    }

When I stop the debugger on the if statement in the first iteration pin.Position.Latitude get 0 index from List<Position> from HardcodedLocations class and position.Latitude get 1083 index. pin.Position.Longitude is equal to position.Longitude.

In the second iteration pin.Position.Latitude get 1 index from List<Position> from HardcodedLocations class and position.Latitude get 1083 index again. pin.Position.Longitude get 1 index position.Longitude get different index.

What is the reason for the discrepancies and how could I fix this problem?

CodePudding user response:

Try to compare the two positions directly as the sample shown

CustomPin GetCustomPin(MKPointAnnotation annotation)
{
   var position = new Position(annotation.Coordinate.Latitude, annotation.Coordinate.Longitude);
   foreach (var pin in customPins)
   {
       if (pin.Position == position)
       {
           return pin;
       }
   }
   return null;
}

Sample here : https://github.com/xamarin/xamarin-forms-samples/blob/dacfaec98cf6ddfa587a80a9d9338c14cfb123d1/CustomRenderers/Map/iOS/CustomMapRenderer.cs#L111 .

  • Related