1

MapView と SearchBar があります。検索バーに地名を入力します。入力した場所を検索し、その場所に注釈を付けるには MapView が必要です。誰でもこれを行うための最良の手順を提案できますか?

4

3 に答える 3

0

try this,

-(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 に答える
0

Google api と NSXMLParser を使用して、検索された場所の座標を取得し、その特定の座標に注釈を追加できます

NSXMLParser については、 https: //stackoverflow.com/questions/10845732/xml-parser-objective-c/10845892#10845892 の私の回答を見て ください。

latlng を取得するには、次の Google API を使用できます。

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

次のように住所を検索します。

- (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 に答える