2012-08-31 46 views
3

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

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

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

回答

2

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

有其中一些棘手的颠簸我跑进周围的应用程序委托,以及它是如何在C#中的处理方式不同,以客观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)我的mapOverlay类中的bounding mapRect函数覆盖MKOverlay。 是的位置是硬编码的,我正在处理单位转换atm,但这些是绘制图像的正确坐标,与我使用的样本对象c代码相同。

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

    } 

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

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

0

你想要这样做的方式将取决于你想叠加的图像的种类。如果它非常小,则只需使用单个图像即可脱身。但是,如果它覆盖使用期望可以放大的较大区域,则可能必须将其分割为单独的区块才能获得更好的性能。

这里的堆栈溢出可能会为您一一解答了一些其他问题:

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

iPhone - Image overlay MapKit framework?

看到苹果的WWDC2010示例代码tilemap的 https://github.com/klokantech/Apple-WWDC10-TileMap(被人发布到GitHub上)

这与Mono没有任何关系,但您应该可以将其转换为...