2016-10-30 5 views
9

MapKitの使い方を理解しようとする新しいコーダー。目標は、ユーザーがアドレスを使用してピンを追加できるマップを作成することです。しかし、私は現在の段階ですが、ピンをマップに追加する方法を理解することができません。MapKit - Swift 3.0を使用してマップにピンを追加する

ピンをマップに追加するにはどうすればよいですか?私は今までの注釈の使い方を理解するのに苦労してきました。

これは私が助けと方向性を望んでいることです。ありがとう!

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) 
    } 
} 

答えて

29

彼らがMapKitでannotationsと呼ばれ、あなたがそうのようにそれらをインスタンス化している:ちょうど座標を設定viewDidLoad()方法で、その後

let annotation = MKPointAnnotation()

と同様にマップに追加します:

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

数字は座標です

+0

ミスラフ、助けてくれてありがとう!これはまさに私が必要としていたものです。 – roguephillips

+6

私はurコードを使用して注釈を追加しましたが、注釈は地図に表示されません??? –

+0

これのために表示されません。https://stackoverflow.com/questions/39469529/swift-adding-annotations-on-map – Cesare

関連する問題