Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to prevent UWP map from automatically panning to user location

stuart_Astuart_A Member ✭✭

Very frustrating.

I have a map with a ZoomToFeatures method which successfully zooms and pans the map such that all elements are shown on screen. It works well on Android.

However... On UWP the method also works fine, but as soon as the device has been given my GPS co-ordinates, it pans to those co-ordinates instead. There doesn't seem to be any way I can override this or tell it not to.

Have I missed something somewhere? How can I prevent this annoying 'feature' from happening? I want to keep the user location showing on the map, so I can't just turn that part off.

Thanks.

Tagged:

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    You can try to disable the location capability:

    1.In Solution Explorer, double-click package.appxmanifest and select the Capabilities tab.
    2.In the Capabilities list, check the box for Location. Try to remove the location device capability to the package manifest file.

        <Capabilities>
        <!-- DeviceCapability elements must follow Capability elements (if present) -->
        <!-- <DeviceCapability Name="location"/> -->
       </Capabilities>
    

    For more information, you can check: https://docs.microsoft.com/en-us/windows/uwp/maps-and-location/get-location

  • stuart_Astuart_A Member ✭✭

    @jezh said:
    You can try to disable the location capability:

    1.In Solution Explorer, double-click package.appxmanifest and select the Capabilities tab.
    2.In the Capabilities list, check the box for Location. Try to remove the location device capability to the package manifest file.

        <Capabilities>
        <!-- DeviceCapability elements must follow Capability elements (if present) -->
        <!-- <DeviceCapability Name="location"/> -->
       </Capabilities>
    

    For more information, you can check: https://docs.microsoft.com/en-us/windows/uwp/maps-and-location/get-location

    I said I want to keep the user location...

  • jezhjezh Member, Xamarin Team Xamurai

    Ok, if it is convenient for you, could you please post a basic demo so that we can test on our side?

  • stuart_Astuart_A Member ✭✭
    edited July 10

    @jezh said:
    Ok, if it is convenient for you, could you please post a basic demo so that we can test on our side?

    Create a view with a map element

    public MapView()
        {
             this.Map = new CustomMap
            {
                IsShowingUser = true,
    
            };
    
            MapArea.Content = Map;
        }
    

    Tell the view to pan to another area when ready

    protected override async void OnAppearing()
    {
        //= await Geolocation.GetLastKnownLocationAsync();
        await this.ZoomMapToFeatures(); 
    }
    

    No matter what order I do this in, the map will just go to the user's GPS spot once the GPS has been gathered.

    The best I can do is utilise await Geolocation.GetLastKnownLocationAsync(); , which means the page pauses until it gets GPS, and then it can pan safely and correctly to the actual desired features.

    However, this only works on the first load of the map. If I go away and then come back to the same map page, it automatically starts on the GPS spot no matter what. Very annoying. This issue doesn't happen on Android so I know it's specific to UWP.

    I'm using a custom map renderer too, but cannot find any native UWP API functions allowing me to deal with this.

Sign In or Register to comment.