首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >比较两个城市背景下的两个地点

比较两个城市背景下的两个地点
EN

Stack Overflow用户
提问于 2016-02-21 11:22:03
回答 1查看 61关注 0票数 0

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

注意:我使用的是Google Maps iOS SDK

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

代码语言:javascript
复制
-(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检查savedLocation (CLCircularRegion)的半径,并检查用户是否在此半径内,代码:

代码语言:javascript
复制
-(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城市和用户的位置城市,并检查它们是否相等(问题在这里),代码:

代码语言:javascript
复制
-(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
    }
}

问题是,当应用程序到达checkIfUserCityIsSameToLocation:上的checkIfUserCityIsSameToLocation:状态时,userCitysavedLocationCity都是零(因为reverseGeocodeCoordinate: completionHandler:方法运行在后台线程上)。

我想不出怎么解决,

如果有人能帮我做这件事,我会好好学习的。

非常感谢!

EN

Stack Overflow用户

回答已采纳

发布于 2016-02-21 11:48:48

这样做吧:

代码语言:javascript
复制
-(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];
    }
    }];
  }];
}
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35535460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档