2016-10-30 22 views
9

Trình lập trình mới, cố gắng tìm hiểu cách sử dụng MapKit. Mục tiêu là tạo một bản đồ mà người dùng có thể thêm ghim để sử dụng địa chỉ của họ. Tuy nhiên, bước hiện tại, tôi đang gặp khó khăn trong việc tìm hiểu cách thêm chân vào bản đồ.Thêm Ghim vào Bản đồ bằng MapKit - Swift 3.0

Tôi làm cách nào để thêm ghim vào bản đồ? Tôi đã đấu tranh để tìm ra cách sử dụng chú thích cho đến nay.

Đó là những gì tôi hy vọng giúp đỡ/hướng dẫn. Cảm ơn!

import UIKit 
import MapKit 
import CoreLocation 

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate 

{ 
    @IBOutlet weak var bigMap: MKMapView! 

    let locationManager = CLLocationManager() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.locationManager.delegate = self 
     self.locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     self.locationManager.requestWhenInUseAuthorization() 
     self.locationManager.startUpdatingLocation() 
     self.bigMap.showsUserLocation = true 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     let location = locations.last 
     let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude) 
     let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02)) 

     self.bigMap.setRegion(region, animated: true) 
     self.locationManager.stopUpdatingLocation() 

    } 

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { 
     print("Errors " + error.localizedDescription) 
    } 
} 

Trả lời

29

Chúng được gọi là annotations trong MapKit và bạn nhanh chóng chúng như vậy:

let annotation = MKPointAnnotation()

sau đó trong viewDidLoad() phương pháp chỉ cần thiết lập tọa độ và thêm chúng vào bản đồ như:

annotation.coordinate = CLLocationCoordinate2D(latitude: 11.12, longitude: 12.11) 
mapView.addAnnotation(annotation) 

Số là tọa độ của bạn

+0

Mislav, cảm ơn vì sự giúp đỡ! Đây chính xác là những gì tôi cần. – roguephillips

+6

tôi đã thêm chú thích bằng mã ur nhưng chú thích không hiển thị trên bản đồ ??? –

+0

Chúng không hiển thị vì điều này: https://stackoverflow.com/questions/39469529/swift-adding-annotations-on-map – Cesare

Các vấn đề liên quan