2015-05-26 6 views
8

मैं उपयोगकर्ताओं को सड़क के नाम की खोज करने और UITableView में परिणाम प्रदर्शित करने की अनुमति देना चाहता हूं। इस पल के लिए क्षेत्र महत्वपूर्ण नहीं है, यह किसी भी क्षेत्र से हो सकता है।उद्देश्य-सी - उपयोगकर्ता द्वारा दर्ज की गई क्वेरी के आधार पर सड़कों की खोज

मुझे अपनी खोजों में कोई प्रासंगिक उदाहरण नहीं मिला और मुझे नहीं पता कि मुझे CLLocation या MKLocalSearch का उपयोग करना चाहिए या नहीं।

डॉक्स के आधार पर, मैं MKLocalSearch उपयोग करना चाहिए:

हालांकि स्थानीय खोज और जियोकोडिंग समान हैं, वे विभिन्न उपयोग मामलों समर्थन करते हैं। जब आप मानचित्र निर्देशांक और एक संरचित पता, जैसे पता पुस्तिका पता के बीच कनवर्ट करना चाहते हैं तो जियोकोडिंग का उपयोग करें। स्थानीय खोज का उपयोग करें जब आप स्थानों का एक सेट ढूंढना चाहते हैं जो उपयोगकर्ता के इनपुट से मेल खाता है।

लेकिन मैं दोनों तरीकों की कोशिश की है और यह मेरे लिए केवल 1 परिणाम देता है (यहां तक ​​कि-हालांकि वहाँ है एक NSArray लौटे

यह CLGeocoder दृष्टिकोण है:।

CLGeocoder *geocoding = [[CLGeocoder alloc] init]; 
[geocoding geocodeAddressString:theTextField.text completionHandler:^(NSArray *placemarks, NSError *error) { 
    if (error) { 
     NSLog(@"%@", error); 
    } else { 
     NSLog(@"%i", [placemarks count]); 
     for(CLPlacemark *myStr in placemarks) { 
      NSLog(@"%@", myStr); 
    } 
    } 
}]; 

और ये मेरे है MKLocalSearch कोशिश:

MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init]; 
request.naturalLanguageQuery = theTextField.text; 
request.region = self.region; 

localSearch = [[MKLocalSearch alloc] initWithRequest:request]; 

[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error){ 

    if (error != nil) { 
     [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Map Error",nil) 
            message:[error localizedDescription] 
            delegate:nil 
          cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] show]; 
     return; 
    } 

    if ([response.mapItems count] == 0) { 
     [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No Results",nil) 
            message:nil 
            delegate:nil 
          cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] show]; 
     return; 
    } 
    self.streets = response; 
    [self.streetsTableView reloadData]; 
}]; 

MKLocalSearch कुछ मामलों में 1 से अधिक प्रतिक्रिया वापस करने लगता है, लेकिन इन पीएलए से जुड़े हुए हैं सड़क नामों की खोज नहीं करता है।

अग्रिम धन्यवाद।

+0

क्या आपने क्षेत्र को शून्य पर सेट करने का प्रयास किया था? या आप अपने self.region पर अधिक जानकारी साझा कर सकते हैं? –

+0

मेरे पास एक नक्शा दृश्य है और मैं MapView.region का उपयोग अपने अनुभव से self.region – CristiC

+0

के रूप में कर रहा हूं MKLocalSearch बहुत प्राचीन है। क्या आप किसी भी कारण से Google स्थल API का उपयोग करने का विरोध करेंगे? –

उत्तर

4

की सामग्री को इस निकटतम मैं मिल सकता है के साथ खेलते हैं। इसमें google places API Web Service का उपयोग करना शामिल है।

नोट: आप शायद अपने Google मानचित्र एपीआई आदि का उपयोग कर सकते हैं। मुझे यकीन है कि इस जानकारी को विभिन्न Google एपीआई से प्राप्त करने के अन्य तरीके हैं।

NSURL *googlePlacesURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%@&location=%f,%f&sensor=true&key=API_KEY", formattedSearchText, location.coordinate.latitude, 
                location.coordinate.longitude]]; 

प्रतिक्रिया एक JSON ऑब्जेक्ट है। इसे एक शब्दकोश में कनवर्ट करें। > स्ट्रीट नाम

  • इलाका - -> शहर/स्थान नाम
  • राजनीतिक -> राज्य, आदि
  • जियोकोड -> अक्षांश/देशांतर उपलब्ध

    NSDictionary *response = [NSJSONSerialization JSONObjectWithData:_googlePlacesResponse 
                        options:NSJSONReadingMutableContainers error:&error]; 
    
    if([[response objectForKey:@"status"] isEqualToString:@"OK"]) 
    { 
        NSArray *predictions = [response objectForKey:@"predictions"]; 
        for(NSDictionary *prediction in predictions) 
        { 
         NSArray *addressTypes = [prediction objectForKey:@"types"]; 
         if([addressTypes containsObject:@"route"]) 
         { 
          //This search result contains a street name. 
          //Now get the street name. 
          NSArray *terms = [prediction objectForKey:@"terms"]; 
          NSDictionary *streetNameKeyValuePair = [terms objectAtIndex:0]; 
          NSLog(@"%@",[streetNameKeyValuePair [email protected]"value"]); 
         } 
        } 
    } 
    

    संभव types

  • होने के लिए मार्ग लगते हैं
  • आप उन predictions के साथ तालिका दृश्य को पॉप्युलेट कर सकते हैं जो केवल सह ntain route एक पता प्रकार के रूप में। यह काम कर सकता है।

    +1

    हाँ, यही तरीका है। मैंने आपका जवाब पढ़ने से पहले भी ऐसा ही किया है :)। इसने मेरी मदद की: http://stackoverflow.com/a/25318167/347807 – CristiC

    2

    CLGeocoder बस पता प्रारूप देता है। थप्पड़ यह अपने कोड में और mapitems

    MKLocalSearchRequest *request = [MKLocalSearchRequest new]; 
    request.naturalLanguageQuery = @"Pizza"; 
    request.region = MKCoordinateRegionMake(location.coordinate, MKCoordinateSpanMake(.01, .01)); 
    MKLocalSearch *search = [[MKLocalSearch alloc]initWithRequest:request]; 
    [search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) { 
        NSArray *mapItems = response.mapItems; 
        for (MKMapItem *mapItem in mapItems) { 
         MKPointAnnotation *point = [MKPointAnnotation new]; 
         point.coordinate = mapItem.placemark.coordinate;` 
        } 
    }]; 
    
    +0

    इस कोड के साथ समस्या यह है कि यह सड़कों पर नहीं, बल्कि स्थान लौटाती है। .. – CristiC

    1

    सरणी लौटे mapItems होता है, आप सरणी पर पुनरावृति इस तरह सभी mapItems बाहर निकलने के लिए कर सकते हैं:

    myMatchingItems = [[NSMutableArray alloc] init]; 
    for (MKMapItem *item in response.mapItems){ 
            [myMatchingItems addObject:item]; 
        } 
    

    प्रत्येक mapItem.placemark.thoroughfare स्थान कि पाया गया था की स्ट्रीट में शामिल है।

    संबंधित मुद्दे