在背景中比较来自 2 个位置的 2 个城市
Comparing 2 cities from 2 locations in background
我有一个应用程序,用户可以保存位置并设置要通知的半径:1KM、5KM(如果他之前保存的位置和他当前的位置在设置的半径内,用户将收到通知) 和 "City" (如果之前保存的位置所在的城市与当前所在的城市是同一个城市,用户将收到通知)。
注意:我正在使用 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:
方法是 运行 在后台线程上)。
我想不出怎么解决,
如果有人能帮助我,我将非常感激。
非常感谢!
这样做:
-(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];
}
}];
}];
}
我有一个应用程序,用户可以保存位置并设置要通知的半径:1KM、5KM(如果他之前保存的位置和他当前的位置在设置的半径内,用户将收到通知) 和 "City" (如果之前保存的位置所在的城市与当前所在的城市是同一个城市,用户将收到通知)。
注意:我正在使用 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:
方法是 运行 在后台线程上)。
我想不出怎么解决,
如果有人能帮助我,我将非常感激。
非常感谢!
这样做:
-(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];
}
}];
}];
}