我在 Fragment 中使用了 MapView,我使用的以下代码和我的标记等都正确呈现。希望有人能从这段代码中获得帮助,因为我自己无法获得直接帮助,我将 MapView 的 Native Java 代码翻译成 Xamarin Android C#。
我的片段代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/arbitraryView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Title" />
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
那么My Fragment File代码如下:
public class PropertyMapView : Fragment, IOnMapReadyCallback
{
private GoogleMap googleMap;
private MapView mapView;
private bool mapsSupported = true;
static readonly LatLng Location_Xamarin = new LatLng(37.80, -122.40);
static readonly LatLng Location_NewYork = new LatLng(40.77, -73.98);
static readonly LatLng Location_Chicago = new LatLng(41.88, -87.63);
static readonly LatLng Location_Dallas = new LatLng(32.90, -97.03);
static readonly LatLng Location_Author = new LatLng(47.855334, 12.184079);
public override void OnActivityCreated(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
try
{
MapsInitializer.Initialize(Activity);
}
catch (GooglePlayServicesNotAvailableException e)
{
mapsSupported = false;
}
if (mapView != null)
{
mapView.OnCreate(savedInstanceState);
}
//initialize Map
mapView.GetMapAsync(this);
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//return base.OnCreateView(inflater, container, savedInstanceState);
var view = inflater.Inflate(Resource.Layout.PropMapView, container, false);
mapView = view.FindViewById<MapView>(Resource.Id.mapView);
return view;
}
public void OnMapReady(GoogleMap map)
{
googleMap = map;
googleMap.MapType = GoogleMap.MapTypeNormal;
googleMap.MyLocationEnabled = true;
googleMap.UiSettings.CompassEnabled = false;
googleMap.UiSettings.MyLocationButtonEnabled = true;
googleMap.UiSettings.ZoomControlsEnabled = true;
//googleMap.UiSettings.SetAllGesturesEnabled(false);
googleMap.AddMarker(new MarkerOptions()
.SetPosition(Location_NewYork));
googleMap.AddMarker(new MarkerOptions()
.SetPosition(Location_Xamarin)
.SetTitle("Xamarin HQ")
.SetSnippet("Where the magic happens")
.SetIcon(BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueOrange)));
Marker chicagoMarker = googleMap.AddMarker(new MarkerOptions()
.SetPosition(Location_Chicago)
.SetTitle("Chicago")
.Draggable(true)
.SetIcon(BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueYellow)));
Marker dallasMarker = googleMap.AddMarker(new MarkerOptions()
.SetPosition(Location_Dallas)
.SetTitle("Dallas")
.SetSnippet("Go Cowboys!")
.InfoWindowAnchor(1, 0)
.SetIcon(BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueBlue)));
googleMap.MarkerClick += delegate (object sender, GoogleMap.MarkerClickEventArgs e) {
if (e.Marker.Equals(dallasMarker))
{
dallasMarker.Flat = !dallasMarker.Flat;
dallasMarker.ShowInfoWindow();
}
else
{
// Execute default behavior for other markers.
e.Handled = false;
}
};
googleMap.InfoWindowClick += (sender, e) =>
{
if (e.Marker.Id == chicagoMarker.Id)
{
e.Marker.SetIcon(BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueRose));
}
};
googleMap.MapClick += (sender, e) =>
{
if (!chicagoMarker.IsInfoWindowShown)
{
chicagoMarker.SetIcon(BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueYellow));
}
};
googleMap.AddMarker(new MarkerOptions()
.SetPosition(Location_Author)
.SetTitle("Author")
.SetSnippet("Author Home")
.SetIcon(BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueViolet)));
googleMap.MapLongClick += (sender, e) =>
googleMap.AnimateCamera(CameraUpdateFactory.ZoomOut(), 1000, null);
// Center on Xamarin HQ
CameraUpdate update = CameraUpdateFactory.NewLatLngZoom(Location_Author, googleMap.MaxZoomLevel);
googleMap.MoveCamera(update);
}
public override void OnSaveInstanceState(Bundle outState)
{
base.OnSaveInstanceState(outState);
mapView.OnSaveInstanceState(outState);
}
public override void OnResume()
{
base.OnResume();
mapView.OnResume();
}
public override void OnPause()
{
base.OnPause();
mapView.OnPause();
}
public override void OnDestroy()
{
base.OnDestroy();
mapView.OnDestroy();
}
public override void OnLowMemory()
{
base.OnLowMemory();
mapView.OnLowMemory();
}
}