将Google地图设置为背景而不使用Xamarin表单地图(Android)

时间:2017-01-29 15:38:48

标签: google-maps xamarin.android xamarin.forms

我尝试使用xamarin表格将谷歌地图设置为页面背景。我能够使用自定义页面渲染器在iOS中实现此目的。我们的想法是创建一个继承自ContentPage的页面,然后使用自定义页面渲染器覆盖页面。我能够使用以下代码在iOS上实现此目的:

using System;
using MyProject;
using MyProject.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using Google.Maps;
using CoreGraphics;

[assembly: ExportRenderer(typeof(MapPage), typeof(MapPageRenderer))]
namespace MyProject.iOS
{

// MapPageRenderer.cs
public class MapPageRenderer : PageRenderer
{
    private MapView mapView;

    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);
    }

    public override void LoadView()
    {
        base.LoadView();

        CameraPosition camera = CameraPosition.FromCamera(latitude: 6.438068, 
                                                      longitude: 3.472604,
                                                      zoom: 15);
        mapView = MapView.FromCamera(CGRect.Empty, camera);
        mapView.MyLocationEnabled = true;
        mapView.MapType = MapViewType.Normal;
        mapView.Settings.ConsumesGesturesInView = false;
        mapView.Settings.CompassButton = true;

        mapView.UserInteractionEnabled = true;

        View = mapView;
    }

    public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);
        //mapView.StartRendering();
    }

    public override void ViewWillDisappear(bool animated)
    {
        //mapView.StopRendering();
        base.ViewWillDisappear(animated);
    }
}
}

但是我无法在Android中获得相同的结果,任何帮助将不胜感激。这是我目前用于android的自定义页面渲染器:

using Xamarin.Forms.Platform.Android;
using MyProject;
using Xamarin.Forms;
using Xamarin.Forms.Maps.Android;
using MyProject.Droid;
using Android.Gms.Maps;
using Android.Gms.Maps.Model;

[assembly: ExportRenderer(typeof(MapPage), typeof(MapPageRenderer))]
namespace MyProject.Droid
{
public class MapPageRenderer : PageRenderer, IOnMapReadyCallback
{
    private MapView mapView;
    GoogleMap map;

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

        //if (e.OldElement != null || Element == null)
        //{
        //  return;
        //}
        //CameraPosition camera = CameraPosition.FromLatLngZoom(new LatLng(6.438068, 3.472604), 15);

        //mapView = new Android.Gms.Maps.MapView(Context);
        //mapView.GetMapAsync(this);
        //AddView(mapView);
    }
    public override void AddView(Android.Views.View child)
    {
        mapView = new Android.Gms.Maps.MapView(Context);
        mapView.GetMapAsync(this);
        mapView.AddView(child);

        base.AddView(mapView);
    }

    public void OnMapReady(GoogleMap googleMap)
    {
        this.map = googleMap;
        //Setup and customize your Google Map
        map.UiSettings.CompassEnabled = false;
        map.UiSettings.MyLocationButtonEnabled = false;
        map.UiSettings.MapToolbarEnabled = false;
    }
}
}

0 个答案:

没有答案