【发布时间】:2014-12-17 15:14:55
【问题描述】:
我在 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.");
}
}];
}
【问题讨论】:
标签: ios annotations mkmapview