我试着去地图上显示我的当前位置,但是当我在模拟器中运行我的代码,这是行不通的
问题描述:
//下面是对这个问题的一些详细信息=尝试启动MapKit位置更新,而不提示进行位置信息授权。必须首先调用 - [CLLocationManager requestWhenInUseAuthorization]或 - [CLLocationManager requestAlwaysAuthorization]。我试着去地图上显示我的当前位置,但是当我在模拟器中运行我的代码,这是行不通的
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet var myMap : MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
myMap.showsUserLocation = true }
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
print("locations = \(locValue.latitude) \(locValue.longitude)")
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("Errors: " + error.localizedDescription)
}
@IBAction func satelliteView(){
myMap.mapType = MKMapType.Satellite
}
@IBAction func normalView(){
myMap.mapType = MKMapType.Standard
}
@IBAction func pin(sender: UILongPressGestureRecognizer) {
let location = sender.locationInView(self.myMap)
let lCoord = self.myMap.convertPoint(location, toCoordinateFromView: self.myMap)
let anno = MKPointAnnotation()
anno.coordinate = lCoord
anno.title = "store"
anno.subtitle = "loctaion of Store"
self.myMap.removeAnnotations(myMap.annotations)
self.myMap.addAnnotation(anno)
}
}
答
下面是一个应用程序我写的。你需要调用类似的东西时的LocationManager(经理:didUpdateLocations:被称为
func addPinAtCoordinate(latlong: Vector2D, plotting: Bool = defaultPlotState) {
if self.plotting && !plotting {
self.removeAnnotations(annotationStack)
annotationStack = []
self.plotting = plotting
}
let lat = latlong.x
let lon = latlong.y > 180.0 ? latlong.y - 360.0 : latlong.y
let coord = CLLocationCoordinate2DMake(lat, lon)
let region = self.regionThatFits(MKCoordinateRegionMake(coord, MKCoordinateSpan(latitudeDelta: mapSpanDegrees, longitudeDelta: mapSpanDegrees)))
if region.center.latitude != -180.0 || region.center.longitude != -180.0 {
self.setRegion(region, animated: true)
let note = MKPointAnnotation()
let annotPoint = CLLocationCoordinate2D(latitude: lat, longitude: lon)
note.coordinate = annotPoint
self.addAnnotation(note)
self.annotationStack.append(note)
if annotationStack.count > annotationStackLength {
self.removeAnnotation(annotationStack[0])
annotationStack.removeAtIndex(0)
}
}
else {
NSLog("Can't place invalid cordinate: ", latlong)
}
}
最特别有用相对于你的问题是创建一个区域,并呼吁setRegion线
请解释一下你的没有按”是什么意思。牛逼的工作 - 否则很难知道如何帮助你 – Feldur
当我欠幅脉冲模拟器它只是显示一个世界地图,并在所有 –
没有指出我的当前位置你选择忽略的位置代表向您发送等。比打印出来,所以这就是为什么你的当前位置不露面。什么是启动的时候'pin'功能呢? – Feldur