2016-06-12 3 views
-3

// Hier sind einige weitere Informationen zum Problem = Es wird versucht, MapKit-Standortaktualisierungen ohne Aufforderung zur Standortautorisierung zu starten. Muss zuerst - [CLLocationManager requestWhenInUseAuthorization] oder - [CLLocationManager requestAlwaysAuthorization] aufrufen.Ich versuche, meinen aktuellen Standort auf einer Karte zu zeigen, aber wenn ich meinen Code im Simulator starte, funktioniert es nicht

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

Bitte erklären, was Sie doesn bedeuten‘. t Arbeit - sonst ist es schwer zu wissen, wie ich Ihnen helfen kann – Feldur

+0

Wenn ich den Simulator runt zeige es nur eine Weltkarte und zeigt nicht auf meine aktuelle Position überhaupt –

+0

Sie ignorieren, was der Standortdelegat Ihnen anderen sendet als es auszudrucken, deshalb wird Ihr aktueller Standort nicht angezeigt. Was macht die "Pin" -Funktion, wenn aktiviert? – Feldur

Antwort

2

Der Simulator kennt Ihren aktuellen Standort nicht. Sie müssen dem Simulator Ihren Standort mitteilen. Im Simulator-Menü wählen Debug > Location > Custom Location

Sie die Lat und Long Ihrer physischen Standort eingeben oder anderen Orten nutzen.

enter image description here

0

Das Folgende ist aus einer App ich geschrieben habe. Sie müssen etwas Ähnliches rufen, wenn Locationmanager (Manager: didUpdateLocations: genannt wird

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

Ganz besonders nützlich in Bezug auf Ihr Problem sind die Linien einen Bereich zu schaffen und fordern setRegion

Verwandte Themen