简体   繁体   English

如何在当前位置附近加载引脚并在缩小 Xamarin.Forms.Maps 时加载更多

[英]How to load pins just around your current location and load more as you zoom out in Xamarin.Forms.Maps

I'm trying to show pins on the map but only the ones that can fit the screen around your current location depending on the zoom level I've set on when the map appears, because I have 10's of pins and when I open the map it loads every pin and takes a long time to load.我正在尝试在 map 上显示引脚,但只有在 map 出现时我设置的缩放级别可以适合您当前位置的屏幕,因为我有 10 个引脚并且当我打开 map 时它加载每个引脚并且需要很长时间才能加载。

Any idea on how to do it?关于如何做的任何想法?

Method to load pins:加载引脚的方法:

    async Task ExecuteLoadPinsCommand()
    {
        IsBusy = true;

        try
        {
            Map.Pins.Clear();
            Map.MapElements.Clear();
            Map.CustomPins.Clear();
            var contents = await placeRepository.GetAllPlacesWithoutRelatedDataAsync();

            if (contents == null || contents.Count < 1)
            {
                await App.Current.MainPage.DisplayAlert("No places found", "No places have been found for that category, please try again later", "Ok");
                await ExecuteLoadPinsCommand();
            }

            if (contents != null)
            {
                places.Clear();
                var customPins = this.Map.CustomPins;
                places = contents;
                foreach (var item in places)
                {
                    CustomPin devicePin = new CustomPin
                    {
                        Type = PinType.Place,
                        PlaceId = item.PlaceId.ToString(),
                        Position = new Position(item.Latitude, item.Longitude),
                        Label = $"{item.Name}",
                        Address = $"{item.Name}"
                    };

                    Map.CustomPins.Add(devicePin);
                    Map.Pins.Add(devicePin);
                }
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
        }
        finally
        {
            IsBusy = false;
        }
    }

CustomMapRenderer:自定义地图渲染器:

    protected override MarkerOptions CreateMarker(Pin pin)
    {
        CustomPin pin = (CustomPin)pin;
        var thePlace = Task.Run(async () => await placeRepository.GetPlaceByIdWithMoodAndEventsAsync(Guid.Parse(pin.PlaceId)));
        var place = thePlace.ConfigureAwait(true)
                            .GetAwaiter()
                            .GetResult();

        var marker = new MarkerOptions();

        marker.SetPosition(new LatLng(place.Position.Latitude, place.Position.Longitude));
        if (place.Category == "" && place.SomethingCount == 0)
        {
            marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.icon));
        }
        //else if ...

        return marker;

    }

Try this approach.试试这个方法。 Create the map WITH its pins, BEFORE displaying it.在显示之前使用其引脚创建 map。

using Xamarin.Forms;
using Xamarin.Forms.Maps;

namespace XFSOAnswers
{
    // Based on https://github.com/xamarin/xamarin-forms-samples/blob/main/WorkingWithMaps/WorkingWithMaps/WorkingWithMaps/PinPageCode.cs
    public class PinPageCode : ContentPage
    {
        public PinPageCode()
        {
            Title = "Pins demo";

            Position position = new Position(36.9628066, -122.0194722);
            MapSpan mapSpan = new MapSpan(position, 0.01, 0.01);

            Map map = new Map(mapSpan);

            Pin pin = new Pin
            {
                Label = "Santa Cruz",
                Address = "The city with a boardwalk",
                Type = PinType.Place,
                Position = position
            };
            map.Pins.Add(pin);
            // ... more pins.

            Content = new StackLayout
            {
                Margin = new Thickness(10),
                Children =
                {
                    map
                }
            };
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM