gpt4 book ai didi

ios - 当前位置未显示在 iOS 模拟器中

转载 作者:搜寻专家 更新时间:2023-10-31 22:35:36 25 4
gpt4 key购买 nike

我试图在 MKMapView mapView 中显示用户的当前位置。我请求许可,我相信我所有的基地都被覆盖了,但由于某种原因,当前位置没有显示在 map 上。

下面是代码。不要介意分段控件,我只是用它们来更改 map 类型。

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);
}
}
}

最佳答案

您需要将 MKMapView 的 showsUserLocation 属性设置为 true。这是一个 bool 值属性,将显示当前位置(您指的那个脉冲蓝点 - 它可以着色,并不总是蓝色)。

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 属性不控制当前位置点。

关于ios - 当前位置未显示在 iOS 模拟器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29216965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com