【问题标题】:Current Location not showing in iOS Simulator当前位置未显示在 iOS 模拟器中
【发布时间】:2015-03-23 17:49:01
【问题描述】:

我正在尝试在 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);
    } 
}
}

【问题讨论】:

    标签: ios swift location mkmapview


    【解决方案1】:

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

    这是 Apple 在他们的 documentation 中总结的方式

    这个属性并不表示用户的位置是否真的在地图上可见,只表示地图视图是否应该尝试显示它。将此属性设置为true 会导致地图视图使用核心位置框架来查找当前位置并尝试将其显示在地图上。只要此属性为true,地图视图就会继续跟踪用户的位置并定期更新。该属性的默认值为false

    显示用户的位置并不能保证该位置在地图上可见。用户可能已经将地图滚动到不同的点,导致当前位置不在屏幕上。要确定用户的当前位置当前是否显示在地图上,请使用userLocationVisible 属性。

    所以在你的viewDidLoad 函数中,你应该设置这个属性:

    mapView.showsUserLocation = true
    

    zoomToUserLocation 属性不控制当前位置点。

    【讨论】:

      【解决方案2】:

      有时您需要先设置自定义位置才能让模拟器更新并实际设置位置(不知道为什么)。我要做的是去Debug->Location->Apple 只是为了检查地图位置的东西是否真的有效。然后,我使用从谷歌地图或类似地图中找到的坐标设置了一个自定义位置。

      【讨论】:

      • 谢谢。在多次尝试使用“Apple”位置失败后,我才将位置更改为自定义。还是不行。
      • 好的,看看@Sam 的回复,那应该可以帮到你了:)
      猜你喜欢
      • 2012-12-25
      • 2015-10-01
      • 2011-09-04
      • 2017-06-30
      • 2011-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-26
      相关资源
      最近更新 更多