Map get current location

Hi,first than nothing, I dont speak English very much, so sorry for that.

I'm new in Xamarin, and I'm trying create a Project Xamarin Form and I'm having problems with the Map in my project. I have a component Map but I would like when my app start, the map be in the current position of device. I have severals days but nothing. I tryed this:

    private async Task Button_ClickedAsync(object sender, EventArgs e){
        var locator = CrossGeolocator.Current;
        locator.DesiredAccuracy = 50;

        var location = await locator.GetPositionAsync(TimeSpan.FromTicks(10000));
        Position position = new Position(location.Latitude, location.Longitude);

        MyMap.MoveToRegion(MapSpan.FromCenterAndRadius(position, Distance.FromMiles(3)));

}

Please help me :(

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    If my memory serves there is a Map.SetCenter method you can call - providing your current GPS location.
    That will move the map to your current location.

  • Miki93Miki93 DOMember ✭✭

    @ClintStLaurent said:
    If my memory serves there is a Map.SetCenter method you can call - providing your current GPS location.
    That will move the map to your current location.

    I Don't find that method :/

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @Miki93 said:

    @ClintStLaurent said:
    If my memory serves there is a Map.SetCenter method you can call - providing your current GPS location.
    That will move the map to your current location.

    I Don't find that method :/

    Sorry about that. The HERE map component has .SetCenter and SetZoomLevel. The Google Map web API has .SetCenter but the Xamarin map component doesn't have that command. Sorry - after a while all the different map API tend to blur together in my head.

  • Miki93Miki93 DOMember ✭✭

    in that example is not used the "Geolocator" to localize the current position, but have things very interesting. very thanks.

    @ClintStLaurent said:

    @Miki93 said:

    @ClintStLaurent said:
    If my memory serves there is a Map.SetCenter method you can call - providing your current GPS location.
    That will move the map to your current location.

    I Don't find that method :/

    Sorry about that. The HERE map component has .SetCenter and SetZoomLevel. The Google Map web API has .SetCenter but the Xamarin map component doesn't have that command. Sorry - after a while all the different map API tend to blur together in my head.

    Very thanks anyway Clint StLaurent :smiley:

  • Ashish_sharmaAshish_sharma INMember ✭✭✭

    how to get latitude and longitude in xamarin forms for all platforms i use
    var locator = CrossGeolocator.Current;
    locator.DesiredAccuracy = 50;

                var position = await locator.GetPositionAsync(timeoutMilliseconds:1000);
                decimal latitude = Convert.ToDecimal(position.Latitude);
                decimal longitude = Convert.ToDecimal(position.Longitude);
                App.latitude = latitude;
                App.longitude = longitude;
    

    but i got error in "timeoutMilliseconds:1000" it not acceptable in GetPositionAsync method.

  • MKVIMALANMKVIMALAN Member ✭✭
    edited November 13

    Add this Nuget Package manager, It's completely free

        private async void OnButtonClicked(object sender, EventArgs e)  
                {  
                    var locator = CrossGeolocator.Current;  
    
                locator.DesiredAccuracy = 50;  
    
                    var position = await locator.GetPositionAsync(timeoutMilliseconds:10000);  
    
                    LogitudeLabel.Text = position.Longitude.ToString();  
    
                    LatitudeLabel.Text = position.Latitude.ToString();  
    
                }  
    

    Now, we need to add location permission to our projects
    ACCESS_COARSE_LOCATION
    ACCESS_FINE_LOCATION

Sign In or Register to comment.