[英]Xamarin Google Map
我在Xamarin中进行循环,将引脚添加到谷歌地图中。 想知道它为什么不立即呈现给地图?
我想一次显示要添加到Map中的引脚,所以我将Sleep添加1秒以延迟绘图。
我认为原因是因为它在绘图上运行代码,因此使用下面代码的方法将无效。
for(int i = 0;i < templist.Count ; i++)
{
if (i != 0)
{
if (MyMap.Pins.Count > 9) {
MyMap.Pins.RemoveAt(0);
}
}
var Item = templist.ElementAt(i);
string resultDatetimer = Item.DateTimer;
string resultLocation = Item.Location;
string resultLatitude = Item.Latitude;
string resultLongitude = Item.Longitude;
var position = new Position(Convert.ToDouble(resultLatitude), Convert.ToDouble(resultLongitude));
var pin1 = new Pin
{
Type = PinType.Place,
Position = position,
Label = "Date:" + resultDatetimer + ", Location:" + resultLocation
};
MyMap.Pins.Add(pin1);
MyMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(Convert.ToDouble(resultLatitude), Convert.ToDouble(resultLongitude))
, Distance.FromMeters(500)));
System.Threading.Thread.Sleep(1000);
}
1)不要使用System.Threading.Thread.Sleep
因为这是一个阻塞调用,如果你在UI线程上,你的UI /键盘/触摸输入等等。所有这些都只是冻结了那个时间段,使用等待的Task.Delay
相反,所以该线程的消息泵可以继续。
await Task.Delay(1000);
2)如果您的代码在后台线程上运行,请确保在ui线程上执行pin add和movetoregion。
Device.BeginInvokeOnMainThread(() =>
{
MyMap.Pins.Add(pin1);
MyMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(Convert.ToDouble(resultLatitude), Convert.ToDouble(resultLongitude))
, Distance.FromMeters(500)));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.