gpt4 book ai didi

swift - Swift 中的 MapKit

转载 作者:行者123 更新时间:2023-11-30 10:22:54 25 4
gpt4 key购买 nike

我正在尝试在 Swift 中使用 Map Kit。我尝试在 map 上显示该区域、一根图钉和当前位置。全部显示正常,排除当前位置 - 它不显示。

这是示例代码:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var mainMapView: MKMapView!

var locationManager = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()

locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()

var userLocation = MKUserLocation()
println("Location: \(userLocation.location)")

var objectLatitude = 53.204526
var objectLongitude = 50.111751

var currentLatitude = 53.203715
var currentLongitude = 50.160374

var latDelta = 0.01
var longDelta = 0.01

var currentLocationSpan: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
var currentLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(currentLatitude, currentLongitude)
var currentRegion: MKCoordinateRegion = MKCoordinateRegionMake(currentLocation, currentLocationSpan)
self.mainMapView.setRegion(currentRegion, animated: true)

var objectLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(objectLatitude, objectLongitude)
var objectAnnotation = MKPointAnnotation()
objectAnnotation.coordinate = objectLocation
objectAnnotation.title = "St. George's Church"
objectAnnotation.subtitle = "Church of the Great Martyr St. George"
self.mainMapView.addAnnotation(objectAnnotation)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}

println("位置:\(userLocation.location)") 显示 位置:nil

采取所有必要措施确保地理定位​​:

enter image description here enter image description here enter image description here

最佳答案

已解决。只需添加以下内容:

locationManager.startUpdatingLocation()

几秒钟后,模拟器中一切正常。

关于swift - Swift 中的 MapKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25200530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com