2017-09-14 9 views
0

를 반환 내가 가진 :MKMapItem.forCurrentLocation()는 "알 수없는 위치"내 <code>ViewController's</code> 클래스 <code>viewDidLoad</code> 방법 내부

override func viewDidLoad() { 
    super.viewDidLoad() 
    requestAuthorization() 
    locationManager.delegate = self 
    print(MKMapItem.forCurrentLocation()) 
} 

을 여기 requestAuthorization() 기능입니다 :

private func requestAuthorization(){ 
    if CLLocationManager.authorizationStatus() == .notDetermined{ 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     locationManager.requestWhenInUseAuthorization() 
    } 
    else if CLLocationManager.authorizationStatus() == .denied{ 
     //TODO 
    } 

} 

문제는 forCurrentLocation() 기능을 결코 반환하는 실제 사용자 위치 대신 MKMapItem의 좌표는 다음과 같습니다 (위도 : 0, 경도 : 0). 다음은 인쇄 기능의 결과입니다.

MKMapItem : 0x60000014e020 isCurrentLocation = 1; name = "알 수없는 위치";

내가 뭘 잘못하고 있니?

편집 :

내가 MKMapItem.forCurrentLocation() 나는 사용자가 prepareForSegue 좌표를 얻을 계획이었다 사용하고자하는 이유. 그리고 나는 didUpdateLocations 대리자 메서드를 사용하는 것보다 쉬울 것이라고 생각했습니다. 코드 참조 :처럼,

if segue.identifier == "categories", 
     let destinationVC = segue.destination as? CategoriesGroupViewController{ 
     if let cell = sender as? UICollectionViewCell{ 
      let indexPath = categoriesCollectionView.indexPath(for: cell) 
      destinationVC.groupOfDestinations = destinationsByCategory[indexPath!.row] 
      // 0 is index for Near Me cell 
      if indexPath?.row == 0 { 
       destinationVC.groupOfDestinations = getNearMeDestinations() 
      } 

} 

private func getNearMeDestinations() -> GroupOfDestinations{ 
     let userCoordinates = MKMapItem.forCurrentLocation().placemark.coordinate 
     let nearMeCircularRegion: CLCircularRegion = CLCircularRegion(center: userCoordinates, radius: 10000, identifier: "nearMe") 
... 

     return nearMeDestinations 
    } 
+0

ViewController 안에 MKMapView가 있으면'print (mapView.userLocation.location? .coordinate)'를 사용하십시오. –

답변

0
locationManager.desiredAccuracy = kCLLocationAccuracyBest 

외부의 다른 블록의 경우 선 위에 넣어를 당신에게 현재 위치를 제공 맵에 대한 위임 방법은있다

var locationManager: CLLocationManager? 

private func requestAuthorization(){ 

    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.delegate = self 

    if CLLocationManager.authorizationStatus() == .notDetermined{ 
     locationManager.requestWhenInUseAuthorization() 
    } 
    else if CLLocationManager.authorizationStatus() == .denied{ 
     //TODO 
    } 
} 

// CLLocationManagerDelegate 
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

    guard let location = locations.last else { 
     return 
    } 
    Print(location)// This is your current location 
} 
1

너는 아무것도하지 않고있어. ng. MKMapItem.forCurrentLocation() 장치의 현재 위치를 나타내는 싱글 톤 맵 항목 개체를 만들어 반환합니다.

MKMapItem.forCurrentLocation().isCurrentLocation은지도 항목이 사용자의 현재 위치를 나타내는 지 여부를 나타내는 부울 값입니다. 귀하의 경우 true.

MKMapItem.forCurrentLocation().name지도 항목과 연관된 설명 이름입니다. 이지도 항목이 사용자의 현재 위치를 나타내는 경우 속성의 값은 지역화 된 버전의 '현재 위치'로 설정됩니다.

그리고 그것이 을 반환합니다. 알 수없는 위치이 이상합니다. 그러나 MKMapItem.forCurrentLocation().isCurrentLocation 값을 추적하는 것으로 충분합니다.

업데이트 :

var location = CLLocation() 

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    guard let latitude = manager.location?.coordinate.latitude, let longitude = manager.location?.coordinate.longitude else { return } 
    location = CLLocation(latitude: latitude, longitude: longitude) 
} 

을 그리고 항상 사용자의 이동 때 최신 될 것 위치를 사용 :
사용자의 위치를 ​​다음과 같이 조정하세요.

+0

위치 관리자 대리인이 실제 사용자 위치를 반환하더라도 MKMapItem의 좌표는 항상 (위도 : 0, 경도 : 0) 약간 변경되었습니다. –

+0

@barola_mes, 사용자 위치를 얻기 위해'MKMapItem'을 사용하지 마십시오. 그 대신에 그것을하는 방법에 대한 나의 편집을 체크 아웃하십시오. –

+0

내 편집을 참조하십시오, 처음에는 명시 적이 아니 었습니다 –