简体   繁体   中英

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. 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. They then have to press ANOTHER button called "show" on the screen to plot the points on the nearest ATM's. 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. Here is the code for my 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:

- (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:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    NSString *lowerString = [tappedString lowercaseString];
    [self queryGooglePlaces:lowerString];
}

MKMapViewDelegate reference

You should nest the dispatch_sync block inside dispatch_async block. Try like this:

dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
        dispatch_sync(kBgQueue, ^{
             [self fetchedData:data];
        })
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM