LoginSignup
4
3

More than 5 years have passed since last update.

CLLocationの2地点間の中間地点の座標を求める

Last updated at Posted at 2015-01-31

Swiftでアプリを作り始めて一ヶ月ほど経ちました。

CoreLocationを使っていて、CLLocationの2地点の中間地点の座標を求めるのにハマったのでメモ。

最初、2地点の中間座標を下記のように平均値を使って求めようとしましたが、エラーが出てうまくいきませんでした。
(西経と東経をまたがる計算がうまくいかないのは承知の上です)

location.swift
pointA = CLLocation()
pointB = CLLocation()

// いろいろな処理で座標を取り込んで・・・

centerLatitude: CLLocationDegrees = (pointA.coordinate.latitude + pointB.coordinate.latitude) / 2.0
centerLongitude: CLLocationDegrees = (pointA.coordinate.longitude + pointB.coordinate.longitude) / 2.0

center = CLLocationCoordinate2DMake(centerLatitude, centerLongitude)

CLLocation()でもうまくいかない・・・

2時間ほど悩んで、ようやくたどり着きました。
勘違いしてました。CLLocationのインスタンス生成時に引数のラベルを忘れていたという・・・

location.swift
pointA = CLLocation()
pointB = CLLocation()

// いろいろな処理で座標を取り込んで・・・

centerLatitude: CLLocationDegrees = (pointA.coordinate.latitude + pointB.coordinate.latitude) / 2.0
centerLongitude: CLLocationDegrees = (pointA.coordinate.longitude + pointB.coordinate.longitude) / 2.0

center = CLLocation(latitude: centerLatitude, longitude: centerLongitude)

西経180度と東経180度をまたがるとうまくいかない気がします。まあ、地上でそんな地点は少ないのでしょうが、他に良い方法あるんでしょうか。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3