我一直在寻找一个好的教程,来添加C# Monotouch中Mapkit的图像叠加。
我已经找到了许多关于彩色圆形或多边形覆盖层的示例。但是我想要加载一个PNG文件到我的地图上方。我之前在MonoAndroid中实现过这个功能,但现在需要将程序转移到iOS平台。
即使有Objective C的示例也会有所帮助,但最好还是有Mono的示例。
我一直在寻找一个好的教程,来添加C# Monotouch中Mapkit的图像叠加。
我已经找到了许多关于彩色圆形或多边形覆盖层的示例。但是我想要加载一个PNG文件到我的地图上方。我之前在MonoAndroid中实现过这个功能,但现在需要将程序转移到iOS平台。
即使有Objective C的示例也会有所帮助,但最好还是有Mono的示例。
我最终下载了一些原生的Objective C代码,然后将其转换为C#。功能名称非常相似,Xamarin API参考文档非常有帮助。
在应用程序委托方面,我遇到了一些棘手的问题,因为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();
}
第二个问题是:在我的mapOverlay类中,覆盖MKOverlay函数的bounding mapRect。是的,位置是硬编码的,我正在进行单位转换,但那些是正确的坐标,可以像我使用的样本Objective C代码一样绘制图像。
public MKMapRect BoundingMapRect
{
[Export("boundingMapRect")]
get
{
var bounds = new MKMapRect(1.35928e+08, 9.23456e+07,17890.57, 26860.05);
return bounds;
}
}
你想要覆盖图片的方式将取决于你想要覆盖的图像类型。如果它只是相当小的,你应该能够只使用单个图像。然而,如果它覆盖了一个较大的区域,并且希望能够对其进行缩放,则可能需要将其拆分为单独的瓦片以获得更好的性能。
以下是一些Stack Overflow的其他问题,可能会为您提供答案:
iPhone - Image overlay MapKit framework?
请参阅Apple的WWDC2010示例代码TileMap https://github.com/klokantech/Apple-WWDC10-TileMap (由某人发布到GitHub)
所有这些都与Mono无关,但您应该能够进行转换...