gpt4 book ai didi

xcode - MKMapView 不被称为 Swift ios 8

转载 作者:行者123 更新时间:2023-11-30 14:19:06 24 4
gpt4 key购买 nike

嗨,我只是想知道为什么我的 MapView 函数没有被调用。我试图在点之间画一条线,但所有显示的只是中心位置。提前致谢。 导入 UIKit导入核心位置导入MapKit

类 ThirdViewController: UIViewController , CLLocationManagerDelegate {

@IBOutlet weak var theMap: MKMapView!

@IBOutlet weak var theLabel: UILabel!

var manager:CLLocationManager!
var myLocations: [CLLocation] = []

override func viewDidLoad() {
super.viewDidLoad()

//Setup our Location Manager
manager = CLLocationManager()
self.manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestAlwaysAuthorization()
manager.startUpdatingLocation()


//Setup our Map View
// theMap.delegate = self
theMap.mapType = MKMapType.Standard
theMap.showsUserLocation = true
}


func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) {
theLabel.text = "\(locations[0])"
myLocations.append(locations[0] as CLLocation)

let spanX = 0.007
let spanY = 0.007
var newRegion = MKCoordinateRegion(center: theMap.userLocation.coordinate, span: MKCoordinateSpanMake(spanX, spanY))
theMap.setRegion(newRegion, animated: true)
println("not yet ")
if (myLocations.count > 1){
println("here ")
var sourceIndex = myLocations.count - 1
var destinationIndex = myLocations.count - 2
let c1 = myLocations[sourceIndex].coordinate
let c2 = myLocations[destinationIndex].coordinate
var a = [c1, c2]
var polyline = MKPolyline(coordinates: &a, count: a.count)
theMap.addOverlay(polyline)
}
}
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
println("sad")
if overlay is MKPolyline {
println("aadsfsad")
var polylineRenderer = MKPolylineRenderer(overlay: overlay)
polylineRenderer.strokeColor = UIColor.blueColor()
polylineRenderer.lineWidth = 4
return polylineRenderer
}
return nil
}

}

最佳答案

在 ios8+ 中,MapView 没有自动加载位置坐标。如果我们想使用 MKMapView 通过“mapView:didUpdateUserLocation”加载更准确的位置:

前提:
1.设置mapView委托(delegate)。
2.setShowsUserLocation:YES
3.mapView必须在容器中(addSubview:mapView)!!!!!!

例如:

self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
_mapView.delegate = self;
[_mapView setShowsUserLocation:YES];
[[UIApplication sharedApplication].keyWindow addSubview:_mapView];

关于xcode - MKMapView 不被称为 Swift ios 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30703221/

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