7

I am making one app on iphone sdk4.0.In that did update location method never called. I have given my code below.Please help.Thanks in advance.

-(id)init
{
    [super init];

    obj=[[UIApplication sharedApplication]delegate];

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    //locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
    [locationManager startUpdatingLocation];

    return self;

}

-(void)locationManager:(CLLocationManager *)manager
   didUpdateToLocation:(CLLocation *)newLocation
          fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"%f",newLocation.coordinate.latitude);

    NSLog(@"%f",newLocation.coordinate.longitude);

    obj=[[UIApplication sharedApplication]delegate];

    location=[[CLLocation alloc]initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
    obj.lattitude1=[NSString stringWithFormat:@"%f",newLocation.coordinate.latitude];

    obj.longitude1=[NSString stringWithFormat:@"%f",newLocation.coordinate.longitude];
    //location=[[CLLocation alloc]initWithLatitude:39.9883 longitude:-75.262227];
}
4

6 に答える 6

18

さらに iOS8 では、次の 2 つの追加機能が必要です。

  • Info.plist にキーを追加し、ロケーション マネージャーに開始を求める承認を要求します。

    • NSLocationWhenInUseUsageDescription

    • NSLocationAlwaysUsageDescription

  • 対応するロケーション メソッドの承認をリクエストする必要があります。

    • [self.locationManager requestWhenInUseAuthorization]

    • [self.locationManager requestAlwaysAuthorization]

コード例:

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];

ソース: http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/

于 2014-09-22T16:32:28.310 に答える
5

こんにちは、あなたのコードは問題ないようです。これらは考えられる理由です:

あなたのinitで:locationServicesEnabledがあるかどうかを確認してください。

locationManager = [[CLLocationManager alloc] init];
if(locationManager.locationServicesEnabled == NO){
    //Your location service is not enabled, Settings>Location Services  
}

他の理由で、アプリへの位置情報の取得を許可しない場合があります。解決策: アプリケーションを iPhone から削除して再ビルドするだけで、場所を許可するダイアログが表示されます。

これを使用してエラーを確認します

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{       
    NSLog(@"Error while getting core location : %@",[error localizedFailureReason]);
    if ([error code] == kCLErrorDenied) {
        //you had denied 
    }
    [manager stopUpdatingLocation];
}

それ以外の場合はすべて問題ないようです。すでに ios 4.0 を実行していて、iPhone 3G 以降にインストールできます。iPhone 2g の場合、この問題が発生する可能性があります。

于 2011-03-21T06:17:27.993 に答える
3

これをシミュレーターでテストしている場合、機能しません。

これは、そのメソッドがデバイスの場所が変更されたときにのみ呼び出されるためです (シミュレーターは場所を変更しません)。

于 2011-03-21T07:09:09.357 に答える
1

あなた-locationManager:didFailWithError:の代理人に呼ばれることはありますか?ある時点で位置データへのアクセスを拒否し、プロンプトが表示されなくなった可能性がありますが、アクセスは拒否されます。

于 2011-03-21T06:09:21.237 に答える
0

あなたallocinitあなたの場所のマネージャーの後、位置情報サービスが許可されているかどうかを確認してください. このコードは、Apple の「LocateMe」コード サンプルからのものです。

if (locationManager.locationServicesEnabled == NO) {
    UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled" message:@"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [servicesDisabledAlert show];
    [servicesDisabledAlert release];
}

シミュレーターでテストしている場合は、代わりにデバイスでテストしてみてください。シミュレーターでは、コンピューターの WiFi がオンになっていて、既知の場所のデータベースにある WiFi ベース ステーションを見つけることができる必要があります。

于 2011-03-21T06:35:57.160 に答える