★ MapViewにオーバーレイで円を表示させる
// 中心座標と半径を設定
let coordinate = CLLocationCoordinate2DMake(location.latitude, location.longitude)
let circle = MKCircle(centerCoordinate: coordinate, radius: 200)
mapView.addOverlay(circle)
// MKMapViewDelegate
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
let circleRenderer = MKCircleRenderer(overlay: overlay)
circleRenderer.strokeColor = .redColor()
circleRenderer.fillColor = .yellowColor()
circleRenderer.lineWidth = 1.0
circleRenderer.alpha = 0.2
return circleRenderer
}
★ 現在地にマップ表示を移動
@IBAction func currentLocation(sender: AnyObject) {
let span = MKCoordinateSpanMake(0.01, 0.01)
let region = MKCoordinateRegionMake(mapView.userLocation.coordinate, span)
mapView.setRegion(region, animated: true)
}
★ Pinを落とす Simple
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
// MapView
@IBOutlet weak var myMapView: MKMapView!
// Pinをインスタンス変数として定義
let an = MKPointAnnotation()
// 経度緯度.
let myLan: CLLocationDegrees = 37.331741
let myLon: CLLocationDegrees = -122.030333
var center: CLLocationCoordinate2D!
override func viewDidLoad() {
super.viewDidLoad()
// 地図の中心の座標.
center = CLLocationCoordinate2DMake(myLan, myLon)
myMapView.centerCoordinate = center
// 縮尺を指定.
let mySpan: MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
let myRegion: MKCoordinateRegion = MKCoordinateRegion(center: center, span: mySpan)
// regionをmapViewに追加.
myMapView.region = myRegion
// Pinを作成
an.title = "title"
an.coordinate = center
myMapView.addAnnotation(an)
}
}