使用 Monodroid/Xamarin 在地图上绘制叠加层 [英] Draw Overlay on Map with Monodroid/Xamarin

查看:36
本文介绍了使用 Monodroid/Xamarin 在地图上绘制叠加层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像叠加添加到自定义渲染器中的 Xamarin/Android 地图.

I am trying to add an image overlay to my Xamarin/Android map which is in a custom renderer.

我无法让它出现在地图上.这个食谱似乎过时了 http://developer.xamarin.com/recipes/android/control/mapview/add_an_overlay_to_a_map/ 或者我只是想念 Android.GoogleMaps.Overlay 类.

I cannot get it to appear on the map. This recipe seems outdated http://developer.xamarin.com/recipes/android/controls/mapview/add_an_overlay_to_a_map/ or I am just missing the Android.GoogleMaps.Overlay class somehow.

public class MapContentPageRenderer: PageRenderer
    {
        Android.Views.View _view;
        private GoogleMap _map;
        private MapFragment _mapFragment;
        private LatLng _initLatLng;

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

            var page = e.NewElement as MapContentPage;
            var activity = this.Context as Activity;

            if (activity != null)
            {
                _view = activity.LayoutInflater.Inflate(Resource.Layout.MapLayout, this, false);
                AddView(_view);
                if (page != null) _initLatLng = new LatLng(page.InitLat, page.InitLng);
                InitMapFragment(activity);
            }
            SetupMapIfNeeded();
        }

        protected override void OnLayout(bool changed, int l, int t, int r, int b)
        {
            base.OnLayout(changed, l, t, r, b);
            var msw = MeasureSpec.MakeMeasureSpec(r - l, MeasureSpecMode.Exactly);
            var msh = MeasureSpec.MakeMeasureSpec(b - t, MeasureSpecMode.Exactly);
            if (_view == null) return;
            _view.Measure(msw, msh);
            _view.Layout(0, 0, r - l, b - t);
        }

        private void InitMapFragment(Activity activity)
        {
            var cameraPosition = new CameraPosition.Builder().Target(_initLatLng).Zoom(14.0f).Build();
            _mapFragment = activity.FragmentManager.FindFragmentByTag("mapWithOverlay") as MapFragment;
            if (_mapFragment != null) return;
            var mapOptions = new GoogleMapOptions()
                .InvokeMapType(GoogleMap.MapTypeNormal)
                .InvokeRotateGesturesEnabled(false)
                .InvokeZoomControlsEnabled(false)
                .InvokeCamera(cameraPosition)
                .InvokeCompassEnabled(true);

            var fragTx = activity.FragmentManager.BeginTransaction();
            _mapFragment = MapFragment.NewInstance(mapOptions);

            fragTx.Add(Resource.Id.mapWithOverlay, _mapFragment, "mapWithOverlay");
            fragTx.Commit();
        }

        private void SetupMapIfNeeded()
        {
            if (_map != null) return;
            _map = _mapFragment.Map;
            if (_map == null) return;
            ZoomToPosition();

            var mapOverlay = new GroundOverlayOptions()
                .Position(_initLatLng, 150, 200)
                .InvokeImage(BitmapDescriptorFactory.FromResource(Resource.Drawable.Icon));
            _map.AddGroundOverlay(mapOverlay);
        }

        private void ZoomToPosition()
        {
            var cameraUpdate = CameraUpdateFactory.NewLatLngZoom(_initLatLng, 15);

            _map.MoveCamera(cameraUpdate);
        }
    }

推荐答案

Android.GoogleMaps.Overlay 已弃用.

既然如此,您不应该尝试将其用作链接您发布的已过期.

Being so you should not attempt to use this as the example in the link you posted is out-of-date.

相反,您应该使用 GroundOverlayTileOverlay for Android GoogleMaps,或其他<强>叠加适用于您要实现的目标.

Instead you should use a GroundOverlay or a TileOverlay for Android GoogleMaps, or one of the other overlays that are applicable depending on what you are trying to achieve.

更新 1:-

这是一个创建自定义GroundOverlay的示例:-

Here is an example that creates a custom GroundOverlay of:-

  • 100 x 100 像素
  • 绘制一个红色的半透明圆圈
  • 然后是一些显示18"的黄色文本

cobjGoogleMap 将是 GoogleMap 类型的地图引用,之前已经定义和引用过.

cobjGoogleMap would be your map reference of type GoogleMap, already previously defined and referenced.

就像您拥有的参考 link 一样,它创建了画布,并展示如何在将画布添加到地图之前在画布上绘制一些东西.希望这能让您走上创建自定义绘制叠加层的正确道路?

Like in the reference link that you had, it creates a Canvas and shows how to draw a couple things on this canvas, prior to adding this to the map. Hopefully this should set you on the right path to creating your custom drawn overlays?

private GroundOverlay cobjGroundOverlay = null;

private void AddMyCustomDrawnOverlayToMap()
{
    LatLng objMapPosition = new LatLng(51, -1.1);
    int intWidth = 100;
    int intHeight = 100;
    //
    using (Bitmap objBitmap = GenerateMyCustomDrawnOverlay(intWidth, intHeight))
    {
        using (Android.Gms.Maps.Model.BitmapDescriptor objBitmapDescriptor = Android.Gms.Maps.Model.BitmapDescriptorFactory.FromBitmap(objBitmap))
        {
            GroundOverlayOptions objGroundOverlayOptions = new GroundOverlayOptions()
                .Position(objMapPosition, intWidth, intHeight)
                .InvokeImage(objBitmapDescriptor);
            //
            cobjGroundOverlay = cobjGoogleMap.AddGroundOverlay(objGroundOverlayOptions);
        }
    }
    //
    // Move the camera to the position where we added the new ground overlay:-
    CameraUpdate objCameraUpdate = CameraUpdateFactory.NewLatLngZoom(objMapPosition, 17);
    cobjGoogleMap.MoveCamera(objCameraUpdate);
}


private Bitmap GenerateMyCustomDrawnOverlay(int pintWidth, int pintHeight)
{
    // Create your custom overlay Bitmap using a Canvas object:-
    //
    var objPaint = new global::Android.Graphics.Paint();
    objPaint.AntiAlias = true;
    objPaint.Color = global::Android.Graphics.Color.Red;
    objPaint.Alpha = 150;
    //
    var objPaint2 = new global::Android.Graphics.Paint();
    objPaint2.AntiAlias = true;
    objPaint2.Color = global::Android.Graphics.Color.Yellow;
    objPaint2.TextSize = 90;
    objPaint2.Alpha = 255;
    //
    Bitmap objBitmap = Bitmap.CreateBitmap(pintWidth, pintHeight, Bitmap.Config.Argb8888);
    Canvas objCanvas = new Canvas(objBitmap);
    //
    int intHalfWidth = pintWidth / 2;
    int intHalfHeight = pintHeight / 2;
    //
    // Draw a circle (red / semi-transparent):-
    objCanvas.DrawRoundRect(new global::Android.Graphics.RectF(0, 0, (float)pintWidth, (float)pintHeight), intHalfWidth, intHalfHeight, objPaint);
    //
    // Draw some text, although your probably want to do MeasureText etc in order to center things properly:-
    objCanvas.DrawText("18", 0, 80, objPaint2);
    //
    // Return the bitmap that will be used:-
    return objBitmap;
}

这篇关于使用 Monodroid/Xamarin 在地图上绘制叠加层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆