无法在swift xcode 7.1中获取用户在地图上的位置

时间:2016-03-25 10:00:56

标签: swift xcode7.1

我尝试在地图上获取用户的准确位置,但我只收到城市名称而不是确切的位置。这是代码。

import UIKit
import MapKit
import CoreLocation

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

let locationManager = CLLocationManager()

override func viewDidLoad()
{
    super.viewDidLoad()

    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
    self.mapView.showsUserLocation = true

}

override func didReceiveMemoryWarning()
{
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


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

    let location = locations.last! as CLLocation

    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))

    self.mapView.setRegion(region, animated: true)
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
    print("Error: " + error.localizedDescription)
}

}

NSLocationWhenInUseUsageDescription添加plist文件。 任何帮助。三江源

1 个答案:

答案 0 :(得分:0)

如果你在模拟器中看这个。然后按照此过程缩放地图。这样你就可以在地图上看到街道了

按Option / Alt键 - >你会看到两轮。 捏进/出进行放大/缩小