【发布时间】:2016-02-21 11:22:03
【问题描述】:
我有一个应用程序,用户保存位置并设置要通知它的半径: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检查savedLocation(CLCircularRegion)的半径并检查用户是否在这个半径内,代码:
-(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
}
}
问题是当应用程序到达checkIfUserCityIsSameToLocation: 上的if 状态时,userCity 和savedLocationCity 都为零(因为reverseGeocodeCoordinate: completionHandler: 方法在后台线程上运行)。
我不知道怎么解决,
如果有人能帮我解决这个问题,我会非常感激。
非常感谢!
【问题讨论】:
标签: ios objective-c iphone cocoa-touch google-maps-sdk-ios