【问题标题】:How to go to user location with Mapkit (Swift)如何使用 Mapkit (Swift) 去用户位置
【发布时间】:2015-07-11 19:24:29
【问题描述】:

我是应用程序开发的新手,我一直在尝试创建一个应用程序,该应用程序将使用核心位置管理器来查找用户位置,然后使用 mapkit 显示它。下面是我想出的代码。我一直无法修复它,有谁知道我做错了什么?谢谢。

//
//  ViewController.swift
//  MapKit Test
//
//  Created by TMT on 7/11/15.
//  Copyright (c) 2015 TMT Inc. All rights reserved.
//

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
    super.viewDidLoad()
    mapView.showsUserLocation = true
    mapView.showsPointsOfInterest = true
    mapView.delegate = self

  let locationManager = CLLocationManager()

  // Ask for Authorisation from the User.
  locationManager.requestAlwaysAuthorization()

  // For use in foreground
  locationManager.requestWhenInUseAuthorization()

  if CLLocationManager.locationServicesEnabled() {
     locationManager.delegate = self
     locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
     locationManager.startUpdatingLocation()
  }

  @NSCopying var location: CLLocation! { get }


  var span = MKCoordinateSpanMake(0.2
     , 0.2)

  var region = MKCoordinateRegion(center: location, span: span)

  mapView.setRegion(region, animated: true)


  var request = MKLocalSearchRequest()
  request.naturalLanguageQuery = "library"


            // Do any additional setup after loading the view, typically from a nib.
}

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


}

【问题讨论】:

标签: swift mapkit coordinates core-location


【解决方案1】:

您没有处理位置/地图的委托方法。您的代码正在设置地图和位置框架以及委托方法。你已经初始化了它们并告诉编译器你想用 mapkit/corelocation 做一些事情。所以现在你需要处理你包含的委托方法MKMapViewDelegate, CLLocationManagerDelegate,并设置为self

将委托处理和代码放在某处

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

    //Get map location
    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: 0.01, longitudeDelta: 0.01))
    self.mapView.setRegion(region, animated: true)

    //Get co ordinates
    CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error)->Void in

        if (error != nil) {
            println("Error: " + error.localizedDescription)
            return
        }

        if placemarks.count > 0 {
            let pm = placemarks[0] as! CLPlacemark
            self.displayLocationInfo(pm)
        } else {
            println("Error with the data.")
        }
    })
}
func displayLocationInfo(placemark: CLPlacemark) {

//Remove observer
self.locationManager.stopUpdatingLocation()

println(placemark.locality)
println(placemark.postalCode)
println(placemark.administrativeArea)
println(placemark.country)

}

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

【讨论】:

  • 我需要把这个放在哪里?我尝试将它粘贴到我的代码中,但我得到了一堆错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
  • 2021-12-06
  • 1970-01-01
  • 2022-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多