Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone Mapkit:如何在MKLocalSearch结果placemarks上添加注释?_Iphone_Ios_Mapkit_Objective C Blocks_Mkannotation - Fatal编程技术网

Iphone Mapkit:如何在MKLocalSearch结果placemarks上添加注释?

Iphone Mapkit:如何在MKLocalSearch结果placemarks上添加注释?,iphone,ios,mapkit,objective-c-blocks,mkannotation,Iphone,Ios,Mapkit,Objective C Blocks,Mkannotation,基本上,我需要一种方法在搜索请求拾取的所有“Walmart”上添加注释。我不是在使用interface builder,我只是在为这个应用程序使用代码 MKMapView * map = [[MKMapView alloc] initWithFrame: CGRectMake(0, 0, 320, 480)]; map.delegate = self; [self.view addSubview:map]; [super viewDidLoad]; // D

基本上,我需要一种方法在搜索请求拾取的所有“Walmart”上添加注释。我不是在使用interface builder,我只是在为这个应用程序使用代码

MKMapView * map = [[MKMapView alloc] initWithFrame:
                   CGRectMake(0, 0, 320, 480)];
map.delegate = self;
[self.view addSubview:map];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.



MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = @"Walmart";
request.region = map.region;
方法如下:

MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request];
[self.localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {

if(error)
{
        NSLog(@"localSearch startWithCompletionHandlerFailed!  Error: %@", error);
        return;
} 
else 
{
        for(MKMapItem *mapItem in response.mapItems)
        {
             MKPinAnnotation *annotation = [[MKPinAnnotation alloc] initWithCoordinate: mapItem.placemark.location.coordinate];
             [map addAnnotation:annotation];
             NSLog(@"Name for result: = %@", mapItem.name);
        }
}}];
方法如下:

MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request];
[self.localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {

if(error)
{
        NSLog(@"localSearch startWithCompletionHandlerFailed!  Error: %@", error);
        return;
} 
else 
{
        for(MKMapItem *mapItem in response.mapItems)
        {
             MKPinAnnotation *annotation = [[MKPinAnnotation alloc] initWithCoordinate: mapItem.placemark.location.coordinate];
             [map addAnnotation:annotation];
             NSLog(@"Name for result: = %@", mapItem.name);
        }
}}];

我建议只需在
viewDidLoad
中创建mapview:

- (void)viewDidLoad
{
    [super viewDidLoad];

    MKMapView * map = [[MKMapView alloc] initWithFrame:self.view.bounds];
    map.delegate = self;

    [self.view addSubview:map];
}
但当用户移动地图时,请查找Walmarts并添加注释:

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    // if on slow network, it's useful to keep track of the previous
    // search, and cancel it if it still exists

    [self.previousSearch cancel];

    // create new search request

    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
    request.naturalLanguageQuery = @"Walmart";
    request.region = mapView.region;

    // initiate new search

    MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request];
    [localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {

        NSMutableArray *annotations = [NSMutableArray array];

        [response.mapItems enumerateObjectsUsingBlock:^(MKMapItem *item, NSUInteger idx, BOOL *stop) {

            // if we already have an annotation for this MKMapItem,
            // just return because you don't have to add it again

            for (id<MKAnnotation>annotation in mapView.annotations)
            {
                if (annotation.coordinate.latitude == item.placemark.coordinate.latitude &&
                    annotation.coordinate.longitude == item.placemark.coordinate.longitude)
                {
                    return;
                }
            }

            // otherwise, add it to our list of new annotations
            // ideally, I'd suggest a custom annotation or MKPinAnnotation, but I want to keep this example simple

            [annotations addObject:item.placemark];
        }];

        [mapView addAnnotations:annotations];
    }];

    // update our "previous" search, so we can cancel it if necessary

    self.previousSearch = localSearch;
}


还有其他可能的改进(例如,
UIActivityIndicatorView
或网络活动指示器,如果正在进行搜索,可能会删除不在地图当前区域内的注释,等等),但希望您能理解。

我建议您只需在
viewDidLoad
中创建地图视图即可:

- (void)viewDidLoad
{
    [super viewDidLoad];

    MKMapView * map = [[MKMapView alloc] initWithFrame:self.view.bounds];
    map.delegate = self;

    [self.view addSubview:map];
}
但当用户移动地图时,请查找Walmarts并添加注释:

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    // if on slow network, it's useful to keep track of the previous
    // search, and cancel it if it still exists

    [self.previousSearch cancel];

    // create new search request

    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
    request.naturalLanguageQuery = @"Walmart";
    request.region = mapView.region;

    // initiate new search

    MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request];
    [localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {

        NSMutableArray *annotations = [NSMutableArray array];

        [response.mapItems enumerateObjectsUsingBlock:^(MKMapItem *item, NSUInteger idx, BOOL *stop) {

            // if we already have an annotation for this MKMapItem,
            // just return because you don't have to add it again

            for (id<MKAnnotation>annotation in mapView.annotations)
            {
                if (annotation.coordinate.latitude == item.placemark.coordinate.latitude &&
                    annotation.coordinate.longitude == item.placemark.coordinate.longitude)
                {
                    return;
                }
            }

            // otherwise, add it to our list of new annotations
            // ideally, I'd suggest a custom annotation or MKPinAnnotation, but I want to keep this example simple

            [annotations addObject:item.placemark];
        }];

        [mapView addAnnotations:annotations];
    }];

    // update our "previous" search, so we can cancel it if necessary

    self.previousSearch = localSearch;
}


还有其他可能的改进(例如,
UIActivityIndicatorView
或网络活动指示器,如果正在搜索,可能会删除不在地图当前区域内的注释,等等),但希望您能明白这一点。

您的初始化也需要更改-按照XCode注释所说的操作-所有mapview初始化都必须在[super viewDidLoad]之后进行。@moomoo-FYI,如果在慢速网络上工作,我已经更新了我的答案以进行优化,从而取消先前的搜索(如果仍在进行中的话)在启动新的网络之前。您的初始化也需要更改-按照XCode注释所说的操作-所有mapview初始化必须在[super viewDidLoad]之后进行。@moomoo-FYI,如果在慢速网络上工作,我已经更新了我的答案,以便在启动新网络之前取消先前的搜索(如果有任何搜索仍在进行中)。