あなたがドライバの場合は、Geolocationを使用して現在地を検出できます。
threadから、CLLocationManager
を使用して位置を取得する必要があります。
まず、プロジェクトにCoreLocation.framework
を追加します。
- ゴー
Project Navigator
- にタブに、プロジェクト
- クリックして選択し
Build Phases
Link Binary with Libraries
で
CoreLocation.framework
を追加します。
次に、Apple documentationの基本例に従うだけです。
おそらくあなたViewDidLoad
でCLLocationManager
を作成します:CLLocationManagerDelegate
位置は が更新されるたびに付き
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
//Configure Accuracy depending on your needs, default is kCLLocationAccuracyBest
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
// Set a movement threshold for new events.
locationManager.distanceFilter = 500; // meters
[locationManager startUpdatingLocation];
、あなたはあなたのGoogle Maps
上のユーザ位置を更新することができます。
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
// If it's a relatively recent event, turn off updates to save power.
CLLocation* location = [locations lastObject];
NSDate* eventDate = location.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15.0) {
// Update your marker on your map using location.coordinate.latitude
//and location.coordinate.longitude);
}
}
ネイティブMapKit.framework
を使用している場合でも動作します。 yourMapView.myLocationEnabled = YES;
を追加する必要があり、フレームワークがすべてを行います。 (あなたの地図の中心を除いて)。
この手順は、documentationに従います。あなたの位置に従うように地図を更新したい場合は、フレームワークディレクトリに含まれているGoogleの例MyLocationViewController.m
をコピーすることができます。カメラのプロパティを更新するためにオブザーバをmyLocation
プロパティに追加するだけです。
移動距離の合計を取得するには、related questionにチェックを入れます。あなたは緯度/経度にアドレスをジオコーディングする必要があると言いますが、CLLocation
フレームワークを使用して距離を計算することができます。
住所をジオコードするには、forward geocoding APIを使用します。
// get CLLocation fot both addresses
CLLocation *location = [[CLLocation alloc] initWithLatitude:address.latitude longitude:address.longitude];
// calculate distance between them
CLLocationDistance distance = [firstLocation distanceFromLocation:secondLocation];
また、このrelated linkを確認することができます。
希望すると便利です。