在 iOS 12.0 上加载 MapKit 地图时,所有标注都会显示而不是隐藏
On load of MapKit Map on iOS 12.0, all callouts are displayed instead of hidden
上下文
我有一个使用原生地图的 Xamarin Forms 应用程序(Google 地图 Android,Apple Mapkit iOS)。在各种 Android 和 Apple 手机和平板电脑上进行测试后,除了 iPhone 6 运行 iOS 12.0 之外,地图显示正确。
这很奇怪,尤其是因为该地图正在处理比 iPhone 6 更旧和更新的 iPhones。 实际上,问题似乎与最新版本有关iOS;将应用程序更新到 12.0 会导致地图显示不正确。
地图有很多注释,所有注释都有标注。
预期行为:
打开地图后会显示地图注释。用户应该能够点击注释以打开关联的标注,并且在点击标注时,他们会导航到另一个页面。
实际行为:
在 an iPhone 6 iOS 12.0 上,地图注释显示但所有标注也显示,应用程序挂起。当用户点击标注时,应用程序崩溃。
附加信息:
我们使用 FreshMvvm 作为我们的 Mvvm 框架。
我们正在使用自定义地图渲染器。地图渲染器的主要结构是这样的:
public class IosDefaultMapRenderer : MapRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
var nativeMap = Control as MKMapView;
if (e.OldElement != null && nativeMap != null)
{
nativeMap.RemoveAnnotations(nativeMap.Annotations);
nativeMap.GetViewForAnnotation = null;
nativeMap.CalloutAccessoryControlTapped -= OnCalloutAccessoryControlTapped;
nativeMap.DidSelectAnnotationView -= OnDidSelectAnnotationView;
nativeMap.DidDeselectAnnotationView -= OnDidDeselectAnnotationView;
if (nativeMap.Overlays != null)
{
nativeMap.RemoveOverlays(nativeMap.Overlays);
}
}
if (e.NewElement == null)
{
return;
}
_formsMap = (DefaultMap)e.NewElement; // Map from Xamarin Forms
customPins = _formsMap.CustomPins;
if (nativeMap == null) return;
nativeMap.GetViewForAnnotation = GetViewForAnnotation;
nativeMap.CalloutAccessoryControlTapped += OnCalloutAccessoryControlTapped;
nativeMap.DidSelectAnnotationView += OnDidSelectAnnotationView;
nativeMap.DidDeselectAnnotationView += OnDidDeselectAnnotationView;
AddCustomPins(nativeMap);
}
protected void AddCustomPins (MKMapView nativeMap)
{
foreach (CustomPin customPin in customPins)
{
// Generate Annotation
//
// Add annotation
nativeMap.AddAnnotation(annotation);
}
}
protected void MapRendererOnElementChanged(ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
}
public virtual MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
{
var description = new UILabel();
var detailButton = UIButton.FromType(UIButtonType.DetailDisclosure);
var annotationView = mapView.DequeueReusableAnnotation(AnnotationIdentifier);
if (annotationView == null)
{
annotationView = new MKAnnotationView(annotation, AnnotationIdentifier);
}
else
{
annotationView.Annotation = annotation;
}
// Set annotationView and description properties from customPin data
//
return annotationView;
}
public void OnDidSelectAnnotationView(object sender, MKAnnotationViewEventArgs e)
{
// ...
}
protected virtual void OnCalloutAccessoryControlTapped(object sender, MKMapViewAccessoryTappedEventArgs e)
{
// navigate to another page
}
public void OnDidDeselectAnnotationView(object sender, MKAnnotationViewEventArgs e)
{
// ...
}
}
我将逻辑从 OnDidSelectAnnotationView
移到了 GetViewForAnnotation
。
这修复了点击标注时应用崩溃的问题。
我还删除了将注释设置为选中的一行。 GetViewForAnnotation
似乎在加载地图后对所有注释调用一次,因此所有注释都被选中。这很奇怪,因为这在以前的 iOS.
版本中没有发生
很简单。
要隐藏 iOS12 的标注,我们只需通过更改地图区域来稍微移动地图。
你可以试试这个。从 'mapview didAdd views'
调用以下方法
var isiOS12issueDone: Bool = false (Global variable)
func handleiOS12issue()
{
if (isiOS12OrLater() && isiOS12issueDone == false)
{
var region = mvMapView!.region
region.center.latitude += 0.000001
mvMapView!.region = region
isiOS12issueDone = true
}
}
上下文
我有一个使用原生地图的 Xamarin Forms 应用程序(Google 地图 Android,Apple Mapkit iOS)。在各种 Android 和 Apple 手机和平板电脑上进行测试后,除了 iPhone 6 运行 iOS 12.0 之外,地图显示正确。
这很奇怪,尤其是因为该地图正在处理比 iPhone 6 更旧和更新的 iPhones。 实际上,问题似乎与最新版本有关iOS;将应用程序更新到 12.0 会导致地图显示不正确。
地图有很多注释,所有注释都有标注。
预期行为:
打开地图后会显示地图注释。用户应该能够点击注释以打开关联的标注,并且在点击标注时,他们会导航到另一个页面。
实际行为:
在 an iPhone 6 iOS 12.0 上,地图注释显示但所有标注也显示,应用程序挂起。当用户点击标注时,应用程序崩溃。
附加信息: 我们使用 FreshMvvm 作为我们的 Mvvm 框架。 我们正在使用自定义地图渲染器。地图渲染器的主要结构是这样的:
public class IosDefaultMapRenderer : MapRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
var nativeMap = Control as MKMapView;
if (e.OldElement != null && nativeMap != null)
{
nativeMap.RemoveAnnotations(nativeMap.Annotations);
nativeMap.GetViewForAnnotation = null;
nativeMap.CalloutAccessoryControlTapped -= OnCalloutAccessoryControlTapped;
nativeMap.DidSelectAnnotationView -= OnDidSelectAnnotationView;
nativeMap.DidDeselectAnnotationView -= OnDidDeselectAnnotationView;
if (nativeMap.Overlays != null)
{
nativeMap.RemoveOverlays(nativeMap.Overlays);
}
}
if (e.NewElement == null)
{
return;
}
_formsMap = (DefaultMap)e.NewElement; // Map from Xamarin Forms
customPins = _formsMap.CustomPins;
if (nativeMap == null) return;
nativeMap.GetViewForAnnotation = GetViewForAnnotation;
nativeMap.CalloutAccessoryControlTapped += OnCalloutAccessoryControlTapped;
nativeMap.DidSelectAnnotationView += OnDidSelectAnnotationView;
nativeMap.DidDeselectAnnotationView += OnDidDeselectAnnotationView;
AddCustomPins(nativeMap);
}
protected void AddCustomPins (MKMapView nativeMap)
{
foreach (CustomPin customPin in customPins)
{
// Generate Annotation
//
// Add annotation
nativeMap.AddAnnotation(annotation);
}
}
protected void MapRendererOnElementChanged(ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
}
public virtual MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
{
var description = new UILabel();
var detailButton = UIButton.FromType(UIButtonType.DetailDisclosure);
var annotationView = mapView.DequeueReusableAnnotation(AnnotationIdentifier);
if (annotationView == null)
{
annotationView = new MKAnnotationView(annotation, AnnotationIdentifier);
}
else
{
annotationView.Annotation = annotation;
}
// Set annotationView and description properties from customPin data
//
return annotationView;
}
public void OnDidSelectAnnotationView(object sender, MKAnnotationViewEventArgs e)
{
// ...
}
protected virtual void OnCalloutAccessoryControlTapped(object sender, MKMapViewAccessoryTappedEventArgs e)
{
// navigate to another page
}
public void OnDidDeselectAnnotationView(object sender, MKAnnotationViewEventArgs e)
{
// ...
}
}
我将逻辑从 OnDidSelectAnnotationView
移到了 GetViewForAnnotation
。
这修复了点击标注时应用崩溃的问题。
我还删除了将注释设置为选中的一行。 GetViewForAnnotation
似乎在加载地图后对所有注释调用一次,因此所有注释都被选中。这很奇怪,因为这在以前的 iOS.
很简单。 要隐藏 iOS12 的标注,我们只需通过更改地图区域来稍微移动地图。
你可以试试这个。从 'mapview didAdd views'
调用以下方法var isiOS12issueDone: Bool = false (Global variable)
func handleiOS12issue()
{
if (isiOS12OrLater() && isiOS12issueDone == false)
{
var region = mvMapView!.region
region.center.latitude += 0.000001
mvMapView!.region = region
isiOS12issueDone = true
}
}