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

时间:2014-07-17 06:44:48

标签: ios objective-c xcode web-services mkmapview

我有一个应用程序,我使用谷歌API查找用户附近的地方。如果他们点击一个名为" ATM"在我的表视图中,它们被定向到MapView,它将在地图上显示它们的用户位置。然后,他们必须按另一个名为" show"在屏幕上绘制最近的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;

}

3 个答案:

答案 0 :(得分:0)

在您获取所有数据后,使用执行选择器调用 - (IBAction)showPlace:(id)发件人此方法。

答案 1 :(得分:0)

使用MKMapViewDelegate知道何时找到用户:

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

MKMapViewDelegate reference

答案 2 :(得分:0)

您应该将dispatch_sync块嵌套在dispatch_async块中。试试这样:

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