2017-02-14 45 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,並且您應該很好地去,讓我知道發生了什麼 –

相關問題