簡體   English   中英

調用某些方法后如何添加方法

[英]How to add a method after certain methods have been called

我有一個應用程序,在其中我使用Google API查找用戶附近的地方。 如果他們在我的表格視圖中點擊一個名為“ ATM”的單元,則會將他們定向到MapView,該視圖將在地圖上顯示其用戶位置。 然后,他們必須按屏幕上稱為“顯示”的另一個按鈕,以在最近的ATM上繪制點。 我希望我的應用程序能夠運行,以便他們不必按下第二個按鈕,並且一旦地圖加載了他們的用戶位置,應用程序就會搜索距離他們最近的“ xyz”。 這是我的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];
    });
}

這是我要消除的方法。 我希望此代碼在地圖聚焦於用戶的位置后運行:

- (IBAction)showPlace:(id)sender {
    NSString *lowerString = [tappedString lowercaseString];
    [self queryGooglePlaces:lowerString];
}

最后,這是我的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)在獲取所有數據后,使用性能選擇器 發送此方法。

使用MKMapViewDelegate知道何時找到用戶:

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

MKMapViewDelegate參考

你應該窩dispatch_sync內塊dispatch_async塊。 嘗試這樣:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM