xamarin.formsmapsui

MapsUI always shows "own location" marker at LatLng 0,0


I have the issue with MapsUI 2.0.3 that the marker of my own location is always at 0,0 west of Africa. Clicking the focus on own location button obviously also moves me there.

I did add coarse and fine location permissions to manifest and do get permission to access locations, but "you are here" is always 0,0.

I then downloaded the MapsUI repository, and tried the samples, which show the same behavior, mostly. When the map loads, it shows position at 0,0. If I drag the map slightly, the marker slowly moves to my correct position. If I (in the samples) reload the same sample or another one, the marker remains stuck at 0,0, even when I drag the map. In summary, I can "fix" the 0,0 marker by interacting with the map but only once.

My device does have gps location enabled, and other location using apps work fine. This including other home made Xamarin forms apps, so this is an issue for MapsUI only.

It failing for both samples and own code makes this a bit confusing. Does this ring a bell for anyone? Seems a bit strange to say the least.

Note that this also fails on the xamagin/android device emulator with a set position. Just mentioning this as a "fun" extra detail, map longpress event will never fire on my device, but does work on the emulator. I saw someone else, a long time back, complaining about that same issue, and a developer commenting on it being fixed in 2.0, while I see it in 2.3. All in all MapsUI seems like an extremely rich system that I would love to use, but which has weird little bugs and poor support.

The xaml for adding the mapview

<StackLayout>
  <mapsui:MapView x:Name="mapView" IsMyLocationButtonVisible="True" />
</StackLayout>

And the c# setup

void start()
{
  var status = await Permissions.RequestAsync<Permissions.LocationAlways>();
  if(status==PermissionStatus.Denied) return;  
  
var map = new Mapsui.Map
  {
    Transformation = new MinimalTransformation(),CRS = "EPSG:3857"};
    
    var tileSource = new HttpTileSource(new GlobalSphericalMercator(),
      "https://tile.thunderforest.com/landscape/{z}/{x}/{y}.png? 
      apikey=xxxxxxxxx",new[] { "a", "b", "c" }, name: "OpenStreetMap");
    
    var tileLayer = new TileLayer(tileSource) { Name = "Carto Light" };
    map.Layers.Add(tileLayer);
    mapView.Map = map;
}

Solution

  • Although not knowing why MyLocationButton not works, but there is a workaround to make the current location to show in MapsUI.

    There is a UpdateMyLocation method inside MyLocationLayer,then we can use this method to show the current location programmatically.

    In addition, you could use Geolocation to get the current location.

    Code as follows:

    protected override async void OnAppearing()
    {
        base.OnAppearing();
      
        var location = await Geolocation.GetLastKnownLocationAsync();
    
        if (location != null)
        {
            Console.WriteLine($"Latitude: {location.Latitude}, Longitude: {location.Longitude}, Altitude: {location.Altitude}");
        }
    
        mapView.MyLocationLayer.UpdateMyLocation(new Position(location.Latitude, location.Longitude), true);
    }
    

    The effect:

    enter image description here