MonoTouch Mapkit图像叠加

时间:2012-08-31 01:07:11

标签: ios xamarin.ios mapkit mkoverlay

我一直在寻找一个关于在C#Monotouch中为Mapkit添加图像叠加的好教程。

我找到了许多彩色圆圈或多边形的叠加示例。但我想在我的地图顶部加载PNG。我是从MonoAndroid来的,已经在那里完成了,但需要将我的程序转移到iOS。

即使是客观的C示例也会有所帮助,但Mono会更好。

2 个答案:

答案 0 :(得分:2)

我最终下载了一些本地目标C代码,几乎只是将其转换为C#。函数名称非常相似,Xamarin API参考文档非常有用。

我遇到了一些棘手的问题,围绕着app代理,以及如何在C#中对Objective C进行不同的处理。

以下是两个最难转换的功能和我的解决方案:

1)绘图功能在地图覆盖类

    public override void DrawMapRect (MKMapRect mapRect, float zoomScale, CGContext ctx)
    {
        InvokeOnMainThread(
            () => 
            {
            UIImage image = UIImage.FromFile(@"indigo_eiffel_blog.png");

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

    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 = 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(DegreesToRadians(rotDegree)));
        m.Multiply( CGAffineTransform.MakeTranslation(theRect.Width/2,theRect.Height/2));
        c.ConcatCTM( m );

        c.DrawImage(theRect, imageRef);

        c.RestoreState();
    }

和2)我的mapOverlay类中的边界mapRect函数重写MKOverlay。 是的,位置是硬编码的,我正在进行单位转换atm,但这些是绘制图像的正确坐标,与我使用的示例obejctive c代码相同。

    public MKMapRect BoundingMapRect
    {
        [Export("boundingMapRect")]
        get 
        {
            var bounds = new MKMapRect(1.35928e+08, 9.23456e+07,17890.57, 26860.05);
            return bounds; 
        }

    }

我转换的Objective C项目的源代码在这里:https://github.com/indigotech/Blog-MKMapOverlayView

Xamarin API参考文档:http://iosapi.xamarin.com/

答案 1 :(得分:0)

您想要进行此操作的方式取决于您要叠加的图像类型。如果它只是非常小,你应该能够使用单个图像逃脱。但是,如果它覆盖了一个较大的区域,可以放大使用,您可能需要将其拆分为单独的图块以获得更好的性能。

以下是Stack Overflow的其他一些问题,可能会为您解答:

How do I create an image overlay and add to MKMapView?

iPhone - Image overlay MapKit framework?

请参阅Apple的WWDC2010示例代码TileMap https://github.com/klokantech/Apple-WWDC10-TileMap(由某人发布到GitHub)

这些与Mono没有任何关系,但你应该能够转换它......

相关问题