画面上で見えてる範囲だけ距離を取得しAPIに投げたい。のが動機です。
まずはiPhone上に地図を表示しなくては始まりませんね。
xcodeで新規プロジェクト、シングルページで新規アプリを作ります。
下記コードをコピペ。とりあえず地図が表示されます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// MapKit001 | |
// | |
import UIKit | |
import MapKit | |
class ViewController: UIViewController, MKMapViewDelegate { | |
// MapView. | |
var myMapView : MKMapView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// MapViewの生成. | |
myMapView = MKMapView() | |
// MapViewのサイズを画面全体に. | |
myMapView.frame = self.view.bounds | |
// Delegateを設定. | |
myMapView.delegate = self | |
// MapViewをViewに追加. | |
self.view.addSubview(myMapView) | |
// 中心点の緯度経度. | |
let myLat: CLLocationDegrees = 37.506804 | |
let myLon: CLLocationDegrees = 139.930531 | |
let myCoordinate: CLLocationCoordinate2D = CLLocationCoordinate2DMake(myLat, myLon) | |
// 縮尺. | |
let myLatDist : CLLocationDistance = 100 | |
let myLonDist : CLLocationDistance = 100 | |
// Regionを作成. | |
let myRegion: MKCoordinateRegion = MKCoordinateRegionMakeWithDistance(myCoordinate, myLatDist, myLonDist); | |
// MapViewに反映. | |
myMapView.setRegion(myRegion, animated: true) | |
} | |
// Regionが変更された時に呼び出されるメソッド. | |
func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool) { | |
println("regionDidChangeAnimated") | |
} | |
} |
下記のファンクションを使用します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/ Regionが変更された時に呼び出されるメソッド. | |
func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool) { | |
println("regionDidChangeAnimated") | |
} |
このファンクションを利用し、地図に変化があった場合、その都度距離を取得します。
下記内容に変更します。
iPhoneの縦の距離を表示します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func mapView(mapView: MKMapView!, | |
regionDidChangeAnimated animated: Bool) { | |
println("regionDidChangeAnimated") | |
var mRect = mapView.visibleMapRect | |
var topMapPoint = MKMapPointMake(MKMapRectGetMidX(mRect), MKMapRectGetMinY(mRect)) | |
var bottomMapPoint = MKMapPointMake(MKMapRectGetMidX(mRect), MKMapRectGetMaxY(mRect)) | |
var currentDist = MKMetersBetweenMapPoints(topMapPoint, bottomMapPoint) | |
println("\(currentDist)m") | |
} |
コンソールに距離が表示されます。
もし横方向の距離を取得したい場合は下記に変更します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func mapView(mapView: MKMapView!, | |
regionDidChangeAnimated animated: Bool) { | |
println("regionDidChangeAnimated") | |
var mRect = mapView.visibleMapRect | |
var leftMapPoint = MKMapPointMake(MKMapRectGetMinX(mRect), MKMapRectGetMidY(mRect)) | |
var rightMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), MKMapRectGetMidY(mRect)) | |
var currentDist = MKMetersBetweenMapPoints(leftMapPoint, rightMapPoint) | |
println("\(currentDist)m") | |
} |
受け取った距離を使ってAPIに投げれば画面上の情報の取得が可能です。
0 コメント:
コメントを投稿