我正在使用带有自定义地图的Xamarin.Forms.Maps和带有Droid Renderer的自定义引脚。我想重新定位地图右下角的myLocationButton和地图左下角的北按钮。
这是CustomRenderer。 [assembly:ExportRenderer(typeof(CustomMap),typeof(CustomMapRenderer))]
命名空间CustomRenderer.Droid {
public class CustomMapRenderer:MapRenderer,GoogleMap.IInfoWindowAdapter
{
List<CustomPin> customPins;
bool isDrawn;
protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Map> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
NativeMap.InfoWindowClick -= OnInfoWindowClick;
}
if (e.NewElement != null)
{
var formsMap = (CustomMap)e.NewElement;
customPins = formsMap.CustomPins;
Control.GetMapAsync(this);
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName.Equals("VisibleRegion") && !isDrawn)
{
NativeMap.Clear();
NativeMap.UiSettings.ScrollGesturesEnabled = true;
NativeMap.InfoWindowClick += OnInfoWindowClick;
NativeMap.SetInfoWindowAdapter(this);
NativeMap.UiSettings.MyLocationButtonEnabled = true;
NativeMap.MyLocationEnabled = true;
foreach (var pin in customPins)
{
var marker = new MarkerOptions();
marker.SetPosition(new LatLng(pin.Pin.Position.Latitude, pin.Pin.Position.Longitude));
marker.SetTitle(pin.Pin.Label);
marker.SetSnippet(pin.Pin.Address);
if (pin.Id == "0")
{
marker.SetIcon(BitmapDescriptorFactory.FromResource(project.Droid.Resource.Drawable.googlecab));
}
else if (pin.Id == "1")
{
marker.SetIcon(BitmapDescriptorFactory.FromResource(project.Droid.Resource.Drawable.googlehome));
}
else if (pin.Id == "2")
{
marker.SetIcon(BitmapDescriptorFactory.FromResource(project.Droid.Resource.Drawable.googlerv));
}
else if (pin.Id == "Xamarin")
{
marker.SetIcon(BitmapDescriptorFactory.FromResource(project.Droid.Resource.Drawable.googlerv));
}
NativeMap.AddMarker(marker);
}
isDrawn = false;
}
}
protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
base.OnLayout(changed, l, t, r, b);
if (changed)
{
isDrawn = false;
}
}
void OnInfoWindowClick(object sender, GoogleMap.InfoWindowClickEventArgs e)
{
var customPin = GetCustomPin(e.Marker);
if (customPin == null)
{
throw new Exception("Custom pin not found");
}
if (!string.IsNullOrWhiteSpace(customPin.Url))
{
//var url = Android.Net.Uri.Parse(customPin.Url);
//var intent = new Intent(Intent.ActionView, url);
//intent.AddFlags(ActivityFlags.NewTask);
//Android.App.Application.Context.StartActivity(intent);
MessagingCenter.Send(new CategoryItems()
{
Vid = customPin.vId,
VType = customPin.vType
}, "UserName");
}
}
public Android.Views.View GetInfoContents(Marker marker)
{
isDrawn = true;
var inflater = Android.App.Application.Context.GetSystemService(Context.LayoutInflaterService) as Android.Views.LayoutInflater;
if (inflater != null)
{
Android.Views.View view;
var customPin = GetCustomPin(marker);
if (customPin == null)
{
throw new Exception("Custom pin not found");
}
view = inflater.Inflate(project.Droid.Resource.Layout.XamarinMapInfoWindow, null);
var infoTitle = view.FindViewById<TextView>(project.Droid.Resource.Id.InfoWindowTitle);
var infoSubtitle = view.FindViewById<TextView>(project.Droid.Resource.Id.InfoWindowSubtitle);
var infoBg = view.FindViewById<ImageView>(project.Droid.Resource.Id.InfoWindowBackgroundImage);
if (infoTitle != null)
{
infoTitle.Text = marker.Title;
}
if (infoSubtitle != null)
{
infoSubtitle.Text = marker.Snippet;
}
if (infoBg != null){
if(!string.IsNullOrEmpty(customPin.Url)){
Picasso.With(Context)
.Load(customPin.Url)
.Into(infoBg);
}
}
return view;
}
isDrawn = true;
return null;
}
public Android.Views.View GetInfoWindow(Marker marker)
{
return null;
}
CustomPin GetCustomPin(Marker annotation)
{
var position = new Position(annotation.Position.Latitude, annotation.Position.Longitude);
foreach (var pin in customPins)
{
if (pin.Pin.Position == position)
{
return pin;
}
}
return null;
}
}
}
答案 0 :(得分:0)
我使用了自定义渲染器,因此我试图通过使用FindViewByID使用其ID来获取myLocationButton而没有运气。
最后我找到了解决方案。不要获取视图并设置其布局。你只需要给地图一个填充。 那就是
NativeMap.SetPadding(0,800,0,0);