[英]How to add a method after certain methods have been called
I have an app in which I use a google API to find places near a user. 我有一个应用程序,在其中我使用Google API查找用户附近的地方。 If they tap a cell named "ATM" on my table view, they are directed to a MapView which will display their user location on the map.
如果他们在我的表格视图中点击一个名为“ ATM”的单元,则会将他们定向到MapView,该视图将在地图上显示其用户位置。 They then have to press ANOTHER button called "show" on the screen to plot the points on the nearest ATM's.
然后,他们必须按屏幕上称为“显示”的另一个按钮,以在最近的ATM上绘制点。 I want my app to run so that they don't have to press the second button, and once the map loads their user location, the app searches for the nearest "xyz's" near them.
我希望我的应用程序能够运行,以便他们不必按下第二个按钮,并且一旦地图加载了他们的用户位置,应用程序就会搜索距离他们最近的“ xyz”。 Here is the code for my querySearch:
这是我的querySearch的代码:
-(void) queryGooglePlaces: (NSString *) googleType
{
NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%@&types=%@&sensor=true&key=%@", currentCentre.latitude, currentCentre.longitude, [NSString stringWithFormat:@"%i", currenDist], googleType, kGOOGLE_API_KEY];
//Formulate the string as URL object.
NSURL *googleRequestURL=[NSURL URLWithString:url];
// Retrieve the results of the URL.
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}
Here is the method I want to eliminate. 这是我要消除的方法。 I want this code to run once the map has focused on the user's location:
我希望此代码在地图聚焦于用户的位置后运行:
- (IBAction)showPlace:(id)sender {
NSString *lowerString = [tappedString lowercaseString];
[self queryGooglePlaces:lowerString];
}
And lastly, here are my MapView delegate method's incase they help: 最后,这是我的MapView委托方法的帮助:
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
//Zoom back to the user location after adding a new set of annotations.
//Get the center point of the visible map.
CLLocationCoordinate2D centre = [mv centerCoordinate];
MKCoordinateRegion region;
//If this is the first launch of the app then set the center point of the map to the user's location.
if (firstLaunch) {
region = MKCoordinateRegionMakeWithDistance(locationManager.location.coordinate,1000,1000);
firstLaunch=NO;
}else {
//Set the center point to the visible region of the map and change the radius to match the search radius passed to the Google query string.
region = MKCoordinateRegionMakeWithDistance(centre,currenDist,currenDist);
}
//Set the visible region of the map.
[mv setRegion:region animated:YES];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
//Define our reuse indentifier.
static NSString *identifier = @"MapPoint";
if ([annotation isKindOfClass:[MapPoint class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.animatesDrop = YES;
return annotationView;
}
return nil;
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
//Get the east and west points on the map so we calculate the distance (zoom level) of the current map view.
MKMapRect mRect = self.mapView.visibleMapRect;
MKMapPoint eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mRect), MKMapRectGetMidY(mRect));
MKMapPoint westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), MKMapRectGetMidY(mRect));
//Set our current distance instance variable.
currenDist = MKMetersBetweenMapPoints(eastMapPoint, westMapPoint);
//Set our current centre point on the map instance variable.
currentCentre = self.mapView.centerCoordinate;
}
调用-(IBAction)showPlace:(id)在获取所有数据后,使用性能选择器 发送此方法。
Use MKMapViewDelegate to know when the user has been found: 使用MKMapViewDelegate知道何时找到用户:
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSString *lowerString = [tappedString lowercaseString];
[self queryGooglePlaces:lowerString];
}
You should nest the dispatch_sync
block inside dispatch_async
block. 你应该窝
dispatch_sync
内块dispatch_async
块。 Try like this: 尝试这样:
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
dispatch_sync(kBgQueue, ^{
[self fetchedData:data];
})
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.