1

バックグラウンドアプリを書きたいです。このアプリでは、ユーザーが設定した currentUserLocation という場所があります。だから私が欲しいのは、この2つの場所の間の距離を計算し、ユーザーが目的地に到着した場合、または指定された範囲にいる場合、ローカル通知のようなイベントを開始することです.

私が持っているもの

ViewController クラス (MapView を提供)

MapView クラス (マップを提供)

   - (id)initWithFrame:(CGRect)frame
 {
      self = [super initWithFrame:frame];
         if (self) {
           self.map = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];

           self.map.delegate = self;
          [self addSubview:self.map];

           self.locationManager = [[CLLocationManager alloc] init];
           self.locationManager.delegate = self;
           self.locationManager.pausesLocationUpdatesAutomatically = NO;
          self.locationManager.desiredAccuracy = [[[Manager getInstance]            returnValueForKey:@"GPS"] intValue];
  }

次に、locationManager で: didUpdateLocations: メソッド

   - (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{


CLLocation *loc = [locations lastObject];

   // calculate the distance between two points

float d = acos(sin(self.mapPin.coordinate.latitude/ 180*M_PI) *sin(self.map.userLocation.coordinate.latitude/ 180*M_PI)
             + cos(self.mapPin.coordinate.latitude/ 180*M_PI) *cos(self.map.userLocation.coordinate.latitude/ 180*M_PI) *cos(self.map.userLocation.coordinate.longitude/180*M_PI - self.mapPin.coordinate.longitude/ 180*M_PI));

d = d * 6378.137;



// calculate the distance between two points also

CLLocationDistance dist = [self.map.userLocation.location  distanceFromLocation:loc];




if (UIApplication.sharedApplication.applicationState == UIApplicationStateActive ){


    [self calculateDistanz:loc.coordinate];
}else{


    NSLog(@"dist = %f",dist/1000);
    NSLog(@"d = %f",d);


    if (d <= self.range ) {

        [[UIApplication sharedApplication] cancelAllLocalNotifications];


        self.localNotif = [[UILocalNotification alloc] init];

        if (self.localNotif == nil)

            return;

        self.localNotif.timeZone = [NSTimeZone defaultTimeZone];
        self.localNotif.alertBody = @"Text";
        self.localNotif.alertAction = @"Stop";
        self.localNotif.soundName = @"Alarm.mp3";

        NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];


        self.localNotif.userInfo = infoDict;
        self.localNotif.hasAction = YES;

        [[UIApplication sharedApplication] presentLocalNotificationNow:self.localNotif];

    }

}

}

また、アプリをローカル更新用のバックグラウンド アプリとして info.plist に登録します。問題は、バックグラウンド モードに入ると計算が停止することです。ログステートメントがコンソールに表示されるため、メソッド -(void) locationManager: didUpdateLocations: が呼び出されます。そして、この瞬間、アプリがバックグラウンド モードに入るとき、変数 d は常に同じであることがわかります。アプリがアクティブになるまで、d が更新されます。

1週間読んで読みましたが、間違いが見つかりません...助けてくれてありがとう...

4

0 に答える 0