簡體   English   中英

MonoTouch Mapkit圖像疊加

[英]MonoTouch Mapkit image overlay

我一直在尋找一個關於在C#Monotouch中為Mapkit添加圖像疊加的好教程。

我找到了許多彩色圓圈或多邊形的疊加示例。 但我想在我的地圖頂部加載PNG。 我是從MonoAndroid來的,已經在那里完成了,但需要將我的程序轉移到iOS。

即使是一個客觀的C例也會有所幫助,但Mono會更好。

我最終下載了一些原生的客觀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/

您想要進行此操作的方式取決於您要疊加的圖像類型。 如果它只是非常小,你應該能夠使用單個圖像逃脫。 但是,如果它覆蓋了一個較大的區域,使用了期望能夠放大的區域,則可能需要將其拆分為單獨的區域以獲得更好的性能。

以下是Stack Overflow的其他一些問題,可能會為您解答:

如何創建圖像疊加並添加到MKMapView?

iPhone - 圖像疊加MapKit框架?

請參閱Apple的WWDC2010示例代碼TileMap https://github.com/klokantech/Apple-WWDC10-TileMap (由某人發布到GitHub)

這些與Mono沒有任何關系,但你應該能夠轉換它...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM