首页 > 解决方案 > 如何使用 xamarin 在 Google 地图上每 x 秒刷新/更新 pin 位置

问题描述

我正在开发基于 xamarin 的移动应用程序。我需要从服务器检索有关用户位置的数据,并每 x 秒更新一次地图上的用户位置。我在服务器上存储用户纬度和经度,但是当我从服务器检索坐标时是最新的大头针不会在地图上渲染。只有第一次渲染大头针时,即使纬度和经度的位置发生变化,大头针也会保持原样,不会在新位置渲染。有人可以提供相同的解决方案吗?

请在下面找到代码 这段代码用于每 10 秒获取一次位置

Device.StartTimer(TimeSpan.FromSeconds(10), () =>
                     {
                         bool CallAgain = true;
                         lblStatus.IsVisible = false;
                         mapVM.getGpsPosition().ContinueWith((Task<ApiResponseModel.DriverLocationDetailsResponseModel> t) =>
                         {
                             if (t.Result.status == DriverStatus.Running.ToString() || t.Result.status == DriverStatus.Started.ToString())
                             {
                                 RedrawMap(t.Result.lat, t.Result.lon);
                             }
                             else
                             {
                                 ParentMaps.IsVisible = false;
                                 lblStatus.IsVisible = true;
                                 lblStatus.Text = "Driver has stopped the journey";
                                 CallAgain = false;
                             }

                         });
                         return CallAgain;
                     });

这段代码用于在地图上绘制图钉

private void RedrawMap(double lat, double lon)
{
    var pin = new CustomPin
    {
        Type = PinType.Place,
        Position = new Xamarin.Forms.Maps.Position(lat, lon),
        Label = "Current Location",
        Id = "Xamarin"
    };

    DriverMap.CustomPins = new List<CustomPin> { pin };
    if (DriverMap.Pins.Count > 0)
    {
        DriverMap.Pins.Clear();
    }
    DriverMap.Pins.Add(pin);
    DriverMap.MoveToRegion(
            MapSpan.FromCenterAndRadius(new 
            Xamarin.Forms.Maps.Position(lat, lon), 
            Distance.FromMiles(0.1))
            );
  }

标签: xamarin

解决方案


你可以做一个计时器,每 x 秒清理一次引脚并重新加载新引脚

参考:https://forums.xamarin.com/discussion/118999/refresh-pins-positions-in-google-maps


推荐阅读