在点击Xamarin.Forms.Maps

时间:2019-03-10 15:01:00

标签: c# 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;

我想解决这个问题,或者其他任何想法来在点击时添加图钉

1 个答案:

答案 0 :(得分:0)

我在演示中使用了您的代码,得到的结果如以下屏幕截图所示(如果看不到google的区域,则应检查API_KEY,如果正确)。 / p>

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

enter image description here

然后我将lit更改为37,将longt更改为-122并在点击时添加图钉。我可以看到地图并得到以下结果。如果值合法,请检查您的litlongt

enter image description here

有我的代码。

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