1

我有一个 MapView 和一个 SearchBar。我在搜索栏中输入了一些地名。我需要 MapView 来搜索我输入的地点并用注释标记该地点。谁能建议最好的步骤来做到这一点?

4

3 回答 3

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>) 并将其添加到您的地图视图中:

[self.mapView addAnnotation:annotation];
于 2012-09-04T09:47:21.913 回答
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"];

现在要添加注释,您可以在此处按照我的回答 如何从任何城市或地名设置地图位置

希望对你有帮助 :)

于 2012-09-04T09:52:25.983 回答
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];
}
于 2012-09-04T10:04:08.323 回答