Khalid Harkati
Khalid Harkati

Reputation: 71

How to display a pin on maps

I could display the map on my App interface , and now I'm trying to display a Pin of my current position . I displayed in but with a button Click that shows me another Content Page . but I couldn't display it on the same page even after clicking that button . If there's a way to show it , on the same page and without clicking any button , I'll be so thankful .

Here's the code of my Clicked_Button Event , This Code works and when I click it displays a new content page with my current position . but what I want it to to show it in my Map Module in the Main Page, this is the XAML Code for my map (putting it in a grid):

<maps1:Map  Grid.Column="1" 
Grid.Row="2" 
Grid.RowSpan="2" 
Grid.ColumnSpan="3"
x:Name="myMap">

I tried to remove " Content = myMap; " from my back-end code to keep it on the main page , but the pin doesn't show up.

protected async void OnButtonClicked_speed(object sender, EventArgs args)
{
    var locator = CrossGeolocator.Current;
    locator.DesiredAccuracy = 50;
    var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(10));

    myMap = new Xamarin.Forms.Maps.Map(MapSpan.FromCenterAndRadius(
    new Xamarin.Forms.Maps.Position(position.Latitude,position.Longitude),
    Distance.FromMiles(0.5)))
    {
        IsShowingUser = true,
        VerticalOptions = LayoutOptions.FillAndExpand
    };

    var position1 = new Xamarin.Forms.Maps.Position(position.Latitude, position.Longitude);

    var pin1 = new Pin
    {
        Type = PinType.Place,
        Position = position1,
        Label = "Current Position",
        Address = ""
    };

    myMap.Pins.Add(pin1);
    Content = myMap;
}

Upvotes: 0

Views: 596

Answers (1)

Leo Zhu
Leo Zhu

Reputation: 15021

Content = myMap

this is equivalent to repopulating your current page with a map

so you just move the code out of your OnButtonClicked_speed method.

like this:

in MainPage.xaml.cs

public partial class MainPage : ContentPage
{
    private Map myMap;
    public MainPage(Position position)
    {
        InitializeComponent();           
        myMap.MoveToRegion(MapSpan.FromCenterAndRadius(
            new Xamarin.Forms.Maps.Position(position.Latitude, position.Longitude),
            Distance.FromMiles(0.5)));
        myMap.IsShowingUser = true;
        myMap.VerticalOptions = LayoutOptions.FillAndExpand;
        var position1 = new Xamarin.Forms.Maps.Position(position.Latitude, position.Longitude);

        var pin1 = new Pin
        {
            Type = PinType.Place,
            Position = position1,
            Label = "Current Position",
            Address = ""
        };

        myMap.Pins.Add(pin1);
    }

  public static async Task<MainPage> CreateMainPageAsync()
    {

        var locator = CrossGeolocator.Current;
        locator.DesiredAccuracy = 50;
        var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(10));
        MainPage page = new MainPage(position );
        return page;
    }
}

in App.xaml.cs

public partial class App : Application
{
    public App()
    {
        InitializeComponent();
    }

    protected override async void OnStart()
    {
        namespace.MainPage main = await namespace.MainPage.CreateMainPageAsync();
        MainPage = main;
    }
}

Upvotes: 0

Related Questions