Swift Mapkit示例项目

时间:2014-08-23 09:08:18

标签: ios swift mapkit

我是iOS环境的新手。我创建了一个用于学习的地图套件应用程序。我添加了委托给viewController并且也做了参考插座但模拟屏幕仍然空白这里是我的代码有什么问题?

import Foundation
import MapKit

class Places{
var latitute : CLLocationDegrees
var longtitut : CLLocationDegrees
var placeLoc : CLLocationCoordinate2D?
var theAnnotation : MKPointAnnotation?

init (latit : CLLocationDegrees, long :
    CLLocationDegrees, mytitle : String ,mysubTitle : String){
    self.latitute = latit
    self.longtitut = long
    setLocation()
    setAnnotations(mytitle, subtitle: mysubTitle)
}


func setLocation ( ) {

    placeLoc = CLLocationCoordinate2DMake(self.latitute, self.longtitut)
}

func  setAnnotations (title : String , subtitle : String ) {

    theAnnotation = MKPointAnnotation()
    theAnnotation?.coordinate = placeLoc!
    theAnnotation?.title = title
    theAnnotation?.subtitle = subtitle


}}

的ViewController

import UIKit
import MapKit

class ViewController: UIViewController,MKMapViewDelegate {



@IBOutlet weak var myMapView: MKMapView!

override func viewDidLoad() {
    super.viewDidLoad()

    var latDelta : CLLocationDegrees
    var longDelta : CLLocationDegrees

    longDelta = 0.01
    latDelta = 0.01

    var theSpan : MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)

    var place1 : Places = Places(latit: 45.995079, long: 54.121006, mytitle: "MyHome", mysubTitle: "Home Sweet Home")
    var theRegion : MKCoordinateRegion = MKCoordinateRegionMake(place1.placeLoc!, theSpan)

    self.myMapView.setRegion(theRegion, animated: true)
    self.myMapView.addAnnotation(place1.theAnnotation)


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}}

enter image description here

2 个答案:

答案 0 :(得分:1)

关闭约束以开始并重新运行应用程序,您应该看到地图出现。然后使用设计区域中的Pin按钮设置约束。如果要将其填充视图集,则将每个值设置为0并启用每个值旁边的I栏。

请记住,Xcode 6处于测试版状态,可能有点不对劲。此外,您可能想要确认其约束,请转到Debug>查看调试>捕获视图层次结构并打开线框以查看视图的最终位置。

最后提示,打开助理编辑器并更改它以预览故事板,以便您可以看到约束如何影响地图视图。

答案 1 :(得分:0)

在vieDidLoad方法中使用以下代码,无需创建另一个swift文件。它将显示带有地图视图的注释。也许它会对你有所帮助

     var latDelta : CLLocationDegrees
     var longDelta : CLLocationDegrees

     latDelta = 0.01
    longDelta = 0.01

    let theSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta , longDelta)
    let location:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 13.068452, longitude: 80.25812)
    let theRegion:MKCoordinateRegion = MKCoordinateRegionMake(location, theSpan)
    mapview.setRegion(theRegion, animated: true)

    var anotation = MKPointAnnotation()
    anotation.coordinate = location
    mapview.addAnnotation(anotation)