MKCircle
MKPolygon
MKPolyline
뒤에 Renderer
가 붙은 클래스는 오버레이를 출력할 때 라인의 두께, 컬러 등을 정하는 객체이다.
CLLocationCoordinate2D
객체를 생성한다.func addOverlay(){
let coordinate1 = CLLocationCoordinate2D(latitude: 37.29197366738964, longitude: 127.20029876492822)
let coordinate2 = CLLocationCoordinate2D(latitude: 37.29341613873919, longitude: 127.2023157861073)
let coordinates = [coordinate1, coordinate2]
let polyline = MKPolyline(coordinates:coordinates, count: coordinates.count)
mapView.addOverlay(polyline)
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
switch overlay{
case is MKPolyline:
let r = MKPolylineRenderer(overlay: overlay)
r.strokeColor = .systemRed //색
r.lineWidth = 10 //라인 두께
return r
default:
return MKOverlayRenderer()
}
}
let circle = MKCircle(center: mapView.centerCoordinate, radius: 100)//radius는 meter단위
mapView.addOverlay(circle)
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
switch overlay{
...
case is MKCircle:
let r = MKCircleRenderer(overlay: overlay)
r.strokeColor = .blue
r.lineWidth = 10
return r
...
@IBAction func addOverlay(_ sender: Any){
mapView.removeOverlays(mapView.overlays)
for obj in geoJsonObjects {
guard let feature = obj as? MKGeoJSONFeature else {continue}
let jsonDecoder = JSONDecoder()
guard let pdata = feature.properties,
let properties = try? jsonDecoder.decode(Property.self, from: pdata) else {continue}
if let section = feature.geometry.first as? MKPolygon {
section.title = properties.name
mapView.addOverlay(section)
}
}
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
switch overlay{
case is MKPolyline:
let r = MKPolylineRenderer(overlay: overlay)
r.strokeColor = .systemRed //색
r.lineWidth = 10 //라인 두께
return r
case is MKCircle:
let r = MKCircleRenderer(overlay: overlay)
r.strokeColor = .blue
r.lineWidth = 10
return r
case is MKPolygon:
let r = MKPolygonRenderer(overlay: overlay)
r.fillColor = .yellow
return r
default:
return MKOverlayRenderer()
}
}
case is MKPolygon:
let r = MKPolygonRenderer(overlay: overlay)
switch overlay.title {
case "유러피안 어드벤처":
r.fillColor = .systemGreen
case "매직랜드":
r.fillColor = .systemRed
case "아메리칸 어드벤처":
r.fillColor = .systemBlue
case "글로벌페어":
r.fillColor = .systemPurple
case "주토피아":
r.fillColor = .systemOrange
default:
r.fillColor = .systemYellow
}
r.alpha = 0.2
return r