swift - 如何在 Swift 上正确显示我的位置和 map 中的图钉?

标签 swift mapkit cllocation

我正在尝试在 Swift 应用程序中显示我的位置,但这没有显示任何内容, map 全是蓝色的..

我的代码是这个形式的互联网教程:

 var latitude:CLLocationDegrees = location.location.coordinate.latitude
    var longitude:CLLocationDegrees = location.location.coordinate.longitude
    var homeLati: CLLocationDegrees = 40.01540192
    var homeLong: CLLocationDegrees = 20.87901079
    var latDelta:CLLocationDegrees = 0.01
    var longDelta:CLLocationDegrees = 0.01
    var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)

    var myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
    var region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
    self.mapKit.setRegion(region, animated: true)

    self.mapKit.showsUserLocation = true
    ///Red Pin
    var myHomePin = MKPointAnnotation()
    myHomePin.coordinate = myHome
    myHomePin.title = "Home"
    myHomePin.subtitle = "Bogdan's home"
    self.mapKit.addAnnotation(myHomePin)

我已经在 .split. 中导入了相应的配置。这段代码有什么问题?

谢谢!

最佳答案

使用纬度和经度在 map 上显示位置。

 var latDelta:CLLocationDegrees = 0.01

 var longDelta:CLLocationDegrees = 0.01

 var theSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
 var pointLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(your latitude, your longitude)

 var region:MKCoordinateRegion = MKCoordinateRegionMake(pointLocation, theSpan)
  mapView.setRegion(region, animated: true)

  var pinLocation : CLLocationCoordinate2D = CLLocationCoordinate2DMake(your latitude, your longitude)
  var objectAnnotation = MKPointAnnotation()
  objectAnnotation.coordinate = pinLocation
  objectAnnotation.title = your title
  self.mapView.addAnnotation(objectAnnotation)

关于swift - 如何在 Swift 上正确显示我的位置和 map 中的图钉?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24780325/

相关文章:

ios - UITableViewController.UIView.trailingAnchor 的位置与 UIViewController.UIView.trailingAnchor 不同

ios - Swift 2.1 Core Data - 保存关系创建一个空记录

ios - MKRouteStep step.instruction 未本地化

ios - WatchOS 2 : CLLocationManager does not have "speed" property

iOS:未调用 CLLocationManager 的 didEnterRegion

SwiftCharts 如何隐藏 yLabel 上的 midValue

ios - 如何使用 SwiftUI 为一系列图像制作动画?

ios - Swift - subview 上的按钮,在父 View 中显示方向

iphone - 使用 MKMapView 显示当前用户位置?

ios - 导航到苹果 map 上的当前位置