如何向xamarin forms maps添加位置按钮
我实现了XF映射,并从XF.Essentials中添加了权限 在ANdroid上一切都很好,但在iOS上,在我点击“批准权限”后,我看不到位置按钮 我还需要添加什么才能看到位置按钮(用户地理位置)如何向xamarin forms maps添加位置按钮,xamarin,xamarin.forms,xamarin.ios,Xamarin,Xamarin.forms,Xamarin.ios,我实现了XF映射,并从XF.Essentials中添加了权限 在ANdroid上一切都很好,但在iOS上,在我点击“批准权限”后,我看不到位置按钮 我还需要添加什么才能看到位置按钮(用户地理位置) 。。。 私有异步void GetPermissions() { var status=等待权限。CheckStatusAsync(); if(status!=PermissionStatus.grated) { status=wait Permissions.RequestAsync(); } if
。。。
私有异步void GetPermissions()
{
var status=等待权限。CheckStatusAsync();
if(status!=PermissionStatus.grated)
{
status=wait Permissions.RequestAsync();
}
if(status!=PermissionStatus.grated)
{
等待Shell.Current.DisplayAlert(“权限被拒绝”,“我们需要访问您的位置。但它未被授予”,“确定”);
}
}
...
我的iOS渲染器
[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]
namespace OperaMobile.iOS.Renderers
{
public class CustomMapRenderer : MapRenderer
{
UIStackView customPinView;
ObservableCollection<CustomPin> customPins;
protected override void OnElementChanged(ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
var nativeMap = Control as MKMapView;
nativeMap.GetViewForAnnotation = null;
nativeMap.CalloutAccessoryControlTapped -= OnCalloutAccessoryControlTapped;
nativeMap.DidSelectAnnotationView -= OnDidSelectAnnotationView;
nativeMap.DidDeselectAnnotationView -= OnDidDeselectAnnotationView;
}
if (e.NewElement != null)
{
var formsMap = (CustomMap)e.NewElement;
var nativeMap = Control as MKMapView;
customPins = formsMap.CustomPins;
nativeMap.GetViewForAnnotation = GetViewForAnnotation;
nativeMap.CalloutAccessoryControlTapped += OnCalloutAccessoryControlTapped;
nativeMap.DidSelectAnnotationView += OnDidSelectAnnotationView;
nativeMap.DidDeselectAnnotationView += OnDidDeselectAnnotationView;
}
}
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.Label);
if (annotationView == null)
{
annotationView = new CustomMKAnnotationView(annotation, customPin.Label);
annotationView.Image = UIImage.FromFile("pin.png");
annotationView.CalloutOffset = new CGPoint(0, 0);
UIImageView uIImageView = new UIImageView(UIImage.FromFile("monkey.png"));
uIImageView.Frame = new CGRect(0, 0, 75, 100);
annotationView.LeftCalloutAccessoryView = uIImageView;
//annotationView.LeftCalloutAccessoryView = new UIImageView(UIImage.FromFile("monkey.png"));
//annotationView.RightCalloutAccessoryView = UIButton.FromType(UIButtonType.DetailDisclosure);
((CustomMKAnnotationView)annotationView).Name = customPin.Label;
//((CustomMKAnnotationView)annotationView).Url = customPin.Url;
customPinView = new UIStackView();
foreach (var item in customPin.InfoBox.DetailsObjectInfos)
{
var label = new UILabel();
label.Text = item.BoldLabelTitle + item.LabelValue;
label.BackgroundColor = UIColor.White;
label.Font.WithSize(36);
customPinView.AddArrangedSubview(label);
}
customPinView.Frame = new CGRect(0, 0, 300, 84);
customPinView.Axis = UILayoutConstraintAxis.Vertical;
customPinView.Distribution = UIStackViewDistribution.EqualSpacing;
customPinView.Spacing = 1;
customPinView.Alignment = UIStackViewAlignment.Fill;
annotationView.DetailCalloutAccessoryView = customPinView;
UITapGestureRecognizer tapGestureRecognizer = new
UITapGestureRecognizer((gesture) =>
{
Shell.Current.GoToAsync(Routes.ObjectParametersPage);
});
annotationView.DetailCalloutAccessoryView.AddGestureRecognizer(tapGestureRecognizer);
}
annotationView.CanShowCallout = true;
return annotationView;
}
protected virtual void OnCalloutAccessoryControlTapped(object sender, MKMapViewAccessoryTappedEventArgs e)
{
Shell.Current.GoToAsync(Routes.ObjectParametersPage);
//(App.Current as App).NavigationPage.Navigation.PushAsync(new ContentPage());
}
void OnDidSelectAnnotationView(object sender, MKAnnotationViewEventArgs e)
{
CustomMKAnnotationView customView = e.View as CustomMKAnnotationView;
}
void OnDidDeselectAnnotationView(object sender, MKAnnotationViewEventArgs e)
{
if (!e.View.Selected)
{
customPinView.RemoveFromSuperview();
customPinView.Dispose();
customPinView = null;
}
}
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;
}
}}
[程序集:ExportRenderer(typeof(CustomMap)、typeof(CustomMapRenderer))]
命名空间OperaMobile.iOS.Renderers
{
公共类CustomMapRenderer:MapRenderer
{
UIStackView自定义PinView;
可观察收集的密码;
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(e.OldElement!=null)
{
var nativeMap=作为MKMapView的控件;
nativeMap.GetViewForAnnotation=null;
nativeMap.CalloutAccessoryControlTapped-=OnCalloutAccessoryControlTapped;
nativeMap.DidSelectAnnotationView-=OnDidSelectAnnotationView;
nativeMap.DidDesceneAnnotationView-=OnDidDesceneAnnotationView;
}
if(例如NewElement!=null)
{
var formsMap=(CustomMap)e.NewElement;
var nativeMap=作为MKMapView的控件;
customPins=formsMap.customPins;
nativeMap.GetViewForAnnotation=GetViewForAnnotation;
nativeMap.CalloutAccessoryControlTapped+=OnCalloutAccessoryControlTapped;
nativeMap.DidSelectAnnotationView+=OnDidSelectAnnotationView;
nativeMap.DidDesceneAnnotationView+=OnDidDesceneAnnotationView;
}
}
受保护的覆盖MKAnnotationView GetViewForAnnotation(MKMapView mapView、IMKAnnotation注释)
{
MKAnnotationView annotationView=null;
if(注释为MKUserLocation)
返回null;
var customPin=GetCustomPin(注释为MKPointAnnotation);
如果(customPin==null)
{
抛出新异常(“未找到自定义pin”);
}
annotationView=mapView.DequeueReusableAnnotation(customPin.Label);
if(annotationView==null)
{
annotationView=新的CustomMKAnnotationView(注释,customPin.Label);
annotationView.Image=UIImage.FromFile(“pin.png”);
annotationView.CalloutOffset=新的CGPoint(0,0);
UIImageView UIImageView=新的UIImageView(UIImage.FromFile(“monkey.png”);
uIImageView.Frame=新的CGRect(0,0,75,100);
annotationView.LeftCalloutAccessoryView=uIImageView;
//annotationView.LeftCalloutAccessoryView=新的UIImageView(UIImage.FromFile(“monkey.png”);
//annotationView.RightCalloutAccessoryView=UIButton.FromType(UIButtonType.DetailDisclosure);
((CustomMKAnnotationView)annotationView).Name=customPin.Label;
//((CustomMKAnnotationView)annotationView.Url=customPin.Url;
customPinView=新UIStackView();
foreach(customPin.InfoBox.detailsObjectInfo中的变量项)
{
var label=新的UILabel();
label.Text=item.BoldLabelTitle+item.LabelValue;
label.BackgroundColor=UIColor.White;
标签字体大小(36);
customPinView.AddArrangedSubview(标签);
}
Frame=新的CGRect(0,0,300,84);
customPinView.Axis=UILayoutConstraintAxis.Vertical;
customPinView.Distribution=UIStackViewDistribution.EqualSpacing;
customPinView.Spacing=1;
customPinView.Alignment=UIStackViewAlignment.Fill;
annotationView.DetailCalloutAccessoryView=customPinView;
UITapGestureRecognizer tapGestureRecognizer=新建
UITapGestureRecognitor((手势)=>
{
Shell.Current.GoToAsync(Routes.ObjectParametersPage);
});
annotationView.DetailCalloutAccessoryView.AddGestureRecognitizer(TapGestureRecognitizer);
}
annotationView.CanShowCallout=true;
返回注释视图;
}
CalloutAccessoryControltApped上受保护的虚拟无效(对象发送方,mkmapViewAccessorTappedEventArgs e)
{
Shell.Current.GoToAsync(Routes.ObjectParametersPage);
//(App.Current作为App.NavigationPage.Navigation.PushAsync(新ContentPage());
}
void OnDidSelectAnnotationView(对象发送方,MKAnnotationViewEventArgs e)
{
CustomMKAnnotationView customView=e.作为CustomMKAnnotationView查看;
}
void OnDidCancelAnnotationView(对象发送方,MKAnnotationViewEventArgs e)
{
如果(!e.View.Selected)
{
customPinView.RemoveFromSuperview();
customPinView.Dispose();
customPinView=null;
}
}
CustomPin GetCustomPin(MKPointAnnotation)
{
var位置=新位置(annotation.Coordinate.Latitude,annotation.Coordinate.Longitude);
foreach(customPins中的var pin)
{
如果(销位置==位置)
{
[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]
namespace OperaMobile.iOS.Renderers
{
public class CustomMapRenderer : MapRenderer
{
UIStackView customPinView;
ObservableCollection<CustomPin> customPins;
protected override void OnElementChanged(ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
var nativeMap = Control as MKMapView;
nativeMap.GetViewForAnnotation = null;
nativeMap.CalloutAccessoryControlTapped -= OnCalloutAccessoryControlTapped;
nativeMap.DidSelectAnnotationView -= OnDidSelectAnnotationView;
nativeMap.DidDeselectAnnotationView -= OnDidDeselectAnnotationView;
}
if (e.NewElement != null)
{
var formsMap = (CustomMap)e.NewElement;
var nativeMap = Control as MKMapView;
customPins = formsMap.CustomPins;
nativeMap.GetViewForAnnotation = GetViewForAnnotation;
nativeMap.CalloutAccessoryControlTapped += OnCalloutAccessoryControlTapped;
nativeMap.DidSelectAnnotationView += OnDidSelectAnnotationView;
nativeMap.DidDeselectAnnotationView += OnDidDeselectAnnotationView;
}
}
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.Label);
if (annotationView == null)
{
annotationView = new CustomMKAnnotationView(annotation, customPin.Label);
annotationView.Image = UIImage.FromFile("pin.png");
annotationView.CalloutOffset = new CGPoint(0, 0);
UIImageView uIImageView = new UIImageView(UIImage.FromFile("monkey.png"));
uIImageView.Frame = new CGRect(0, 0, 75, 100);
annotationView.LeftCalloutAccessoryView = uIImageView;
//annotationView.LeftCalloutAccessoryView = new UIImageView(UIImage.FromFile("monkey.png"));
//annotationView.RightCalloutAccessoryView = UIButton.FromType(UIButtonType.DetailDisclosure);
((CustomMKAnnotationView)annotationView).Name = customPin.Label;
//((CustomMKAnnotationView)annotationView).Url = customPin.Url;
customPinView = new UIStackView();
foreach (var item in customPin.InfoBox.DetailsObjectInfos)
{
var label = new UILabel();
label.Text = item.BoldLabelTitle + item.LabelValue;
label.BackgroundColor = UIColor.White;
label.Font.WithSize(36);
customPinView.AddArrangedSubview(label);
}
customPinView.Frame = new CGRect(0, 0, 300, 84);
customPinView.Axis = UILayoutConstraintAxis.Vertical;
customPinView.Distribution = UIStackViewDistribution.EqualSpacing;
customPinView.Spacing = 1;
customPinView.Alignment = UIStackViewAlignment.Fill;
annotationView.DetailCalloutAccessoryView = customPinView;
UITapGestureRecognizer tapGestureRecognizer = new
UITapGestureRecognizer((gesture) =>
{
Shell.Current.GoToAsync(Routes.ObjectParametersPage);
});
annotationView.DetailCalloutAccessoryView.AddGestureRecognizer(tapGestureRecognizer);
}
annotationView.CanShowCallout = true;
return annotationView;
}
protected virtual void OnCalloutAccessoryControlTapped(object sender, MKMapViewAccessoryTappedEventArgs e)
{
Shell.Current.GoToAsync(Routes.ObjectParametersPage);
//(App.Current as App).NavigationPage.Navigation.PushAsync(new ContentPage());
}
void OnDidSelectAnnotationView(object sender, MKAnnotationViewEventArgs e)
{
CustomMKAnnotationView customView = e.View as CustomMKAnnotationView;
}
void OnDidDeselectAnnotationView(object sender, MKAnnotationViewEventArgs e)
{
if (!e.View.Selected)
{
customPinView.RemoveFromSuperview();
customPinView.Dispose();
customPinView = null;
}
}
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;
}
}}
<RelativeLayout>
<local:CustomMap x:Name="customMap"
IsShowingUser="True"
MapType="Street"
IsVisible="true"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Width,
Factor=0,
Constant=0}"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Height,
Factor=0,
Constant=0}"
RelativeLayout.WidthConstraint="{ConstraintExpression
Type=RelativeToParent,Property=Width,Factor=1,Constant=0}"
RelativeLayout.HeightConstraint="{ConstraintExpression
Type=RelativeToParent,Property=Height,Factor=1,Constant=0}" />
<ImageButton Source="location.png"
Clicked="Button_Clicked"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Width,
Factor=0.6,
Constant=100}"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Height,
Factor=0.8,
Constant=80}" />
</RelativeLayout>
private async void Button_Clicked(object sender, System.EventArgs e)
{
var request = new GeolocationRequest(GeolocationAccuracy.Medium);
var location = await Geolocation.GetLocationAsync(request);
if (location != null)
{
Console.WriteLine($"Latitude: {location.Latitude}, Longitude: {location.Longitude}, Altitude: {location.Altitude}");
}
customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(location.Latitude, -location.Longitude), Distance.FromMiles(1.0)));
}
protected override void OnAppearing()
{
base.OnAppearing();
if(Device.RuntimePlatform == "Android")
{
LocationButton.IsVisible = false;
}
}