MapView搜索位置在错误的坐标上可见

时间:2014-10-22 08:11:27

标签: ios annotations mkmapview

我在iOS 7.0中开发了一个应用程序。我有一个地图视图,用户可以通过注释引脚在地图中查看搜索结果。

我已经使用了自定义注释,这是第一次将注释放在正确的坐标上。我的意思是在用户位置,坐标位于正确的位置。

但是如果我滚动地图并搜索位置,那么它会在错误的坐标上显示我的注释。

当用户在地图中搜索任何内容时,下面是我的代码。 (例如酒店,博物馆,餐馆)

- (void) searchForPlace:(NSString *) keyWord {

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    [self.activityView setHidden:NO];

    [self.txtSearch resignFirstResponder];
    [self.txtSearch setEnabled:NO];

    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
    request.naturalLanguageQuery = keyWord; // @"restaurant"
    MKCoordinateSpan span = MKCoordinateSpanMake(.1, .1);

    CLLocationCoordinate2D location = self.mapView.userLocation.coordinate;
    request.region = MKCoordinateRegionMake(location, span);
    MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];

    [search startWithCompletionHandler:
     ^(MKLocalSearchResponse *response, NSError *error) {
         [self.txtSearch setEnabled:YES];
         [self removeMapOverlay];

         [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
         [self.activityView setHidden:YES];
         if (!error) {
             // Result found
             @try {

                 if (response.mapItems && [response.mapItems count] > 0) {

                     for (MKMapItem *item in response.mapItems) {
                         MKPlacemark *placeMark = item.placemark;

                         // Address details
                         NSDictionary *address = placeMark.addressDictionary;
                         NSString *titleString = @"";
                         NSString *subtitleString = @"";
                         NSString *name = @"";
                         NSString *Thoroughfare = @"";
                         NSString *State = @"";
                         NSString *City = @"";
                         NSString *Country = @"";

                         name = [address objectForKey:@"Name"] ? [address objectForKey:@"Name"] : @"";
                         Thoroughfare = [address objectForKey:@"Thoroughfare"] ? [address objectForKey:@"Thoroughfare"] : @"";
                         State = [address objectForKey:@"State"] ? [address objectForKey:@"State"] : @"";
                         City = [address objectForKey:@"City"] ? [address objectForKey:@"City"] : @"";
                         Country = [address objectForKey:@"Country"] ? [address objectForKey:@"Country"] : @"";

                         titleString = [NSString stringWithFormat:@"%@ %@", name, Thoroughfare];
                         subtitleString = [NSString stringWithFormat:@"%@ %@ %@", State, City, Country];

                         CustomAnnotation *annotation = [[CustomAnnotation alloc] initWithTitle:titleString subTitle:subtitleString detailURL:item.url location:placeMark.location.coordinate];
                         [self.mapView addAnnotation:annotation];
                     }
                     [self mapView:self.mapView regionDidChangeAnimated:YES];
                 }

             }
             @catch (NSException *exception) {
                 NSLog(@"Exception :%@",exception.description);
             }

         } else {
             NSLog(@"No result found.");
         }
     }];
}

1 个答案:

答案 0 :(得分:0)

我认为是因为这一行:

CLLocationCoordinate2D location = self.mapView.userLocation.coordinate;

这是您在用户位置围绕region搜索请求的问题。

您需要设置region的中心坐标MKMapView。因此,在地图中滚动的位置将考虑搜索区域的区域。

使用此更改上述行并检查您的搜索结果。

CLLocationCoordinate2D location = self.mapView.centerCoordinate;