monodroid supportmapfragment.map始终返回null

时间:2013-11-11 15:41:15

标签: c# xamarin.android google-maps-android-api-2 supportmapfragment

我正试图通过此代码使用谷歌地图版本2:

    public   void setMapData()
    {
        try {
            if (mapView == null && _mapFragment != null)
            {
                mapView = _mapFragment.Map;
                if (mapView != null)
                {
                    CameraPosition currentPlace = new CameraPosition.Builder()
                        .Target (currentProperty.Location)
                            .Bearing (0) 
                            .Zoom (((RPLApp)pshowAct.Application).typeOfShow .PropertyZoomLevel) //apply zoom here
                            .Build ();

                    mapView.AnimateCamera  (CameraUpdateFactory.NewCameraPosition (currentPlace));

                    mapView .Clear ();
                    MarkersHelperClass.DrawMarkerOnLocation (pshowAct , mapView, currentProperty.Location);
                }

            }
        } catch (Exception ex) {
            RltLog .HandleException (ex);
        }
    }

mapView = _mapFragment.Map;始终返回null。在url下面有一个java代码的解决方案,但它不适用于monodroid。我们如何在monodroid或c#中使用它?它不适合,因为代码在内部和活动中定义了一个新类。我们如何使用C#语法使它成为朋友?

https://stackoverflow.com/a/14072348/1939409

1 个答案:

答案 0 :(得分:0)

您在Google地图中使用了哪些组件?你试过Google Play服务吗? How to implement google play services using android sdk 17 on xamarin

编辑: 这是我的布局(Resource.Layout.GoogleMap):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <FrameLayout
            android:id="@+id/mapView"
            android:layout_width="fill_parent"
            android:layout_height="match_parent" />
            //other controls
</RelativeLayout>

我的代码:

public class GoogleMapActivity : Android.Support.V4.App.FragmentActivity
{
    private Android.Gms.Maps.GoogleMap _mapView;
    private Android.Gms.Maps.SupportMapFragment _fragment;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.GoogleMap);

        var mapOptions = new Android.Gms.Maps.GoogleMapOptions()
            .InvokeMapType(Android.Gms.Maps.GoogleMap.MapTypeNormal)
            .InvokeZoomControlsEnabled(false)
            .InvokeCompassEnabled(true);

        var fragTx = SupportFragmentManager.BeginTransaction();
        var mapFragment = Android.Gms.Maps.SupportMapFragment.NewInstance(mapOptions);
        fragTx.Add(Resource.Id.mapView, mapFragment, "mapView");
        fragTx.Commit();
    }

    protected override void OnResume()
    {
        base.OnResume();

        _fragment = ((Android.Gms.Maps.SupportMapFragment)SupportFragmentManager.FindFragmentById(Resource.Id.mapView));
        _mapView = _fragment.Map;
    }
}