ピンの色を個別に変更したいのですが、現在地アイコンまでピンになってしまいます。
XcodeでMapKitを使ってアプリ開発をしています。
ピンの色を個別に変更したくて、こちらの記事を参考にしたのですが、現在地のアイコンがピンのようになってしまっています。
わかる方教えていただけると嬉しいです。
https://hajihaji-lemon.com/swift/mapview_pincolor/
ViewController.swift
import UIKit
import CoreLocation
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
var myLock = NSLock()
let goldenRatio = 1.618
@IBOutlet var mapView: MKMapView!
var locationManager: CLLocationManager!
@IBAction func clickZoomin(_ sender: Any) {
print("[DBG]clickZoomin")
myLock.lock()
if (0.005 < mapView.region.span.latitudeDelta / goldenRatio) {
print("[DBG]latitudeDelta-1 : " + mapView.region.span.latitudeDelta.description)
var regionSpan:MKCoordinateSpan = MKCoordinateSpan()
regionSpan.latitudeDelta = mapView.region.span.latitudeDelta / goldenRatio
mapView.region.span = regionSpan
print("[DBG]latitudeDelta-2 : " + mapView.region.span.latitudeDelta.description)
}
myLock.unlock()
}
@IBAction func clickZoomout(_ sender: Any) {
print("[DBG]clickZoomout")
myLock.lock()
if (mapView.region.span.latitudeDelta * goldenRatio < 150.0) {
print("[DBG]latitudeDelta-1 : " + mapView.region.span.latitudeDelta.description)
var regionSpan:MKCoordinateSpan = MKCoordinateSpan()
regionSpan.latitudeDelta = mapView.region.span.latitudeDelta * goldenRatio
// regionSpan.latitudeDelta = mapView.region.span.longitudeDelta * GoldenRatio
mapView.region.span = regionSpan
print("[DBG]latitudeDelta-2 : " + mapView.region.span.latitudeDelta.description)
}
myLock.unlock()
}
override func viewDidLoad() {
super.viewDidLoad()
let annotation = TestMKPointAnnotation()
let annotation2 = TestMKPointAnnotation()
let annotation3 = TestMKPointAnnotation()
let annotation4 = TestMKPointAnnotation()
let annotation5 = TestMKPointAnnotation()
let annotation6 = TestMKPointAnnotation()
let annotation7 = TestMKPointAnnotation()
let annotation8 = TestMKPointAnnotation()
let annotation9 = TestMKPointAnnotation()
annotation.title = "山崎駅";
annotation.subtitle = "JR西日本";
annotation.coordinate = CLLocationCoordinate2DMake(34.892323, 135.6793405);
annotation2.title = "河内堅上駅";
annotation2.subtitle = "JR西日本";
annotation2.coordinate = CLLocationCoordinate2DMake(34.5742328, 135.6632028);
annotation3.title = "京都鉄道博物館";
annotation3.subtitle = "JR西日本";
annotation3.coordinate = CLLocationCoordinate2DMake(34.9869056, 135.742733);
annotation4.title = "サントリーカーブ";
annotation4.subtitle = "JR西日本";
annotation4.coordinate = CLLocationCoordinate2DMake(34.891324, 135.674206);
annotation5.title = "島本駅";
annotation5.subtitle = "JR西日本";
annotation5.coordinate = CLLocationCoordinate2DMake(34.8806111, 135.6627284);
annotation6.title = "東淀川駅";
annotation6.subtitle = "JR西日本";
annotation6.coordinate = CLLocationCoordinate2DMake(34.7392934, 135.5040161);
annotation7.title = "丑寅第一踏切";
annotation7.subtitle = "JR西日本";
annotation7.coordinate = CLLocationCoordinate2DMake(34.797566, 135.554972);
mapView.addAnnotation(annotation)
mapView.addAnnotation(annotation2)
mapView.addAnnotation(annotation3)
mapView.addAnnotation(annotation4)
mapView.addAnnotation(annotation5)
mapView.addAnnotation(annotation6)
mapView.addAnnotation(annotation7)
mapView.addAnnotation(annotation8)
mapView.addAnnotation(annotation9)
mapView.delegate = self
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations:[CLLocation]) {
let longitude = (locations.last?.coordinate.longitude.description)!
let latitude = (locations.last?.coordinate.latitude.description)!
print("[DBG]longitude : " + longitude)
print("[DBG]latitude : " + latitude)
myLock.lock()
myLock.unlock()
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let testPinView = MKMarkerAnnotationView()
testPinView.annotation = annotation
if let test = annotation as? TestMKPointAnnotation {
testPinView.markerTintColor = test.markerColor
}
testPinView.canShowCallout = true
return testPinView
}
@IBAction func NowLocation(_ sender: Any) {
locationManager = CLLocationManager()
locationManager.startUpdatingLocation()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
mapView.showsUserLocation = true
}
}
TestMKPointAnnotation.swift
import UIKit
import MapKit
class TestMKPointAnnotation: MKPointAnnotation {
var markerColor:UIColor = UIColor.tintColor
}
標準の現在地アイコンに戻して、ピンの色も変えたいです。