您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 高德地圖實戰:如何判斷一個點是否落在多邊形範圍內

編輯:iOS 開發綜合

在許多 LBS 應用裡都會遇到這樣的需求:使用者在地圖上點了一個位置,App 需要判斷這個點是否落在預設的電子圍欄、配送區域或展場範圍之內。高德地圖 iOS SDK 提供了現成的幾何判斷函式,整個實作可以拆成四步:準備地圖、畫出多邊形、取得多邊形頂點、做點與多邊形的包含性判斷。

一、宣告屬性並初始化地圖

首先在介面控制器中宣告地圖與多邊形覆蓋物兩個屬性,並遵循地圖的代理協議:

@interface OutDoorViewController () <MAMapViewDelegate>
@property (nonatomic, strong) MAMapView *maMapView;
@property (nonatomic, strong) MAPolygonView *polygonView;
@end

接著建立地圖物件、設定代理並加入檢視層級:

self.maMapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
self.maMapView.delegate = self;
[self.view addSubview:self.maMapView];

二、構造多邊形並覆蓋到地圖上

多邊形的本質是一串經緯度座標點。這裡以四個點構成一個不規則四邊形為例:

// 構造多邊形資料物件
CLLocationCoordinate2D coordinates[4];
coordinates[0].latitude  = 39.810892; coordinates[0].longitude = 116.233413;
coordinates[1].latitude  = 39.816600; coordinates[1].longitude = 116.331842;
coordinates[2].latitude  = 39.762187; coordinates[2].longitude = 116.357932;
coordinates[3].latitude  = 39.733653; coordinates[3].longitude = 116.278255;

MAPolygon *polygon = [MAPolygon polygonWithCoordinates:coordinates count:4];
// 在地圖上加入多邊形物件
[_maMapView addOverlay:polygon];

加入 overlay 之後,還需要透過代理方法提供對應的渲染視圖,否則多邊形不會顯示:

- (MAOverlayView *)mapView:(MAMapView *)mapView viewForOverlay:(id <MAOverlay>)overlay {
    if ([overlay isKindOfClass:[MAPolygon class]]) {
        _polygonView = [[MAPolygonView alloc] initWithPolygon:overlay];
        _polygonView.lineWidth   = 5.f;
        _polygonView.strokeColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.8];
        _polygonView.fillColor   = [UIColor colorWithRed:0.77 green:0.88 blue:0.94 alpha:0.8];
        _polygonView.lineJoin    = kCGLineJoinMiter; // 線段連接樣式
        return _polygonView;
    }
    return nil;
}

三、點擊時判斷座標是否落在範圍內

關鍵在於兩個函式的搭配:MAMapPointForCoordinate 先把經緯度轉換為地圖專用的平面座標 MAMapPoint,再交由 MAPolygonContainsPoint 進行包含性判斷。之所以要先做座標轉換,是因為經緯度本身是球面上的角度值,不能直接拿去做平面幾何運算。

- (void)mapView:(MAMapView *)mapView didSingleTappedAtCoordinate:(CLLocationCoordinate2D)coordinate {
    MAMapPoint point = MAMapPointForCoordinate(coordinate);

    if (MAPolygonContainsPoint(point, _polygonView.polygon.points, 4)) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
                                                                      message:@"在區域內"
                                                               preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alert animated:YES completion:nil];
    } else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
                                                                      message:@"不在區域內"
                                                               preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

四、幾個實務上要注意的細節

第三個參數是頂點數量,必須與建立多邊形時傳入的 count 保持一致,寫錯會直接導致判斷結果錯誤。此外,多邊形的點集建議用一個成員變數保存,而不是每次都從 polygonView 上取,這樣在後續擴充「多區域判斷」時會清爽很多——只要把每個區域的頂點陣列放進容器,遍歷比對即可。

完成以上三步,就能針對「落在範圍內」與「落在範圍外」分別執行不同業務邏輯了,例如前者顯示可配送商品,後者提示超出服務範圍。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved