共用方式為


Xamarin.iOS 中的註釋和重疊

我們將在此逐步解說中建置的應用程式如下所示:

MapKit 應用程式的範例

讓我們從建立新的 iOS 空白項目開始,併為其提供相關名稱。 首先,我們將程式代碼新增至檢視控制器以顯示 MapView,然後為 MapDelegate 和自定義註釋建立新的類別。 請遵循下列步驟來進行這項作業:

ViewController

  1. 將下列命名空間新增至 ViewController

    using MapKit;
    using CoreLocation;
    using UIKit
    using CoreGraphics
    
  2. MKMapView 實例變數新增至 類別,以及 MapDelegate 實例。 我們很快就會建立 MapDelegate

    public partial class ViewController : UIViewController
    {
        MKMapView map;
        MapDelegate mapDelegate;
        ...
    
  3. 在控制器的 方法中 LoadView ,新增 MKMapView ,並將其設定為 View 控制器的 屬性:

    public override void LoadView ()
    {
        map = new MKMapView (UIScreen.MainScreen.Bounds);
        View = map;
    }
    

    接下來,我們將新增程序代碼,以在 'ViewDidLoad' 方法中初始化對應。

  4. ViewDidLoad 新增程式代碼以設定地圖類型中,顯示使用者位置並允許縮放和行動瀏覽:

    // change map type, show user location and allow zooming and panning
    map.MapType = MKMapType.Standard;
    map.ShowsUserLocation = true;
    map.ZoomEnabled = true;
    map.ScrollEnabled = true;
    
    
  5. 接下來,新增程式代碼以置中地圖,並將其設定為區域:

    double lat = 30.2652233534254;
    double lon = -97.73815460962083;
    CLLocationCoordinate2D mapCenter = new CLLocationCoordinate2D (lat, lon);
    MKCoordinateRegion mapRegion = MKCoordinateRegion.FromDistance (mapCenter, 100, 100);
    map.CenterCoordinate = mapCenter;
    map.Region = mapRegion;
    
    
  6. 建立 的新實體 MapDelegate ,並將它指派給 DelegateMKMapView。 同樣地,我們很快就會實 MapDelegate 作 :

    mapDelegate = new MapDelegate ();
    map.Delegate = mapDelegate;
    
  7. 從 iOS 8 開始,您應該向使用者要求授權以使用其位置,因此讓我們將此新增至我們的範例。 首先,定義 CLLocationManager 類別層級變數:

    CLLocationManager locationManager = new CLLocationManager();
    
  8. 在方法中 ViewDidLoad ,我們想要檢查執行應用程式的裝置是否使用 iOS 8,如果它是,我們將在應用程式使用時要求授權:

    if (UIDevice.CurrentDevice.CheckSystemVersion(8,0)){
        locationManager.RequestWhenInUseAuthorization ();
    }
    
  9. 最後,我們需要編輯 Info.plist 檔案,以建議使用者要求其位置的原因。 在 Info.plist[來源] 功能表中,新增下列機碼:

    NSLocationWhenInUseUsageDescription

    和字串:

    Maps Walkthrough Docs Sample.

ConferenceAnnotation.cs – 自定義註釋的類別

  1. 我們將針對稱為 ConferenceAnnotation的註釋使用自定義類別。 將下列類別新增至專案:

    using System;
    using CoreLocation;
    using MapKit;
    
    namespace MapsWalkthrough
    {
        public class ConferenceAnnotation : MKAnnotation
        {
            string title;
            CLLocationCoordinate2D coord;
    
            public ConferenceAnnotation (string title,
            CLLocationCoordinate2D coord)
            {
                this.title = title;
                this.coord = coord;
            }
    
            public override string Title {
                get {
                    return title;
                }
            }
    
            public override CLLocationCoordinate2D Coordinate {
                get {
                    return coord;
                }
            }
        }
    }
    

ViewController - 新增註釋和重疊

  1. ConferenceAnnotation有了 就地,我們可以將它新增至地圖。 回到 的 方法ViewControllerViewDidLoad,在地圖的中心座標上新增註釋:

    map.AddAnnotations (new ConferenceAnnotation ("Evolve Conference", mapCenter));
    
  2. 我們也希望酒店重疊。 新增下列程式代碼,以使用所提供的旅館座標來建立 MKPolygon ,並透過呼叫 AddOverlay將它新增至地圖:

    // add an overlay of the hotel
    MKPolygon hotelOverlay = MKPolygon.FromCoordinates(
        new CLLocationCoordinate2D[]{
        new CLLocationCoordinate2D(30.2649977168594, -97.73863627705),
        new CLLocationCoordinate2D(30.2648461170005, -97.7381627734755),
        new CLLocationCoordinate2D(30.2648355402574, -97.7381750192576),
        new CLLocationCoordinate2D(30.2647791309417, -97.7379872505988),
        new CLLocationCoordinate2D(30.2654525150319, -97.7377341711021),
        new CLLocationCoordinate2D(30.2654807195004, -97.7377994819399),
        new CLLocationCoordinate2D(30.2655089239607, -97.7377994819399),
        new CLLocationCoordinate2D(30.2656428950368, -97.738346460207),
        new CLLocationCoordinate2D(30.2650364981811, -97.7385709662122),
        new CLLocationCoordinate2D(30.2650470749025, -97.7386199493406)
    });
    
    map.AddOverlay (hotelOverlay);
    

這會完成中的 ViewDidLoad程序代碼。 現在,我們需要實作 類別 MapDelegate ,以分別處理建立註釋和重迭檢視。

MapDelegate

  1. 建立名為 MapDelegate 的類別,其繼承自 MKMapViewDelegate ,並包含 annotationId 要作為批注重復使用標識碼的變數:

    class MapDelegate : MKMapViewDelegate
    {
        static string annotationId = "ConferenceAnnotation";
        ...
    }
    

    我們在這裡只有一個註釋,因此重複使用程式代碼並非絕對必要,但最好包含它。

  2. 實作 方法,GetViewForAnnotation以使用本逐步解說隨附的conference.png影像,傳回 的檢視ConferenceAnnotation

    public override MKAnnotationView GetViewForAnnotation (MKMapView mapView, NSObject annotation)
    {
        MKAnnotationView annotationView = null;
    
        if (annotation is MKUserLocation)
            return null;
    
        if (annotation is ConferenceAnnotation) {
    
            // show conference annotation
            annotationView = mapView.DequeueReusableAnnotation (annotationId);
    
            if (annotationView == null)
                annotationView = new MKAnnotationView (annotation, annotationId);
    
            annotationView.Image = UIImage.FromFile ("images/conference.png");
            annotationView.CanShowCallout = true;
        }
    
        return annotationView;
    }
    
  3. 當用戶點選註釋時,我們想要顯示顯示奧斯丁市的影像。 將下列變數新增至 MapDelegate 影像的 ,以及要顯示它的檢視:

    UIImageView venueView;
    UIImage venueImage;
    
  4. 接下來,若要在點選批注時顯示影像,請實 DidSelectAnnotation 作 方法,如下所示:

    public override void DidSelectAnnotationView (MKMapView mapView, MKAnnotationView view)
    {
        // show an image view when the conference annotation view is selected
        if (view.Annotation is ConferenceAnnotation) {
    
            venueView = new UIImageView ();
            venueView.ContentMode = UIViewContentMode.ScaleAspectFit;
            venueImage = UIImage.FromFile ("image/venue.png");
            venueView.Image = venueImage;
            view.AddSubview (venueView);
    
            UIView.Animate (0.4, () => {
            venueView.Frame = new CGRect (-75, -75, 200, 200); });
        }
    }
    
  5. 若要在用戶點選地圖上的任何其他位置來取消選取註釋時隱藏影像,請實 DidDeselectAnnotationView 作 方法,如下所示:

    public override void DidDeselectAnnotationView (MKMapView mapView, MKAnnotationView view)
    {
        // remove the image view when the conference annotation is deselected
        if (view.Annotation is ConferenceAnnotation) {
    
            venueView.RemoveFromSuperview ();
            venueView.Dispose ();
            venueView = null;
        }
    }
    

    我們現在已有註釋的程序代碼。 剩下的就是將程式代碼新增至 , MapDelegate 以建立旅館重疊的檢視。

  6. 將 下列 實作 GetViewForOverlay 新增到 MapDelegate

    public override MKOverlayView GetViewForOverlay (MKMapView mapView, NSObject overlay)
    {
        // return a view for the polygon
        MKPolygon polygon = overlay as MKPolygon;
        MKPolygonView polygonView = new MKPolygonView (polygon);
        polygonView.FillColor = UIColor.Blue;
        polygonView.StrokeColor = UIColor.Red;
        return polygonView;
    }
    

執行應用程式。 我們現在有具有自定義註釋和重疊的互動式地圖! 點選註釋並顯示奧斯丁的影像,如下所示:

點選註釋並顯示奧斯丁的影像

摘要

在本文中,我們探討如何將註釋新增至地圖,以及如何新增指定多邊形的重疊。 我們也示範如何將觸控支援新增至註釋,以在地圖上建立影像的動畫效果。