2016-06-12 60 views
-3

//下面是對這個問題的一些詳細信息=嘗試啓動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) 




    } 



} 
+0

請解釋一下你的沒有按」是什麼意思。牛逼的工作 - 否則很難知道如何幫助你 – Feldur

+0

當我欠幅脈衝模擬器它只是顯示一個世界地圖,並在所有 –

+0

沒有指出我的當前位置你選擇忽略的位置代表向您發送等。比打印出來,所以這就是爲什麼你的當前位置不露面。什麼是啓動的時候'pin'功能呢? – Feldur

回答

2

模擬器不知道你現在的位置。您需要讓模擬器知道您的位置。 在模擬器菜單中選擇Debug > Location > Custom Location

您可以進入Lat和你的物理位置Long或其他地點。

enter image description here

0

下面是一個應用程序我寫的。你需要調用類似的東西時的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線

相關問題