2016-06-24 4 views
2

내가시뮬레이터에서 ios의 현재 위치를 얻는 방법은 무엇입니까?

-(void)initLocationManager 
{ 
    locationManager=[[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    if (([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])) 
    { 
     [locationManager requestWhenInUseAuthorization]; 
    } 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 

    //[locationManager requestWhenInUseAuthorization]; 
    // [locationManager startMonitoringSignificantLocationChanges]; 
    [locationManager startUpdatingLocation]; 

} 

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    CLLocation* location = [locations lastObject]; 
    // NSDate* eventDate = location.timestamp; 
    // NSTimeInterval howRecent = [eventDate timeIntervalSinceNow]; 

    latitud = location.coordinate.latitude; 
    longitud = location.coordinate.longitude; 
    NSLog(@"%f,%f",latitud,longitud); 
    [locationManager stopUpdatingLocation]; 

} 

이 현재 위치를 얻는 방법을 말씀 해주십시오, 현재 위치를 얻기 위해이 코드를 사용하고 있지만, 올바른 결과를 얻지 않는 것은 시뮬레이터의 현재 위치를 얻기 위해, 나는에서이 시도 피곤하고 아침. 도와주세요

+0

위치 업데이트는 실제 기기에서 작동하도록되어 있습니다. 시뮬레이터에서 현재 위치를 가져올 수 없습니다. –

+0

시뮬레이터를 사용하여 현재 위치를 가져올 수 없습니다. 장치를 사용하십시오. –

+0

디버그> 위치> 사용자 정의 위치에서 위치를 설정 중이지만 아직 설정하고있는 위치가 표시되지 않습니다. – user6438311

답변

8

다음 단계를 사용하여 GPX 파일을 만듭니다.

프로젝트
  • 이동 -> 아이폰 OS - -> 자원 및 GPX 지금 당신이로 GPX 파일에 작은 코드를 할 필요가

enter image description here

  • 파일 선택> 새로운 추가 다음 :

<!-- 
     Provide one or more waypoints containing a latitude/longitude pair. If you provide one 
     waypoint, Xcode will simulate that specific location. If you provide multiple waypoints, 
     Xcode will simulate a route visitng each waypoint. 
--> 
<wpt lat="37.331705" lon="-122.030237"> // here change lat long to your lat long 
    <name>Cupertino</name> // here set name 

    <!-- 
      Optionally provide a time element for each waypoint. Xcode will interpolate movement 
      at a rate of speed based on the time elapsed between each waypoint. If you do not provide 
      a time element, then Xcode will use a fixed rate of speed. 

      Waypoints must be sorted by time in ascending order. 
     --> 
    <time>2014-09-24T14:55:37Z</time> 
</wpt> 
을 : 14,

  • 이제 우리의 GPX 파일이 그것을 선택하고 닫기를 실행하고 표시가 다음과 같이 선택 스키마를

    enter image description here

  • 을 편집로 이동 run을 선택하고 Option 메뉴에서 할

enter image description here

이제 GPX 파일에서 추가 한 위도의 위치를 ​​길게 얻을 수 있습니다. 당신은 위치 서비스에 대한 NSLocationWhenInUseUsageDescriptionNSLocationWhenInUseUsageDescription을 설정할 필요가의 Info.plist에서

  • : 다음

    UPDATE

    위치를 가능하게하기위한 코드입니다.

    NSLocationWhenInUseUsageDescription

이제 .H 클래스

#import <UIKit/UIKit.h> 
#import <CoreLocation/CoreLocation.h> 
@interface ViewController : UIViewController<CLLocationManagerDelegate> 
{ 

    __weak IBOutlet UILabel *lblLat; 
    __weak IBOutlet UILabel *lblLong; 
} 
@property(strong,nonatomic) CLLocationManager *locationManager; 
@end 

하는 .m 클래스

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.locationManager = [[CLLocationManager alloc] init]; 
    [self.locationManager requestWhenInUseAuthorization]; 


    self.locationManager.delegate = self; 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 

    [self.locationManager startUpdatingLocation]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
    NSLog(@"didFailWithError: %@", error); 
    UIAlertView *errorAlert = [[UIAlertView alloc] 
           initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [errorAlert show]; 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"didUpdateToLocation: %@", newLocation); 
    CLLocation *currentLocation = newLocation; 

    if (currentLocation != nil) { 
     lblLat.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]; 
     lblLong.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]; 
    } 
} 

근무 데모 코드 : https://github.com/nitingohel/UserCurrentLocation_Objc

+0

장치의 현재 위치를 찾고 문자열에 경도를 저장하는 코드가 있어야합니다. 예, – user6438311

+0

코드를 공유하면 코드가 현재 위치를 얻는 데 충분하지만 –

+0

은 현재 위치를 가져 오지 못합니다. 위 코드는 모든 것을 해냈다. 나는 0을 얻고있다. – user6438311

2

시뮬레이터에서 현재 위치를 직접 가져올 수 없으므로 시뮬레이터의 Debug>location>custom 위치로 이동하여 위도와 경도를 설정해야합니다. 시뮬레이터에서

+0

디버그> 위치> 사용자 정의 위치에서 위치를 설정 중입니다. , 아직 거기에 내가 설정하고있는 위치를 얻지 못한다면 – user6438311

+0

이 위치에서 처음에는 아무 것도 선택하지 않고 다시 사용자 정의 위치를 ​​클릭하고 위도 및 경도를 설정하십시오. –

+0

장치의 현재 위치를 찾고 저장해야하는 코드가 있어야합니다 그 latitiude 및 경도 일부 문자열, 그렇다면 공유하시기 바랍니다 – user6438311

3

당신은 계획

디버그 지역

enter image description here

또는 편집에

enter image description here

를 위치 시뮬레이션을 선택할 필요가있다 또는 당신은 당신의 자신의 GPX 좌표로 파일을 추가 할 수 있습니다

+0

거기에 장치의 현재 위치를 찾기 위해 몇 가지 코드가 있어야합니다 그 latitiude과 경도를 문자열에 저장 예, 그렇다면 공유하시기 바랍니다 – user6438311

+0

당신은 ' 'startUpdatingLocation' 다음에 didUpdateLocations' 클래스에 속성을 추가하고 설정하려고하지 않으면 it'self.locationManager = [[CLLocationManager alloc] init]; ' – iSashok

+0

이것은 시뮬레이터에서 완벽하게 작동합니다. 이동 및 검색 기능이있는 사용자 지정 현재 위치의 다른 솔루션. 나는 Google 통합 포드로이 문제를 성공적으로 해결했습니다. 하지만 내가 사용자 정의됩니다. 그것은 복용량 '일. – Rex

관련 문제