【问题标题】:Add pin on click Xamarin.Forms.Maps在单击 Xamarin.Forms.Maps 时添加图钉
【发布时间】:2019-03-10 15:01:00
【问题描述】:

我想在使用 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;

我想解决这个问题,或者想在点击时添加图钉

【问题讨论】:

    标签: c# xamarin.forms maps


    【解决方案1】:

    我在我的演示中使用了你的代码,我得到了如下截图的结果(如果你看不到google的波段,你应该检查API_KEY,如果它是正确的。)

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

    然后我将lit 更改为 37,longt 更改为 -122 并在点击时添加 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

    【讨论】:

    • 谢谢..因为很紧急,我用过Xamarin.Forms.GoogleMaps,这个库解决了这个问题..我会试试你使用的解决方案..再次感谢你:)跨度>
    • 刚刚使用 Xamarin.Forms.GoogleMaps 而不是 Xamarin.Forms.Maps
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多