2016-09-28 58 views
-2

不顯示我試圖顯示標誌物的MapView同時加載,但在地圖中顯示我的countrymap中的MapView但不是註釋。我在做什麼錯事?僅供參考,我的模擬器位置設置爲Apple。MapKit地圖 - 註釋上的模擬器

import MapKit 
import UIKit 

class Capital: NSObject, MKAnnotation { 
    var title: String? 
    var coordinate: CLLocationCoordinate2D 
    var info: String 

    init(title: String, coordinate: CLLocationCoordinate2D, info: String) { 
     self.title = title 
     self.coordinate = coordinate 
     self.info = info 
    } 
} 





import MapKit 
import UIKit 

class ViewController: UIViewController, MKMapViewDelegate { 
    @IBOutlet weak var mapView: MKMapView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let london = Capital(title: "London", coordinate: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), info: "Home to the 2012 Summer Olympics.") 
     let oslo = Capital(title: "Oslo", coordinate: CLLocationCoordinate2D(latitude: 59.95, longitude: 10.75), info: "Founded over a thousand years ago.") 
     let paris = Capital(title: "Paris", coordinate: CLLocationCoordinate2D(latitude: 48.8567, longitude: 2.3508), info: "Often called the City of Light.") 
     let rome = Capital(title: "Rome", coordinate: CLLocationCoordinate2D(latitude: 41.9, longitude: 12.5), info: "Has a whole country inside it.") 
     let washington = Capital(title: "Washington DC", coordinate: CLLocationCoordinate2D(latitude: 38.895111, longitude: -77.036667), info: "Named after George himself.") 

     mapView.addAnnotations([london, oslo, paris, rome, washington]) 
    } 
} 
+0

我已經添加了這個,我試圖代碼的圖像。 –

+0

我們無法調試您的錯誤,直到我們沒有代碼片段。沒有人會從你的圖像編寫代碼。 – iDeveloper

+0

請現在看看它,如果我沒有很好地發佈我的查詢,請不要理睬,這是我第一次在這裏發表問題。 –

回答

0

嘗試這個

import UIKit 
import MapKit 



class MapViewController: UIViewController { 
     @IBOutlet var mapView: MKMapView! 

     let location:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 20.5937, longitude: 78.9629) 


     override func viewDidLoad() { 
      super.viewDidLoad() 



      let anotation = MKPointAnnotation() 
      anotation.coordinate = location 
      anotation.title = "The Location" 
      anotation.subtitle = "This is the location !!!" 
      mapView.addAnnotation(anotation) 

     } 
} 
+0

你的代碼就像魅力一樣。我有一個情況怎麼樣,我會ping一個Web服務,讓我的註釋信息的各種位置,然後我要創建這些註解,並在地圖上顯示出他們,所以我使用了與符合mkannotation一個單獨的類的方法(如你可以在我的查詢中看到),然後我試圖一次添加所有這些,並且不像我期望的那樣工作。我無法使用您的方法,因爲我不會在地圖中只添加一個點註釋。請讓我知道,如果你有任何想法,謝謝。 –

+0

[閱讀本(https://www.raywenderlich.com/90971/introduction-mapkit-swift-tutorial),它會幫助你 – iDeveloper

+0

我也不能標註一個點是我的國家,我曾嘗試只添加了一個離開我國的點,但模擬器只顯示我的國家地圖,沒有註釋。任何解決方案? –