在背景中比较2个城市的2个城市

时间:2016-02-21 11:22:03

标签: ios objective-c iphone cocoa-touch google-maps-sdk-ios

我有一个应用程序,用户保存一个位置并设置要通知的半径:1KM,5KM(用户将收到通知,如果他之前保存的位置和他在设置的半径中的当前位置)和“城市”(如果他之前保存的城市和他当前所在的城市是同一城市,则用户将收到通知。

注意:我正在使用Google Maps iOS SDK

我有一个名为-sendNotificationIfNeeded的方法,用于检查是否发送通知,代码:

-(void)sendNotificationIfNeeded
{    
    if (self.muteEnabled==YES) { //Check if the user muted the app
        NSLog(@"Mute enabled");
        return;
    }

    //Important part:
    if([self checkIfUserInRadiusForLocation:[self.savedLocations objectAtIndex:0]])
    {
        UILocalNotification *notification=[self createNotification];
        [self sendNotification:notification];
    }
}

方法checkIfUserInRadiusForLocation检查savedLocationCLCircularRegion)的半径并检查用户是否在此半径范围内,代码:

-(BOOL)checkIfUserInRadiusForLocation:(SavedLocation*)location
{
    if(location.radiusString isEqualToString:@“1KM”)
    {
        if(location.circularRegion containsCoordinate:self.locationManager.location.coordinate)
        {
            return YES;
        }
    }else if(location.radiusString isEqualToString:@“5KM”)
    {
        if(location.circularRegion containsCoordinate:self.locationManager.location.coordinate)
        {
            return YES;
        }
    //Important part:
    }else if(location.radiusString isEqualToString:@“City”)
    {
        if([self checkIfUserCityIsSameToLocation:location)
        {
            return YES;
        }
    }
    return NO;
}

方法checkIfUserCityIsSameToLocation:找到savedLocation城市和用户的位置城市并检查它们是否相等(问题在这里),代码:

-(BOOL)checkIfUserCityIsSameToLocation:(savedLocation*)location
{
    //Checking user's city
    __block NSString *userCity;
    [[GMSGeocoder geocoder]reverseGeocodeCoordinate:self.locationManager.location.coordinate completionHandler:^(GMSReverseGeocodeResponse *response, NSError *error) {
        if (error) {
            NSLog(@"%@",[error description]);
        }
        userCity=[[[response results] firstObject] locality];
    }];
    //Checking savedLocation’s city
    __block NSString *savedLocationCity;
    [[GMSGeocoder geocoder]reverseGeocodeCoordinate:location.circularRegion.center completionHandler:^(GMSReverseGeocodeResponse *response, NSError *error) {
        if (error) {
            NSLog(@"%@",[error description]);
        }
        savedLocationCity=[[[response results] firstObject] locality];

        NSLog(@"");
    }];

    if ([userCity isEqualToString:savedLocationCity]) { //Both string cities are nil
        Return YES;
    }else
    {
        Return No
    }
}

问题是,当if checkIfUserCityIsSameToLocation: userCitysavedLocationCity reverseGeocodeCoordinate: completionHandler:上的 Index.html ----------------- <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> <title></title> <link href="lib/ionic/css/ionic.css" rel="stylesheet"> <link href="css/style.css" rel="stylesheet"> <script src="lib/ionic/js/ionic.bundle.js"></script> <script src="cordova.js"></script> <script src="js/app.js"></script> </head> <body ng-app="starter"> <ion-pane> <ion-header-bar class="bar-stable"> <h1 class="title">Ionic Blank Starter</h1> </ion-header-bar> <ion-content> this is net connection testing example </ion-content> </ion-pane> </body> </html> app.js -------------- angular.module('starter', ['ionic']) .run(function($ionicPlatform) { $ionicPlatform.ready(function() { if(window.cordova && window.cordova.plugins.Keyboard) { cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); cordova.plugins.Keyboard.disableScroll(true); } if(window.StatusBar) { StatusBar.styleDefault(); } if(window.Connection){ if(navigator.connection.type == Connection.NONE) { alert("there is no internet connection"); $ionicPopup.confirm({ title: "Internet Disconnected", content: "The internet is disconnected on your device." }) .then(function(result) { if(!result) { ionic.Platform.exitApp(); } }); } } }); }) 声明为nil时(因为weapp/scripts/app.js方法正在运行后台线程)。

我无法弄清楚如何解决它,

如果有人能帮助我,我会非常感激。

非常感谢!

1 个答案:

答案 0 :(得分:0)

这样做:

-(BOOL)checkIfUserInRadiusForLocation:(SavedLocation*)location userCity: (NSString *) userCity locationCity: (NSString *) locationCity
{
    if(location.radiusString isEqualToString:@“1KM”)
    {
      if(location.circularRegion containsCoordinate:self.locationManager.location.coordinate)
      {
        return YES;
      }
    }else if(location.radiusString isEqualToString:@“5KM”)
    {
      if(location.circularRegion containsCoordinate:self.locationManager.location.coordinate)
      {
        return YES;
      }
    //Important part:
    }else if(location.radiusString isEqualToString:@“City”)
    {
      if([userCity isEqual: locationCity])
      {
        return YES;
      }
    }
return NO;
}


-(void)sendNotificationIfNeeded
{    
    if (self.muteEnabled==YES) { //Check if the user muted the app
        NSLog(@"Mute enabled");
        return;
    }
    SavedLocation* location = [self.savedLocations objectAtIndex:0];
    [[GMSGeocoder geocoder]reverseGeocodeCoordinate:self.locationManager.location.coordinate completionHandler:^(GMSReverseGeocodeResponse *response, NSError *error) {
    if (error) {
        NSLog(@"%@",[error description]);
    }
    NSString *userCity=[[[response results] firstObject] locality];
   [[GMSGeocoder geocoder]reverseGeocodeCoordinate:location.circularRegion.center completionHandler:^(GMSReverseGeocodeResponse *response, NSError *error) {
    if (error) {
        NSLog(@"%@",[error description]);
    }
    NSString *savedLocationCity=[[[response results] firstObject] locality];

    if([self checkIfUserInRadiusForLocation:location userCity: userCity locationCity: savedLocationCity])
    {
      UILocalNotification *notification=[self createNotification];
      [self sendNotification:notification];
    }
    }];
  }];
}