iOS 模拟器中未显示当前位置
Current Location not showing in iOS Simulator
我正在尝试在 MKMapView 中显示用户的当前位置,mapView
。我请求许可,我相信我所有的基地都被覆盖了,但由于某种原因,当前位置没有显示在地图上。
下面是代码。不要介意分段控件,我只是用它们来更改地图类型。
import UIKit
import MapKit
let annotationId = "annotationID";
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var segControls: UISegmentedControl!
let locationManager = CLLocationManager();
var zoomToUserLocation = false;
// Switch statements for the seg controls.
@IBAction func mapType(sender: UISegmentedControl) {
switch segControls.selectedSegmentIndex {
case 0:
mapView.mapType = MKMapType.Standard
case 1:
mapView.mapType = MKMapType.Satellite
case 2:
mapView.mapType = MKMapType.Hybrid
default:
mapView.mapType = MKMapType.Standard
}
}
override func viewDidLoad() {
super.viewDidLoad()
// We are setting the delegate equal to self here to be able to use the indentifier that deques the annotations.
locationManager.delegate = self
// Status of user is variable that speaks directly to (in this case) authorization status
let status = CLLocationManager.authorizationStatus()
if status == CLAuthorizationStatus.NotDetermined {
locationManager.requestWhenInUseAuthorization()
} else if status != .Denied {
locationManager.startUpdatingLocation()
}
// let annotation = MKPointAnnotation();
}
// If the status changes to authorize when in use or always authorize
// then start updating the location, if not don't do anything.
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == CLAuthorizationStatus.AuthorizedWhenInUse || status == CLAuthorizationStatus.AuthorizedAlways {
locationManager.startUpdatingLocation()
}
}
// If the location failed when trying to get users location execute this function
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println("Error: \(error)")
}
}
extension ViewController: MKMapViewDelegate {
func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!) {
let coordinate = userLocation.coordinate; // this sets the var coordinates to the location of the user.
println("User Location = (\(coordinate.latitude), \(coordinate.longitude))");
if zoomToUserLocation == true {
let locationOfDevice: CLLocation = userLocation.location // this determines the location of the device using the users location
let deviceCoordinate: CLLocationCoordinate2D = locationOfDevice.coordinate // determines the coordinates of the device using the location device variabel which has in it the user location.
let span = MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1) // this determines the span in which its determined that 1 degree is 69 miles.
let region = MKCoordinateRegion(center: deviceCoordinate, span: span) // set the center equal to the device coordinates and the span equal to the span variable we have created this will give you the region.
mapView.setRegion(region, animated: true);
}
}
}
有时您需要先设置一个自定义位置才能让模拟器更新并实际设置一个位置(不知道为什么)。我所做的是去 Debug->Location->Apple
只是为了检查地图位置的东西是否真的有效。然后我设置了一个自定义位置,坐标是我从 google 地图或类似地图中找到的。
您需要将 MKMapView 的 showsUserLocation
属性 设置为 true
。这是一个布尔值 属性,将显示当前位置(您指的那个脉冲蓝点 - 它可以着色,并不总是蓝色)。
以下是 Apple 在 documentation
中的总结
This property does not indicate whether the user’s position is actually visible on the map, only whether the map view should try to display it. Setting this property to true
causes the map view to use the Core Location framework to find the current location and try to display it on the map. As long as this property is true
, the map view continues to track the user’s location and update it periodically. The default value of this property is false
.
Showing the user’s location does not guarantee that the location is visible on the map. The user might have scrolled the map to a different point, causing the current location to be offscreen. To determine whether the user’s current location is currently displayed on the map, use the userLocationVisible
property.
所以在你的 viewDidLoad
函数中,你应该设置这个 属性:
mapView.showsUserLocation = true
zoomToUserLocation
属性不控制当前位置点
我正在尝试在 MKMapView 中显示用户的当前位置,mapView
。我请求许可,我相信我所有的基地都被覆盖了,但由于某种原因,当前位置没有显示在地图上。
下面是代码。不要介意分段控件,我只是用它们来更改地图类型。
import UIKit
import MapKit
let annotationId = "annotationID";
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var segControls: UISegmentedControl!
let locationManager = CLLocationManager();
var zoomToUserLocation = false;
// Switch statements for the seg controls.
@IBAction func mapType(sender: UISegmentedControl) {
switch segControls.selectedSegmentIndex {
case 0:
mapView.mapType = MKMapType.Standard
case 1:
mapView.mapType = MKMapType.Satellite
case 2:
mapView.mapType = MKMapType.Hybrid
default:
mapView.mapType = MKMapType.Standard
}
}
override func viewDidLoad() {
super.viewDidLoad()
// We are setting the delegate equal to self here to be able to use the indentifier that deques the annotations.
locationManager.delegate = self
// Status of user is variable that speaks directly to (in this case) authorization status
let status = CLLocationManager.authorizationStatus()
if status == CLAuthorizationStatus.NotDetermined {
locationManager.requestWhenInUseAuthorization()
} else if status != .Denied {
locationManager.startUpdatingLocation()
}
// let annotation = MKPointAnnotation();
}
// If the status changes to authorize when in use or always authorize
// then start updating the location, if not don't do anything.
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == CLAuthorizationStatus.AuthorizedWhenInUse || status == CLAuthorizationStatus.AuthorizedAlways {
locationManager.startUpdatingLocation()
}
}
// If the location failed when trying to get users location execute this function
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println("Error: \(error)")
}
}
extension ViewController: MKMapViewDelegate {
func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!) {
let coordinate = userLocation.coordinate; // this sets the var coordinates to the location of the user.
println("User Location = (\(coordinate.latitude), \(coordinate.longitude))");
if zoomToUserLocation == true {
let locationOfDevice: CLLocation = userLocation.location // this determines the location of the device using the users location
let deviceCoordinate: CLLocationCoordinate2D = locationOfDevice.coordinate // determines the coordinates of the device using the location device variabel which has in it the user location.
let span = MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1) // this determines the span in which its determined that 1 degree is 69 miles.
let region = MKCoordinateRegion(center: deviceCoordinate, span: span) // set the center equal to the device coordinates and the span equal to the span variable we have created this will give you the region.
mapView.setRegion(region, animated: true);
}
}
}
有时您需要先设置一个自定义位置才能让模拟器更新并实际设置一个位置(不知道为什么)。我所做的是去 Debug->Location->Apple
只是为了检查地图位置的东西是否真的有效。然后我设置了一个自定义位置,坐标是我从 google 地图或类似地图中找到的。
您需要将 MKMapView 的 showsUserLocation
属性 设置为 true
。这是一个布尔值 属性,将显示当前位置(您指的那个脉冲蓝点 - 它可以着色,并不总是蓝色)。
以下是 Apple 在 documentation
中的总结This property does not indicate whether the user’s position is actually visible on the map, only whether the map view should try to display it. Setting this property to
true
causes the map view to use the Core Location framework to find the current location and try to display it on the map. As long as this property istrue
, the map view continues to track the user’s location and update it periodically. The default value of this property isfalse
.Showing the user’s location does not guarantee that the location is visible on the map. The user might have scrolled the map to a different point, causing the current location to be offscreen. To determine whether the user’s current location is currently displayed on the map, use the
userLocationVisible
property.
所以在你的 viewDidLoad
函数中,你应该设置这个 属性:
mapView.showsUserLocation = true
zoomToUserLocation
属性不控制当前位置点