【问题标题】:Search for a location in a map view on iPhone [closed]在 iPhone 上的地图视图中搜索位置 [关闭]
【发布时间】:2012-09-04 09:41:51
【问题描述】:

我有一个 MapView 和一个 SearchBar。我在搜索栏中输入了一些地名。我需要 MapView 来搜索我输入的地点并用注释标记该地点。任何人都可以建议执行此操作的最佳步骤吗?

【问题讨论】:

    标签: iphone cocoa-touch mkmapview uisearchbar


    【解决方案1】:

    您可以使用 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

    希望对你有帮助:)

    【讨论】:

    • 是否需要包含 Google API .. 并且需要为 NSXMLParser 编写代码
    • @Vanitha 不,没有必要这样做,如果您可以使用任何其他方法或其他 api 获取地址的坐标就可以了。
    • K 谢谢你.. 让我试着告诉你!!请有空
    • 在哪里包含上述代码?
    • 对不起,我是 iPhone 新手。我需要一些指导来完成任务
    【解决方案2】:

    这样搜索地址:

    - (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];
    

    【讨论】:

    • 何时调用此方法
    • 你的viewcontroller需要实现UISearchBarDelegate,设置Seach bar的delegate为你的VC,然后实现方法searchBarSearchButtonClicked:,调用[self addressLocation:searchBar.text];
    • 不需要 NSXMLParser 吗?
    • 不,通过这种方式,您可以请求 CSV 响应,您可以使用 componentsSeparatedByString:@"," 轻松解析
    • 感谢您提供最简单的方法...非常感谢!!
    【解决方案3】:

    试试这个,

    -(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];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多