无法在视图中显示以下列表中的所有插针. 注:我下面的样本不是xamarin forms documentation个中的一部分.这是我的 idea ,也许我可以将这两个列表结合在一起,并在 map 上显示.
- Pin.id==1(主针)常量更改其位置,try 添加不同的图像或 colored颜色
- 在加载 map 时,我正在try 显示另外9个别针(静态别针),这些别针不会移动.我再一次指定了不同的图像 colored颜色 . 结果,一个列表将覆盖另一个列表.因此,我要么显示引脚1,要么显示其他9个引脚.但我不能同时把10个插针放在屏幕上.
下面是如何创建第一个静态列表和单个PIN
public class CustomMap : Map
{
public List<CustomPin> AdditionalPins { get; set; }
public List<CustomPin> CustomPins { get; set; }
public CustomMap()
{
CustomPins = new List<CustomPin>();
AdditionalPins = new List<CustomPin>();
}
在主页上:两个PIN列表
var customPin = new CustomPin
{
PinId = 1,
// Position = new Position(37.7749, -122.4194), // Set the position of the pin
Label = "Custom Pin",
Address = "Custom Address",
Rotation = -45 // Set the rotation angle in degrees
};
Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
{
customPin.Position = pos;
// pins.Position = pos;
if (myMap.Pins.Count == 0)
{
myMap.Pins.Add(customPin);
}....
2.一组静态引脚
foreach (var coordinate in coordinates)
{
var poss = new Position(coordinate.Latitude, coordinate.Longitude);
CustomPin additionalPin = new CustomPin
{
PinId = 20,
Label = "Additional Pin",
Name = coordinate.StopNumber
// Add more properties as needed
};
myMap.AdditionalPins.Add(additionalPin);
现在,我得到了两个列表,一个有一个针脚,每10秒更新一次位置,另一个(adittionalpins=9组坐标) 在这里,将这两个列表结合起来
private List<CustomPin> GetAllPins(MKPointAnnotation mKPointAnnotation)
{
List<CustomPin> matchingPins = new List<CustomPin>();
if (mKPointAnnotation != null)
{
var position = new Position(mKPointAnnotation.Coordinate.Latitude, mKPointAnnotation.Coordinate.Longitude);
// Check Pins collection
foreach (var pin in ((CustomMap)Element).Pins)
{
matchingPins.Add(pin as CustomPin);
// If a match is found in Pins, no need to check AdditionalPins, break out of the loop
}
// Check AdditionalPins collection only if a match is not found in Pins
foreach (var additionalPin in ((CustomMap)Element).AdditionalPins)
{
matchingPins.Add(additionalPin);
// If a match is found in AdditionalPins, no need to continue the loop
}
}
return matchingPins;
}
protected override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
{
if (annotation is MKUserLocation)
return null;
var result = GetAllPins(annotation as MKPointAnnotation); //HERE -- get all the items
if (result != null)
{
List<CustomPin> pins = result;
if (pins != null && pins.Any())
{
foreach (var D in pins)
{
if (annotationView == null)
{
annotationView = new MKAnnotationView(annotation, reuseIdentifier);
if (D.PinId == 1) //it gets here the first time
{
annotationView.Image = UIImage.FromFile("lilocation.png");
}
else if (D.PinId == 20) //never assign this part to the view
{
annotationView.Image = UIImage.FromFile("red.png");
}
}
}
}
return annotationView;
}
目标是显示列表中的每一项及其图像和行为.问题是,视图只在第一次得到NULL.