2014-05-15 1 views
2

iOS에서 Google의 자동 완성 API로 우편 번호를 가져 오는 방법은 무엇입니까?자동 완성 API로 우편 번호 받기

이미 구글 장소 API를 시도했지만 그것은 그래서

우편 번호를 반환하지

, 제발 어느 한 솔루션은 나에게 몇 가지 아이디어

답변

1

Google지도 사용 iOS SDK 빠른 사용 :

@IBAction func googleMapsiOSSDKReverseGeocoding(sender: UIButton) { 
    let aGMSGeocoder: GMSGeocoder = GMSGeocoder() 
    aGMSGeocoder.reverseGeocodeCoordinate(CLLocationCoordinate2DMake(self.latitude, self.longitude)) { 
     (let gmsReverseGeocodeResponse: GMSReverseGeocodeResponse!, let error: NSError!) -> Void in 

     let gmsAddress: GMSAddress = gmsReverseGeocodeResponse.firstResult() 
     print("\ncoordinate.latitude=\(gmsAddress.coordinate.latitude)") 
     print("coordinate.longitude=\(gmsAddress.coordinate.longitude)") 
     print("thoroughfare=\(gmsAddress.thoroughfare)") 
     print("locality=\(gmsAddress.locality)") 
     print("subLocality=\(gmsAddress.subLocality)") 
     print("administrativeArea=\(gmsAddress.administrativeArea)") 
     print("postalCode=\(gmsAddress.postalCode)") 
     print("country=\(gmsAddress.country)") 
     print("lines=\(gmsAddress.lines)") 
    } 
} 

Google 역 G eocoding Objective-C :

@IBAction func googleMapsWebServiceGeocodingAPI(sender: UIButton) { 
    self.callGoogleReverseGeocodingWebservice(self.currentUserLocation()) 
} 

// #1 - Get the current user's location (latitude, longitude). 
private func currentUserLocation() -> CLLocationCoordinate2D { 
    // returns current user's location. 
} 

// #2 - Call Google Reverse Geocoding Web Service using AFNetworking. 
private func callGoogleReverseGeocodingWebservice(let userLocation: CLLocationCoordinate2D) { 
    let url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=\(userLocation.latitude),\(userLocation.longitude)&key=\(self.googleMapsiOSAPIKey)&language=\(self.googleReverseGeocodingWebserviceOutputLanguageCode)&result_type=country" 

    AFHTTPRequestOperationManager().GET(
     url, 
     parameters: nil, 
     success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in 
      println("GET user's country request succeeded !!!\n") 

      // The goal here was only for me to get the user's iso country code + 
      // the user's Country in english language. 
      if let responseObject: AnyObject = responseObject { 
       println("responseObject:\n\n\(responseObject)\n\n") 
       let rootDictionary = responseObject as! NSDictionary 
       if let results = rootDictionary["results"] as? NSArray { 
        if let firstResult = results[0] as? NSDictionary { 
         if let addressComponents = firstResult["address_components"] as? NSArray { 
          if let firstAddressComponent = addressComponents[0] as? NSDictionary { 
           if let longName = firstAddressComponent["long_name"] as? String { 
            println("long_name: \(longName)") 
           } 
           if let shortName = firstAddressComponent["short_name"] as? String { 
            println("short_name: \(shortName)") 
           } 
          } 
         } 
        } 
       } 
      } 
     }, 
     failure: { (operation: AFHTTPRequestOperation!, error: NSError!) in 
      println("Error GET user's country request: \(error.localizedDescription)\n") 
      println("Error GET user's country request: \(operation.responseString)\n") 
     } 
    ) 

} 

자세한 내용은 here을 확인하십시오.

2

나는 아래와 같은 솔루션을 가지고 줄이

CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 
     [geocoder geocodeAddressString:item.completionText completionHandler:^(NSArray* placemarks, NSError* error){ 
      for (CLPlacemark* aPlacemark in placemarks) 
      { 
       tfAddress.placeholder = @"Loading..."; 

       // Process the placemark. 
       NSString *strLatitude = [NSString stringWithFormat:@"%.4f",aPlacemark.location.coordinate.latitude]; 
       NSString *strLongitude = [NSString stringWithFormat:@"%.4f",aPlacemark.location.coordinate.longitude]; 

       NSString *yourURL =[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%@,%@&sensor=true_or_false",strLatitude,strLongitude]; 

       AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 

       manager.requestSerializer = [AFJSONRequestSerializer serializer]; 

       [manager POST:yourURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { 

        NSArray *data = [[[responseObject objectForKey:@"results"] objectAtIndex:0]valueForKey:@"address_components"]; 
        NSString *strZip; 
        for (NSDictionary *dict in data) 
        { 
         if ([[[dict valueForKey:@"types"] objectAtIndex:0] length]>10) 
         { 
          if ([[[[dict valueForKey:@"types"] objectAtIndex:0] substringToIndex:11] isEqualToString:@"postal_code"]) 
          { 
           strZip = [dict valueForKey:@"long_name"]; 
          } 
         } 
        } 

        NSLog(@"JSON: %@", responseObject); 

       } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 

        tfAddress.placeholder = @"Zip Code"; 

        NSLog(@"Error: %@", error); 
       }]; 
      } 
     }]; 
     NSLog(@"Autocompleted with: %@", item.completionText); 
관련 문제