在MKMapView上叠加图像

时间:2016-09-01 01:31:18

标签: c# ios xamarin

这一切都在Xamarin for iOS中完成。

我有UIImage天气雷达信息。我需要在mapview上叠加此图像。不是作为注释,而是在某种程度上它将保留在地图上而不是留在屏幕上。 (图像应以lat& long为中心,而不是屏幕上的x& y点)

我已广泛搜索过此内容,但如果有人发现我错过的链接可以解决我的问题,我会非常高兴。

到目前为止,我已尝试创建自定义MKOverlay和MKOverlayRenderer,但我不确定如何使用渲染器。下面是我的自定义叠加层的略微剥离版本(如果它有用)。我也不确定如何在CalculateDerivedPosition()中设置界限。

public class RadarGroundOverlayBounds : MKOverlay
{

    public CLLocationCoordinate2D mOrigin
    {
        get { return _mOrigin; }
        set
        {
            _mOrigin = value;
            CalculateDerivedPosition(_mOrigin);
        }
    }
    public MKMapRect bounds
    {
        get;
        private set;
    }

    public override MKMapRect BoundingMapRect
    {
        get
        {
            return bounds;
        }
    }

    public override CLLocationCoordinate2D Coordinate
    {
        get
        {
            return mOrigin;
        }
    }

    private const double EarthRadius = 6378137.0, Tau = Math.PI * 2, DegreesToRadians = 0.0174532925, RadiansToDegrees = 57.2957795;

    public RadarGroundOverlayBounds(CLLocationCoordinate2D origin)
    {
        mOrigin = origin;
        mWidth = 256;
        mHeight = 256;
    }

    public async Task<UIImage> GetImage()
    {
    }

    private void CalculateDerivedPosition(CLLocationCoordinate2D origin)
    {

        double latRadian = origin.Latitude * DegreesToRadians;

        const double degLatKm = 110.574235;
        double degLongKm = 110.572833 * Math.Cos(latRadian);
        double deltaLat = DEFAULT_RADAR_KM / degLatKm;
        double deltaLong = DEFAULT_RADAR_KM / degLongKm;
        //bounds = ;
        /*new LatLngBounds(new LatLng(origin.Latitude - deltaLat, origin.Longitude - deltaLong),
            new LatLng(origin.Latitude + deltaLat, origin.Longitude + deltaLong))*/
    }

    public class RadarOverlayRenderer : MKOverlayRenderer {

        public override void DrawMapRect(MKMapRect mapRect, nfloat zoomScale, CGContext context)
        {
            InvokeOnMainThread(
                () =>
                {
                    UIImage image = ((RadarGroundOverlayBounds)this.Overlay).GetImage().Result;

                    DrawImgRotated(image, 0, context);
                }
            );
        }

        public void DrawImgRotated(UIImage image, float rotDegree, CGContext c)
        {
            c.SaveState();

            CGImage imageRef = image.CGImage;

            //loading and setting the image
            MKMapRect theMapRect = ((MapOverlay)this.Overlay).BoundingMapRect;//MKMapRect theMapRect    = [self.overlay boundingMapRect];
            RectangleF theRect = (System.Drawing.RectangleF)RectForMapRect(theMapRect);

            //we need to flip and reposition the image
            c.ScaleCTM(1.0f, -1.0f);
            c.TranslateCTM(-theRect.Width / 8, -theRect.Height);

            // Proper rotation about a point
            var m = CGAffineTransform.MakeTranslation(-theRect.Width / 2, -theRect.Height / 2);
            m.Multiply(CGAffineTransform.MakeRotation((System.nfloat)(rotDegree * DegreesToRadians)));
            m.Multiply(CGAffineTransform.MakeTranslation(theRect.Width / 2, theRect.Height / 2));
            c.ConcatCTM(m);

            c.DrawImage(theRect, imageRef);

            c.RestoreState();
        }
    }

0 个答案:

没有答案