(Error: Custom pin not found) when trying to fill map with markers from hardcoded locations
(Error: Custom pin not found) when trying to fill map with markers from hardcoded locations
我想用硬编码坐标填充地图,以便使用 Xamarin.Forms.Maps
显示带有自定义地图的图钉标记
我的 HardcodeLocations class 是:
public class HardcodedLocations
{
public static List<Position> Positions = new List<Position>
{
new Position(41.19197, 25.33719 ),
new Position(41.26352, 25.1471 ),
new Position(41.26365, 25.24215 ),
new Position(41.26369, 25.33719 ),
new Position(41.26365, 25.43224 ),
};
}
在构造函数中 class 我试图加载这样的标记:
public AboutPage()
{
InitializeComponent();
for (int i = 0; i < HardcodedLocations.Positions.Count; i++)
{
CustomPin pin = new CustomPin
{
Type = PinType.Place,
Position = HardcodedLocations.Positions[i],
Label = "Xamarin San Francisco Office",
Address = "394 Pacific Ave, San Francisco CA",
Name = "Xamarin",
Url = "http://xamarin.com/about/"
};
customMap.CustomPins = new List<CustomPin> { pin };
customMap.Pins.Add(pin);
customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(42.8742, 25.3187), Distance.FromMiles(1.0)));
}
}
但我在 iOS 项目的 CustomMapRender class 中收到错误:
var customPin = GetCustomPin(annotation as MKPointAnnotation);
if (customPin == null)
{
throw new Exception("Custom pin not found");
}
这是同一个class:
中GetViewForAnnotation方法中的所有代码
protected override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
{
MKAnnotationView annotationView = null;
if (annotation is MKUserLocation)
return null;
var customPin = GetCustomPin(annotation as MKPointAnnotation);
if (customPin == null)
{
throw new Exception("Custom pin not found");
}
annotationView = mapView.DequeueReusableAnnotation(customPin.Name);
if (annotationView == null)
{
annotationView = new CustomMKAnnotationView(annotation, customPin.Name);
annotationView.Image = UIImage.FromFile("pin.png");
annotationView.CalloutOffset = new CGPoint(0, 0);
annotationView.LeftCalloutAccessoryView = new UIImageView(UIImage.FromFile("monkey.png"));
annotationView.RightCalloutAccessoryView = UIButton.FromType(UIButtonType.DetailDisclosure);
((CustomMKAnnotationView)annotationView).Name = customPin.Name;
((CustomMKAnnotationView)annotationView).Url = customPin.Url;
}
annotationView.CanShowCallout = true;
return annotationView;
}
我不知道如何解决这个错误,因为当我使用基本 xamarin.forms.maps(没有自定义)时,我只是像这样填充地图:
for (int i = 0; i < HardcodedLocations.Positions.Count; i++)
{
Pin pin = new Pin
{
//Label = $"{i + 1}",
Label = $"{i + 1}",
Address = "Кликни тук за да видиш прогнозата.",
Type = PinType.Place,
Position = HardcodedLocations.Positions[i]
};
map.Pins.Add(pin);
}
这是GetCustomPin
的方法:
CustomPin GetCustomPin(MKPointAnnotation annotation)
{
var position = new Position(annotation.Coordinate.Latitude, annotation.Coordinate.Longitude);
foreach (var pin in customPins)
{
if (pin.Position == position)
{
return pin;
}
}
return null;
}
这是CustomPin
代码:
public class CustomPin : Pin
{
public string Name { get; set; }
public string Url { get; set; }
}
要更改引脚,必须重构整个代码,在基本项目中创建 CustomMap
和 CustomPin
classes 并创建 CustomMapRenderer
和 CustomMKAnnotationView
class 在 iOS 和 Android 项目中。
参考:https://github.com/xamarin/xamarin-forms-samples/tree/main/CustomRenderers/Map
有没有办法清除这个错误?
===================================
更新
在这种方法中,我尝试比较两个 Position 属性:
CustomPin GetCustomPin(MKPointAnnotation annotation)
{
var position = new Position(annotation.Coordinate.Latitude, annotation.Coordinate.Longitude);
foreach (var pin in customPins)
{
if (pin.Position.Latitude == position.Latitude && pin.Position.Longitude == position.Longitude)
{
return pin;
}
}
return null;
}
但我仍然收到同样的错误 ;(
正如 Jason 在评论中指出的那样,您需要更改向地图添加项目的方式。
List<CustomPin> pins = new List<CustomPin>();
for (int i = 0; i < HardcodedLocations.Positions.Count; i++)
{
CustomPin pin = new CustomPin
{
Type = PinType.Place,
Position = HardcodedLocations.Positions[i],
Label = "Xamarin San Francisco Office",
Address = "394 Pacific Ave, San Francisco CA",
Name = "Xamarin",
Url = "http://xamarin.com/about/"
};
customMap.Pins.Add(pin);
}
customMap.CustomPins = pins;
customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(42.8742, 25.3187), Distance.FromMiles(1.0)));
您在循环的每次迭代中都用一个包含单个项目的新列表覆盖了 CustomPins。
我想用硬编码坐标填充地图,以便使用 Xamarin.Forms.Maps
显示带有自定义地图的图钉标记我的 HardcodeLocations class 是:
public class HardcodedLocations
{
public static List<Position> Positions = new List<Position>
{
new Position(41.19197, 25.33719 ),
new Position(41.26352, 25.1471 ),
new Position(41.26365, 25.24215 ),
new Position(41.26369, 25.33719 ),
new Position(41.26365, 25.43224 ),
};
}
在构造函数中 class 我试图加载这样的标记:
public AboutPage()
{
InitializeComponent();
for (int i = 0; i < HardcodedLocations.Positions.Count; i++)
{
CustomPin pin = new CustomPin
{
Type = PinType.Place,
Position = HardcodedLocations.Positions[i],
Label = "Xamarin San Francisco Office",
Address = "394 Pacific Ave, San Francisco CA",
Name = "Xamarin",
Url = "http://xamarin.com/about/"
};
customMap.CustomPins = new List<CustomPin> { pin };
customMap.Pins.Add(pin);
customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(42.8742, 25.3187), Distance.FromMiles(1.0)));
}
}
但我在 iOS 项目的 CustomMapRender class 中收到错误:
var customPin = GetCustomPin(annotation as MKPointAnnotation);
if (customPin == null)
{
throw new Exception("Custom pin not found");
}
这是同一个class:
中GetViewForAnnotation方法中的所有代码protected override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
{
MKAnnotationView annotationView = null;
if (annotation is MKUserLocation)
return null;
var customPin = GetCustomPin(annotation as MKPointAnnotation);
if (customPin == null)
{
throw new Exception("Custom pin not found");
}
annotationView = mapView.DequeueReusableAnnotation(customPin.Name);
if (annotationView == null)
{
annotationView = new CustomMKAnnotationView(annotation, customPin.Name);
annotationView.Image = UIImage.FromFile("pin.png");
annotationView.CalloutOffset = new CGPoint(0, 0);
annotationView.LeftCalloutAccessoryView = new UIImageView(UIImage.FromFile("monkey.png"));
annotationView.RightCalloutAccessoryView = UIButton.FromType(UIButtonType.DetailDisclosure);
((CustomMKAnnotationView)annotationView).Name = customPin.Name;
((CustomMKAnnotationView)annotationView).Url = customPin.Url;
}
annotationView.CanShowCallout = true;
return annotationView;
}
我不知道如何解决这个错误,因为当我使用基本 xamarin.forms.maps(没有自定义)时,我只是像这样填充地图:
for (int i = 0; i < HardcodedLocations.Positions.Count; i++)
{
Pin pin = new Pin
{
//Label = $"{i + 1}",
Label = $"{i + 1}",
Address = "Кликни тук за да видиш прогнозата.",
Type = PinType.Place,
Position = HardcodedLocations.Positions[i]
};
map.Pins.Add(pin);
}
这是GetCustomPin
的方法:
CustomPin GetCustomPin(MKPointAnnotation annotation)
{
var position = new Position(annotation.Coordinate.Latitude, annotation.Coordinate.Longitude);
foreach (var pin in customPins)
{
if (pin.Position == position)
{
return pin;
}
}
return null;
}
这是CustomPin
代码:
public class CustomPin : Pin
{
public string Name { get; set; }
public string Url { get; set; }
}
要更改引脚,必须重构整个代码,在基本项目中创建 CustomMap
和 CustomPin
classes 并创建 CustomMapRenderer
和 CustomMKAnnotationView
class 在 iOS 和 Android 项目中。
参考:https://github.com/xamarin/xamarin-forms-samples/tree/main/CustomRenderers/Map
有没有办法清除这个错误?
=================================== 更新
在这种方法中,我尝试比较两个 Position 属性:
CustomPin GetCustomPin(MKPointAnnotation annotation)
{
var position = new Position(annotation.Coordinate.Latitude, annotation.Coordinate.Longitude);
foreach (var pin in customPins)
{
if (pin.Position.Latitude == position.Latitude && pin.Position.Longitude == position.Longitude)
{
return pin;
}
}
return null;
}
但我仍然收到同样的错误 ;(
正如 Jason 在评论中指出的那样,您需要更改向地图添加项目的方式。
List<CustomPin> pins = new List<CustomPin>();
for (int i = 0; i < HardcodedLocations.Positions.Count; i++)
{
CustomPin pin = new CustomPin
{
Type = PinType.Place,
Position = HardcodedLocations.Positions[i],
Label = "Xamarin San Francisco Office",
Address = "394 Pacific Ave, San Francisco CA",
Name = "Xamarin",
Url = "http://xamarin.com/about/"
};
customMap.Pins.Add(pin);
}
customMap.CustomPins = pins;
customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(42.8742, 25.3187), Distance.FromMiles(1.0)));
您在循环的每次迭代中都用一个包含单个项目的新列表覆盖了 CustomPins。