Apple MapKit 사용하기
2021. 4. 16. 03:02ㆍ아이폰 개발
iOS앱에서 애플맵을 사용하여 개발할때 참고할 만한 레퍼런스:
www.raywenderlich.com/7738344-mapkit-tutorial-getting-started#toc-anchor-004
medium.com/swlh/add-a-map-to-your-app-with-mapkit-and-core-location-ios-swift-guide-8ea9c236d895
위의 첫번째 튜토리얼에서 geojson 파일을 Bundle에서 찾지 못할때 조치 방법
The issue is that the file isn't being copied to your app bundle. To fix it:
Click your project
Click your target
Select Build Phases
Expand Copy Bundle Resources
Click '+' and select your file.
stackoverflow.com/questions/41775563/bundle-main-pathforresourceoftypeindirectory-returns-nil
MapKit 상에서 long press를 이용하여 마커 표시하기
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
let longTapGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap))
mapView.addGestureRecognizer(longTapGesture)
}
@objc func longTap(sender: UIGestureRecognizer){
print("long tap")
if sender.state == .began {
let locationInView = sender.location(in: mapView)
let locationOnMap = mapView.convert(locationInView, toCoordinateFrom: mapView)
addAnnotation(location: locationOnMap)
}
}
func addAnnotation(location: CLLocationCoordinate2D){
let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = "Some Title"
annotation.subtitle = "Some Subtitle"
self.mapView.addAnnotation(annotation)
}
}
extension ViewController: MKMapViewDelegate{
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard annotation is MKPointAnnotation else { print("no mkpointannotaions"); return nil }
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.rightCalloutAccessoryView = UIButton(type: .infoDark)
pinView!.pinTintColor = UIColor.black
}
else {
pinView!.annotation = annotation
}
return pinView
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
print("tapped on pin ")
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if control == view.rightCalloutAccessoryView {
if let doSomething = view.annotation?.title! {
print("do something")
}
}
}
}
medium.com/@calmone/ios-mapkit-in-swift-4-drop-the-pin-at-the-point-of-long-press-2bed878fdf93
swift-it-world.tistory.com/24?category=419192