我有一个MapView和一个SearchBar。我在搜索栏中输入了一些地名。我需要MapView来搜索我输入的地方并用注释标记该地点。任何人都可以建议这样做的最佳步骤吗?
答案 0 :(得分:0)
像这样搜索地址:
- (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>
)并将其添加到mapview中:
[self.mapView addAnnotation:annotation];
答案 1 :(得分:0)
此link可能会对您有所帮助。分步指南将帮助您找到您搜索的内容。
答案 2 :(得分:0)
您可以使用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
希望这会对你有所帮助:)。
答案 3 :(得分:0)
试试这个,
-(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];
}