코드를 통해 iPhone의 현재 위치를 얻는 방법은 무엇입니까?
-
20-09-2019 - |
문제
코드를 통해 iPhone의 현재 위치를 어떻게 얻습니까?GPS를 사용하여 위치를 추적해야 합니다.
해결책
"CoreLocation" API를 찾고 있습니다.
여기 튜토리얼
다른 팁
개발자 사이트에는 (항상 핵심 기능에 대한) 포괄적인 문서가 있으며 다음을 찾을 수 있습니다. 이 예 유용한;
기억해야 할 핵심 사항은 다음과 같습니다.
1) 위치 업데이트 수신을 시작하면 비동기적으로 도착하므로 수신 시 응답해야 합니다.정확성과 타임스탬프를 확인하여 위치를 사용할지 여부를 결정하세요.
2) 원하는 위치에 도달하자마자 업데이트 수신을 중지하는 것을 잊지 마십시오(첫 번째 위치가 아닐 수도 있음). 그렇지 않으면 배터리 수명이 소모됩니다.
3) 반환된 첫 번째 위치는 마지막으로 캐시된 위치인 경우가 많으며 휴대전화는 이전에 수정되었을 때와 동일한(아마도 높은) 정확도로 이 위치를 보고합니다.따라서 10미터까지 정확하다고 주장하는 수정 사항을 얻을 수 있지만 실제로는 현재 위치에서 수 마일 떨어진 어제 수신되었습니다.이것의 모토는 타임스탬프를 확인하는 것을 잊지 마세요입니다. 이것은 위치 수정이 실제로 수신된 시간을 알려줍니다. 타임스탬프를 확인하는 방법의 예는 다음과 같습니다..
도움이 되길 바랍니다.
CLLocationManager의 이 페이지 5개의 소스 코드 예제가 있습니다
이것을 다운로드하세요 예 현재 위치가 표시됩니다
장치의 현재 위치만 가져오려는 경우 Core Location을 직접 사용하려면 많은 양의 코드가 필요합니다. 왜냐하면 장치의 GPS가 현재 위치를 정확하게 확인할 때까지 몇 초가 소요되는 지연을 처리해야 하기 때문입니다.(CLLocationManager API는 단계별 GPS 내비게이션 앱과 같이 지속적인 위치 업데이트가 필요한 앱을 위해 구축된 것으로 보입니다.)
CLLocationManager를 직접 사용하는 대신 INTULocationManager와 같은 오픈 소스 구성 요소 이 모든 작업을 처리하고 장치의 현재 위치에 대한 하나 이상의 개별 요청을 간단하게 요청할 수 있습니다.
전체 코드는 아래에 있습니다.
1 드래그 맵 가져 오기 프레임 워크 대의원을 제공합니다
이 코드의 마지막 줄에 self.yourmapreferencename.setRegion...
// 여기에 모든 코드가 있습니다
Uikit 가져 오기 MAPKIT 가져 오기 Corelocation Class ViewController :UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
var locationManager : CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
if CLLocationManager.locationServicesEnabled()
{
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations.last! as CLLocation
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.mapView.setRegion(region, animated: true)
}
}
실제 해결책은 여기에서 찾을 수 있습니다. Z5 개념 iOS 개발 코드 조각
약간의 인코딩이 필요합니다.
- (IBAction)directions1_click:(id)sender
{
NSString* address = @"118 Your Address., City, State, ZIPCODE";
NSString* currentLocation = @"Current Location";
NSString* url = [NSStringstringWithFormat: @"http://maps.google.com/maps?saddr=%@&daddr=%@",[currentLocation stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
UIApplication *app = [UIApplicationsharedApplication];
[app openURL: [NSURL URLWithString: url]];
}