2016-09-28 172 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

我也不能标注一个点是我的国家,我曾尝试只添加了一个离开我国的点,但模拟器只显示我的国家地图,没有注释。任何解决方案? –