2017-02-14 48 views
0

我是编程新手。 我发现了下面的代码,它除了中心位置以外都做了一切(放大地图,蓝色点全部工作)。 如果我在模拟器(城市运行)中运行,蓝色点就会从页面运行。如何使“地图上的中心位置”工作?

import UIKit 
import MapKit 
import CoreLocation 
import Foundation 

class ViewController: UIViewController, CLLocationManagerDelegate { 
    @IBOutlet weak var map: MKMapView! 

    var locationManager: CLLocationManager? 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     locationManager = CLLocationManager() 
     locationManager!.delegate = self 

     map.showsUserLocation = true 

     if CLLocationManager.authorizationStatus() == .authorizedWhenInUse { 
      locationManager!.startUpdatingLocation() 
     } else { 
      locationManager!.requestWhenInUseAuthorization() 
     } 
    } 

    private func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 
     switch status { 
     case .notDetermined: 
      print("NotDetermined") 
     case .restricted: 
      print("Restricted") 
     case .denied: 
      print("Denied") 
     case .authorizedAlways: 
      print("AuthorizedAlways") 
     case .authorizedWhenInUse: 
      print("AuthorizedWhenInUse") 
      locationManager!.startUpdatingLocation() 
     } 
    } 

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     let location = locations.first! 
     let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 500, 500) 
     map.setRegion(coordinateRegion, animated: true) 
     locationManager?.stopUpdatingLocation() 
     locationManager = nil 
    } 
} 
+0

为什么不在地图视图中使用'userTrackingMode'属性? – xoudini

+0

谢谢Xoudini我是编程新手,对userTrackingMode一无所知,所以我不知道如何使用它。 – Doug

+0

当位置服务处于打开状态时,'yourMapView.userTrackingMode = .follow'自动使用户居中,请参阅[documentation](https://developer.apple.com/reference/mapkit/mkmapview/1616208-usertrackingmode)。 – xoudini

回答

0

删除您

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

刚刚获得授权的实施,设置map.showsUserLocation = true和退后。

0

这是我工作并测试的Swift 3代码。它加载mapView,询问用户的权限并放大他的位置。

import UIKit 
import MapKit 

class ViewController: UIViewController, CLLocationManagerDelegate { 
@IBOutlet weak var mapView: MKMapView! 

let locationManager = CLLocationManager() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters 
    locationManager.activityType = .automotiveNavigation 
    locationManager.distanceFilter = 10.0 
    mapView.showsUserLocation = true 
    mapView.mapType = .standard 
    self.mapView.delegate = self 
} 

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 
    locationManager.requestWhenInUseAuthorization() 
    locationManager.startUpdatingLocation() 
} 

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    let userLocation:CLLocation = locations[0] as CLLocation 
    locationManager.stopUpdatingLocation() 
    let location = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude) 
    let span = MKCoordinateSpanMake(0.05, 0.05) 
    let region = MKCoordinateRegion (center: location,span: span)  
    mapView.setRegion(region, animated: true) 
} 
} 
+0

谢谢Mohsen我得到一个错误 – Doug

+0

谢谢Mohsen当我复制并粘贴你的代码时出现错误无法为类型“MKMapViewDelegate?”指定“ViewController”类型的值。 – Doug

+0

将MKMapViewDelegate作为协议添加到您的viewController中,或者从您的代码中删除mapView.delegate = self,并且您应该很好地去,让我知道发生了什么 –

相关问题