【问题标题】:MonoTouch Mapkit image overlayMonoTouch Mapkit 图像叠加
【发布时间】:2012-08-31 01:07:11
【问题描述】:

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

我发现了许多彩色圆圈或多边形的叠加示例。但我想在我的地图顶部加载一个 PNG。我来自 MonoAndroid 并在那里完成,但需要将我的程序转移到 iOS。

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

【问题讨论】:

    标签: ios xamarin.ios mapkit mkoverlay


    【解决方案1】:

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

    我在应用程序委托中遇到了一些棘手的问题,以及在 C# 和 Objective C 中的处理方式不同。

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

    1) 地图叠加类中的Draw函数

        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) 覆盖 MKOverlay 的 mapOverlay 类中的边界 mapRect 函数。 是的,位置是硬编码的,我正在处理单位转换 atm 但这些是绘制图像的正确坐标,与我使用的示例目标 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/

    【讨论】:

      【解决方案2】:

      您要采用的方式取决于您要叠加的图像类型。如果它非常小,您应该可以只使用单个图像就可以逃脱。但是,如果它覆盖的区域较大,并且希望能够放大,您可能需要将其拆分为单独的图块以获得更好的性能。

      以下是有关 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 无关,但你应该可以转换它……

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多