現在地の検索

位置情報を認識するアプリケーションを作成する場合、最も一般的なタスクの 1 つは、マップ上のユーザーの現在の位置を表示することです。 このために、さまざまなソリューションを利用できます。 Apple のCoreLocation を使用して、デバイスに内蔵されている GPS センサーから位置情報を取得する方法があります。

HERE SDK は、商用またはプラットフォーム のポジショニングソリューションでスムーズに動作します。 Navigate Edition の一部として、包括的な HERE Positioning ソリューションが提供されています。 プラットフォーム に依存する可能性のある実装を以下に示します。

デバイスのセンサーにアクセスする前に、ユーザーに許可を求める必要があります。 次の権限をファイルInfo.plist に追加します。

<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs to access your current location to display it on the map while the app is being used.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs to access your current location to display it on the map while the app is being used or when running in background.</string>

次に CLLocationManager 、 iOS プラットフォームからのインスタンスを取得し、位置情報の更新を要求します。

locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()

イベントを受信するには、クラスが CLLocationManagerDelegate プロトコルに準拠している必要があります。

func locationManager(_ manager: CLLocationManager,
                     didUpdateLocations locations: [CLLocation]) {
    guard let lastLocation = locations.last else {
        print("Warning: No last location found")
        return
    }

    delegate?.onLocationUpdated(location: lastLocation)
}

// ...

delegateは、 タイプがPlatformPositioningProviderDelegateです 。 これは、別のクラスが簡単に通知を受け取ることができるように定義したプロトコルです。

CLLocationManager これは、 iOS 位置情報サービスへのアクセスを提供するを統合する方法の一例にすぎません。 自分のニーズに合わせて自由にアレンジできます。

以下に完全なクラスを示します。

import CoreLocation
import Foundation
import UIKit

public protocol PlatformPositioningProviderDelegate {
    func onLocationUpdated(location: CLLocation)
}

// A simple iOS based positioning implementation.
class PlatformPositioningProvider : NSObject,
                                    CLLocationManagerDelegate {

    var delegate: PlatformPositioningProviderDelegate?
    private let locationManager = CLLocationManager()

    func startLocating() {
         if locationManager.delegate == nil {
             locationManager.delegate = self
             locationManager.desiredAccuracy = kCLLocationAccuracyBest
             locationManager.requestAlwaysAuthorization()
         }
    }

    func stopLocating() {
        locationManager.stopUpdatingLocation()
    }

    // Conforms to the CLLocationManagerDelegate protocol.
    func locationManager(_ manager: CLLocationManager,
                         didChangeAuthorization status: CLAuthorizationStatus) {
        switch status {
            case .restricted, .denied, .notDetermined:
                print("Positioning denied by user.")
                break
            case .authorizedWhenInUse, .authorizedAlways:
                print("Positioning authorized by user.")
                locationManager.startUpdatingLocation()
                break
            default:
                break
        }
    }

    // Conforms to the CLLocationManagerDelegate protocol.
    func locationManager(_ manager: CLLocationManager,
                         didFailWithError error: Error) {
       if let error = error as? CLError, error.code == .denied {
          print("Positioning denied by user.")
          manager.stopUpdatingLocation()
       }
    }

    // Conforms to the CLLocationManagerDelegate protocol.
    func locationManager(_ manager: CLLocationManager,
                         didUpdateLocations locations: [CLLocation]) {
        guard let lastLocation = locations.last else {
            print("Warning: No last location found")
            return
        }

        delegate?.onLocationUpdated(location: lastLocation)
    }
}

このクラスを独自のアプリに統合するには、新しいインスタンスを作成し、呼び出し元のクラスをデリゲートとして設定します。

platformPositioningProvider = PlatformPositioningProvider()
platformPositioningProvider.delegate = self

呼び出し元のクラスをデリゲートとして設定すると PlatformPositioningProviderDelegate 、プロトコルに準拠する必要があり、クラスで CLLocation イベントの受信を開始できます。

func onLocationUpdated(location: CLLocation) {
    // ...
}

次のメソッドを呼び出して、検索を開始および停止できます。

platformPositioningProvider.startLocating()

// ...

platformPositioningProvider.stopLocating();

このクラスではイベント CLLocation が受信されることに注意してください。 次のメソッドを使用して、最も一般的なフィールドをカバーするために HERE SDK で使用されるクラスLocation に変換します。

private func convertLocation(nativeLocation: CLLocation) -> Location {
    let geoCoordinates = GeoCoordinates(latitude: nativeLocation.coordinate.latitude,
                                        longitude: nativeLocation.coordinate.longitude,
                                        altitude: nativeLocation.altitude)
    var location = Location(coordinates: geoCoordinates,
                            timestamp: nativeLocation.timestamp)
    location.bearingInDegrees = nativeLocation.course < 0 ? nil : nativeLocation.course
    location.speedInMetersPerSecond = nativeLocation.speed < 0 ? nil : nativeLocation.speed
    location.horizontalAccuracyInMeters = nativeLocation.horizontalAccuracy < 0 ? nil : nativeLocation.horizontalAccuracy
    location.verticalAccuracyInMeters = nativeLocation.verticalAccuracy < 0 ? nil : nativeLocation.verticalAccuracy

    return location
}

CLLocationManagerの詳細、 および他のプラットフォームポジショニング機能の使用方法について は、 iOS の公式ドキュメントを参照してください。

」に一致する結果は 件です

    」に一致する結果はありません