简体   繁体   English

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

[英]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];
}

MKMapViewDelegate reference MKMapViewDelegate参考

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.

相关问题 setRootViewController:在setViewControllers:被调用后无法按预期工作 - setRootViewController: doesn't work as expected after setViewControllers: have been called 没有调用UIWebViewDelegate函数 - UIWebViewDelegate functions have not been called 即使在[[UIPickerView alloc] init]之后设置了委托,也不会调用UIPickerView委托方法 - UIPickerView delegate methods not being called even though delegate has been set after [[UIPickerView alloc] init] OCMockito单元测试-如何验证具有原始方法的方法已被调用? - OCMockito Unit testing - how to verify that a method with primitive has been called? 如何检查是否已调用此方法? - How do I check to see if this method has been called? 如何找出何时调用对象的方法? - How to find out when the method of an object has been called? 设置两个变量后如何调用方法 - How to call a method once two variables have been set 尝试使用尚未从自身检索到的数据后,将调用委托方法didUpdateLocations-Xcode Swift - Delegate method didUpdateLocations is called after trying to use data that has not yet been retrieved from itself - Xcode Swift 隐藏某件事一定时间后如何做出if语句 - How to make a if statement over after a certain thing has been hidden for a certain time 作法:重新排序后保存UITabBarItem的位置/顺序 - How to: Save position/order of UITabBarItems after the have been reordered
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM