繁体   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