Click here for Screenshot我正在尝试使用Xamarin.Forms为andriod显示谷歌地图。我想在屏幕的一半显示地图,所以我将ContentView添加到我的ContentPage。
public partial class FacilitiesProductivity : ContentPage
{
public FacilitiesProductivity()
{
InitializeComponent();
Content = new ContentView { Content = new ProductivityMapPage(new Managers.Facility()) };
}
}
ProductivityMapPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<local:ProductivityMapCustomContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:maps="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps"
xmlns:local="clr-namespace:RadLocWF;assembly=RadLocWF"
x:Class="RadLocWF.views.ProductivityMapPage"/>
ProductivityMapCustomContentView.cs
public class ProductivityMapCustomContentView : ContentView
{
}
ProductivityMapRenderer.cs
[assembly: ExportRenderer(typeof(ProductivityMapCustomContentView), typeof(ProductivityMapRenderer))]
namespace RadLoc.Droid.Renderer
{
class ProductivityMapRenderer : ViewRenderer, IOnMapReadyCallback
{
Location _location;
Android.Views.View _view;
GoogleMap _map;
Marker currentLocationMarker;
public ProductivityMapRenderer()
{
SetUpMap();
}
public void SetUpMap()
{
var activity = this.Context as Activity;
MapFragment mapFrag = (MapFragment)activity.FragmentManager.FindFragmentById(Resource.Id.maps);
mapFrag.GetMapAsync(this);
}
public void OnMapReady(GoogleMap googleMap)
{
_map = googleMap;
_map.MyLocationEnabled = true;
_map.MapType = GoogleMap.MapTypeNormal;
_map.UiSettings.CompassEnabled = true;
_map.UiSettings.MyLocationButtonEnabled = true;
_map.UiSettings.ZoomControlsEnabled = true;
double lat = 0, lng = 0;
LatLng location = new LatLng(lat, lng);
CameraPosition.Builder builder = CameraPosition.InvokeBuilder();
builder.Target(location);
builder.Zoom(12f);
builder.Bearing(90);
builder.Tilt(40);
CameraPosition cameraPosition = builder.Build();
CameraUpdate cameraUpdate = CameraUpdateFactory.NewCameraPosition(cameraPosition);
_map?.AnimateCamera(cameraUpdate);
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.View> e)
{
base.OnElementChanged(e);
var activity = this.Context as Activity;
_view = activity.LayoutInflater.Inflate(Resource.Layout.ProductivityMapLayout, this, false);
var mapFragment = activity.FragmentManager.FindFragmentById<MapFragment>(Resource.Id.maps);
AddView(_view);
}
protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
if (!isMapReady)
{
base.OnLayout(changed, l, t, r, b);
var msw = MeasureSpec.MakeMeasureSpec(r - l, MeasureSpecMode.Exactly);
var msh = MeasureSpec.MakeMeasureSpec(b - t, MeasureSpecMode.Exactly);
_view.Measure(msw, msh);
_view.Layout(0, 0, r - l, b - t);
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
var activity = this.Context as Activity;
var mapFragment = activity.FragmentManager.BeginTransaction().Remove(activity.FragmentManager.FindFragmentById<MapFragment>(Resource.Id.maps)).Commit();
}
}
ProductivityMapLayout.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<View
android:layout_height="0dp"
android:layout_width="fill_parent"
android:layout_weight="1" />
<fragment
class="com.google.android.gms.maps.MapFragment"
android:id="@+id/maps"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginRight="0.0dp"
android:layout_marginTop="0.0dp"
android:layout_marginBottom="0.0dp" />
</LinearLayout>
代码没有给出任何错误,但显示黑屏。如果单独的屏幕上出现Google标记可能是Google API Key问题,但我只是白屏。因此,渲染必定存在问题。任何人都可以帮我解决问题。谢谢!