在Xamarin.Forms.Maps中更改缩放按钮的位置(+ - )

时间:2017-04-06 11:12:31

标签: c# xamarin.android xamarin.forms

我想更改Zoom button controls (+)(-)mapXamarin.Forms的位置。可能吗?

3 个答案:

答案 0 :(得分:1)

经过一番研究,我认为没有办法重新定位谷歌地图的默认缩放控制,但正如我们所说,你可以使用自定义渲染器来创建自己的地图控件,这样你就可以自定义缩放控制,例如:

<Grid>
    <controls:MapWithMyZoomControl x:Name="map"
          HorizontalOptions="FillAndExpand"
          VerticalOptions="FillAndExpand" />

    <StackLayout Orientation="Vertical">
        <Button Text="Zoom In" Clicked="Zoom_In" />
        <Button Text="Zoom Out" Clicked="Zoom_Out" />
    </StackLayout>
</Grid>

MapWithMyZoomControl:

public class MapWithMyZoomControl : Map
{
    public ZoomState MyZoom
    {
        get { return (ZoomState)GetValue(MyZoomProperty); }
        set { SetValue(MyZoomProperty, value); }
    }

    public static readonly BindableProperty MyZoomProperty =
        BindableProperty.Create(
            propertyName: "MyZoom",
            returnType: typeof(ZoomState),
            declaringType: typeof(MapWithMyZoomControl),
            defaultValue: ZoomState.normal,
            propertyChanged: OnZoomPropertyChanged);

    public static void OnZoomPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
    }

    public enum ZoomState
    {
        normal,
        zoomin,
        zoomout
    }
}

渲染器:

public class MapWithMyZoomControlRenderer : MapRenderer, IOnMapReadyCallback
{
    private GoogleMap map;

    public void OnMapReady(GoogleMap googleMap)
    {
        map = googleMap;

        map.UiSettings.ZoomControlsEnabled = false;
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Map> e)
    {
        base.OnElementChanged(e);

        if (e.OldElement != null)
        {
            // Unsubscribe
        }

        if (e.NewElement != null)
        {
            var formsMap = (MapWithMyZoomControl)e.NewElement;

            ((MapView)Control).GetMapAsync(this);
        }
    }

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
        var element = Element as MapWithMyZoomControl;
        if (e.PropertyName == "MyZoom" && map != null)
        {
            if (element.MyZoom == MapWithMyZoomControl.ZoomState.zoomin)
            {
                map.AnimateCamera(CameraUpdateFactory.ZoomIn());
            }
            else if (element.MyZoom == MapWithMyZoomControl.ZoomState.zoomout)
            {
                map.AnimateCamera(CameraUpdateFactory.ZoomOut());
            }
            element.MyZoom = MapWithMyZoomControl.ZoomState.normal;
        }
    }
}

不要忘记在渲染器中添加程序集,如下所示:

[assembly: ExportRenderer(typeof(MapWithMyZoomControl), typeof(MapWithMyZoomControlRenderer))]

现在您可以更改按钮样式和位置。

答案 1 :(得分:1)

你可以只使用 padding 属性。

<maps:Map
    Padding="0, 0, 0, 20">

答案 2 :(得分:0)

namespace WD.EMP.APP.Droid.Renderer {
    public class GEMapRenderer: MapRenderer {
        protected override void OnMapReady(Android.Gms.Maps.GoogleMap googleMap) {
            base.OnMapReady(googleMap);
            NativeMap.SetPadding(0, 0, 0, 500);
        }
    }
}

NativeMap.SetPadding(0,0,0,500 );  

通过设置padding google map将解决您的问题,将NativeMap.SetPadding(0,0,0,500 );放在onmapready函数的渲染器文件中