【发布时间】:2012-09-04 09:41:51
【问题描述】:
我有一个 MapView 和一个 SearchBar。我在搜索栏中输入了一些地名。我需要 MapView 来搜索我输入的地点并用注释标记该地点。任何人都可以建议执行此操作的最佳步骤吗?
【问题讨论】:
标签: iphone cocoa-touch mkmapview uisearchbar
我有一个 MapView 和一个 SearchBar。我在搜索栏中输入了一些地名。我需要 MapView 来搜索我输入的地点并用注释标记该地点。任何人都可以建议执行此操作的最佳步骤吗?
【问题讨论】:
标签: iphone cocoa-touch mkmapview uisearchbar
您可以使用 Google api 和 NSXMLParser 来获取搜索到的地点的坐标,然后在该特定坐标处添加注释
对于 NSXMLParser,请查看我在 https://stackoverflow.com/questions/10845732/xml-parser-objective-c/10845892#10845892 中的回答
您可以使用以下 google api 获取 latlng
NSMutableString *url = [NSMutableString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/xml?address="];
// replace searchAddressString with your address from search bar.
[url appendString:[searchAddressString stringByReplacingOccurrencesOfString:@" " withString:@","]];
[url appendString:@"&sensor=true"];
现在要添加注释,您可以在此处关注我的回答How to set Map location from any city or place name
希望对你有帮助:)
【讨论】:
这样搜索地址:
- (CLLocationCoordinate2D)addressLocation:(NSString*)name
{
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",
[name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSUTF8StringEncoding error:nil];
NSArray *listItems = [locationString componentsSeparatedByString:@","];
double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
// error
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
return location;
}
使用返回的位置对象创建注释 (NSObject <MKAnnotation>) 并将其添加到您的地图视图中:
[self.mapView addAnnotation:annotation];
【讨论】:
试试这个,
-(void)searchBar:(UISearchBar *)searchbar textDidChange:(NSString *)searchText {
NSString *searchURL=[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/xml?location=13.0604220,80.2495830&radius=500&types=restaurant&name=%@&sensor=false&key=%@",searchBar.text,kGooglePlaceAPIkey];
NSURL *url=[[NSURL alloc]initWithString:searchURL];
NSURLRequest *URLReq=[[NSURLRequest alloc]initWithURL:url];
[NSURLConnection connectionWithRequest:URLReq delegate:self];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[activityIndicator startAnimating];
[NSURLConnection sendAsynchronousRequest:URLReq queue:queue completionHandler:^(NSURLResponse *response,NSData *data,NSError *error)
{
xmlParser=[[XMLParser alloc]loadXMLByURL:searchURL];
[self.tableView reloadData];
[mapView removeAnnotations:mapView.annotations];
DisplayMap *selected = [[DisplayMap alloc] init];
for(int i=0;i<[xmlParser.places count];i++) {
currentPlace=[[xmlParser places]objectAtIndex:i];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = [currentPlace.latitude doubleValue];
region.center.longitude = [currentPlace.longitude doubleValue];
region.span.longitudeDelta = 0.03f;
region.span.latitudeDelta = 0.03f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
mapView.zoomEnabled = YES;
mapView.scrollEnabled = YES;
DisplayMap *ann = [[DisplayMap alloc] init];
ann.item=i;
ann.title = currentPlace.name;
ann.subtitle = currentPlace.address;
ann.coordinate = region.center;
[mapView addAnnotation:ann];
if (currentSelectPlace == i)
selected = ann;
}
if(value == 1) {
if(currentSelectPlace+1) {
[mapView setCenterCoordinate:selected.coordinate animated:YES];
[mapView selectAnnotation:selected animated:NO];
}
}
[activityIndicator stopAnimating];
}];
[self.mapView reloadInputViews];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchbar {
[searchBar resignFirstResponder];
}
【讨论】: