【问题标题】:unable to get user location on map in swift xcode 7.1无法在 swift xcode 7.1 中的地图上获取用户位置
【发布时间】:2016-03-25 10:00:55
【问题描述】:

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

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 文件中。 任何帮助。谢谢你

【问题讨论】:

    标签: swift xcode7.1


    【解决方案1】:

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

    按 Option/Alt 键 --> 你会看到两轮。 捏合/缩小以放大/缩小

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多