2016-06-12 1 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

당신은 '아무튼 무슨 뜻인지 설명해주십시오. t 작업 - 그렇지 않으면 당신을 도울 수있는 방법을 알기가 어렵습니다. – Feldur

+0

시뮬레이터를 실행하면 세상의지도가 표시되고 현재 위치는 전혀 알려지지 않습니다 –

+0

위치 대리인이 귀하에게 보내는 정보를 무시합니다. 인쇄하는 것보다 현재 위치가 표시되지 않는 이유입니다. 활성화되었을 때'pin' 기능은 무엇입니까? – Feldur

답변

2

시뮬레이터가 현재 위치를 알지 못합니다. 시뮬레이터가 사용자의 위치를 ​​알 수 있도록해야합니다. 시뮬레이터 메뉴에서 는 Debug > Location > Custom Location

당신은 Lat 및 물리적 위치의 Long 또는 기타 위치 정보를 입력 할 수 있습니다 선택합니다. 다음

enter image description here

0

내가 쓴 응용 프로그램에서입니다. didUpdateLocations :을 locationManager (매니저가 때 비슷한 호출해야이라고

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을 영역을 생성하고 호출하는 라인입니다

관련 문제