簡體   English   中英

在Xamarin.Forms.Maps上添加圖釘

[英]Add pin on click Xamarin.Forms.Maps

我想在使用Xamarin.Forms.Maps單擊地圖上添加新的圖釘。 搜索后,我發現我必須使用TKCustomMap插件..但是它沒有顯示在地圖上..只是空白區域,這是我的代碼

   double lit = 2.394;// double.Parse(Center.CenterLocationX);
   double longt = 43.2352;// double.Parse(Center.CenterLocationY);
   TK.CustomMap.Position position = new TK.CustomMap.Position(lit, longt);
   TK.CustomMap.MapSpan span = TK.CustomMap.MapSpan.FromCenterAndRadius(position, TK.CustomMap.Distance.FromMiles(0.5));

   TK.CustomMap.TKCustomMap map = new TK.CustomMap.TKCustomMap(span);
   map.IsShowingUser = true;
   map.MapType = TK.CustomMap.MapType.Street;
   TK.CustomMap.TKCustomMapPin pin = new TK.CustomMap.TKCustomMapPin()
   {
        //Address = "Test",
        //Label = "Test",
        Position = position,
        IsDraggable = true
        //Type = PinType.SearchResult
    };
    map.MapClicked += (x, y) =>
    {
        SharedTools.MakeToast("Clicked");
    };
    //map.Pins.Add(pin);
    map.Pins = new List<TK.CustomMap.TKCustomMapPin>() { pin };

    map.MoveToMapRegion(span);
    layout.Content = map;

我想解決這個問題,或任何其他想法在點擊時添加圖釘

我在演示中使用了您的代碼,結果如下圖所示(如果看不到google ,則應檢查API_KEY ,如果正確)。

<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="API_KEY" />

在此輸入圖像描述

然后,我改變了lit 37, longt至-122和click.I添加PIN可以看到地圖,得到以下結果。 如果值合法,請檢查您的litlongt

在此輸入圖像描述

有我的代碼。

public partial class MainPage : ContentPage
{
    TK.CustomMap.TKCustomMap map;
    TK.CustomMap.Position position;
    public MainPage()
    {
        InitializeComponent();
        //37,-122
        double lit = 37;// double.Parse(Center.CenterLocationX);
        double longt = -122;// double.Parse(Center.CenterLocationY);
        position = new TK.CustomMap.Position(lit, longt);
        TK.CustomMap.MapSpan span = TK.CustomMap.MapSpan.FromCenterAndRadius(position, TK.CustomMap.Distance.FromMiles(0.5));
        map = new TK.CustomMap.TKCustomMap(span);
        map.IsShowingUser = true;
        map.MapType = TK.CustomMap.MapType.Street;
        map.MapClicked += OnMapClicked;
        Content = map;
    }
    private void OnMapClicked(object sender, TKGenericEventArgs<Position> e)
    {
        TK.CustomMap.TKCustomMapPin pin = new TK.CustomMap.TKCustomMapPin()
        {
            //Address = "Test",
            //Label = "Test",
            Position = position
        ,
            IsDraggable = true
            //Type = PinType.SearchResult
        };

        map.Pins = new List<TK.CustomMap.TKCustomMapPin>() { pin };

    }
}   

這是我的演示,希望能為您提供幫助。

https://github.com/851265601/TKGoogleMapsDemo

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM