2013-08-16 90 views
1

我使用Facebook的SDK 3.0我們可以使用facebook sdk獲取用戶郵政編碼嗎?

Facebook的我已經添加用戶完全地址

地址 - 1,無限循環

城市/城鎮 - 加利福尼亞州Cupertino

UPDATE

按答案我試圖用FBGraphLocation協議這樣

NSDictionary<FBGraphUser> *user 
id<FBGraphPlace> objPlace = user.location; 
id<FBGraphLocation> objLocation = objPlace.location; 

NSLog(@"place.location : %@ location.street : %@",objPlace.location,objLocation.street); 

這是給(null)兩種。還我越來越objLocation object as (null)

了Facebook SDK響應,我得到的位置,因爲只有這樣

location =  { 
     id = 123456789123456; 
     name = "Cupertino, California"; 
    } 

這不是從那裏我可以使用的郵政編碼的足夠的信息。

如果Facebook不允許提供郵政編碼信息則沒有辦法僅從"Cupertino, California"信息獲取郵政編碼?

我已經使用這個網址

http://maps.googleapis.com/maps/api/geocode/json?address=Cupertino,%20California&sensor=true 

這給我造成的

{ 
     "long_name" = Cupertino; 
     "short_name" = Cupertino; 
     types =   (
      locality, 
      political 
     ); 
    }, 
     { 
     "long_name" = "Santa Clara"; 
     "short_name" = "Santa Clara"; 
     types =   (
      "administrative_area_level_2", 
      political 
     ); 
    }, 
     { 
     "long_name" = California; 
     "short_name" = CA; 
     types =   (
      "administrative_area_level_1", 
      political 
     ); 
    }, 
     { 
     "long_name" = "United States"; 
     "short_name" = US; 
     types =   (
      country, 
      political 
     ); 
    } 

如果我靜態把該地址進入網址爲

http://maps.googleapis.com/maps/api/geocode/json?address=1%20Infinite%20Loop,%20Cupertino,%20CA&sensor=true 

然後它給了我郵編代碼也。

{ 
     "long_name" = 1; 
     "short_name" = 1; 
     types =   (
      "street_number" 
     ); 
    }, 
     { 
     "long_name" = "Apple Inc."; 
     "short_name" = "Apple Inc."; 
     types =   (
      establishment 
     ); 
    }, 
     { 
     "long_name" = "Infinite Loop"; 
     "short_name" = "Infinite Loop"; 
     types =   (
      route 
     ); 
    }, 
     { 
     "long_name" = Cupertino; 
     "short_name" = Cupertino; 
     types =   (
      locality, 
      political 
     ); 
    }, 
     { 
     "long_name" = "Santa Clara"; 
     "short_name" = "Santa Clara"; 
     types =   (
      "administrative_area_level_2", 
      political 
     ); 
    }, 
     { 
     "long_name" = California; 
     "short_name" = CA; 
     types =   (
      "administrative_area_level_1", 
      political 
     ); 
    }, 
     { 
     "long_name" = "United States"; 
     "short_name" = US; 
     types =   (
      country, 
      political 
     ); 
    }, 
     { 
     "long_name" = 95014; 
     "short_name" = 95014; 
     types =   (
      "postal_code" 
     ); 
    } 

但是從Facebook的SDK,我只得到了市/縣(如上所述),聯繫人信息添加在Facebook.com

我怎樣才能做到這一點?

回答

1

最後,我使用谷歌API從我在facebook地址的經緯度,並使用這些座標我找到郵政編碼。

- (CLLocation *) getGeoCoordinatesUsingAddress: (NSString *) aStrAddres 
{ 

    NSString *esc_addr = [aStrAddres stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; 

    NSString *req = [NSString stringWithFormat:BaseURLForLocation, esc_addr]; 
    NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL]; 

    NSDictionary *googleResponse = 
    [NSJSONSerialization JSONObjectWithData: [result dataUsingEncoding:NSUTF8StringEncoding] 
            options: NSJSONReadingMutableContainers 
             error: nil]; 

    NSDictionary *resultsDict = [googleResponse valueForKey:@"results"]; 
    NSDictionary *geometryDict = [resultsDict valueForKey:@"geometry"]; 
    NSDictionary *locationDict = [geometryDict valueForKey:@"location"]; 
    NSArray *latArray = [locationDict valueForKey: @"lat"]; NSString *latString = [latArray lastObject]; 
    NSArray *lngArray = [locationDict valueForKey: @"lng"]; NSString *lngString = [lngArray lastObject]; 

    CLLocation *locationFound = [[CLLocation alloc] initWithLatitude:[latString doubleValue] longitude:[lngString doubleValue]]; 

    NSLog(@"%s lat: %f\tlon:%f ",__FUNCTION__, [latString doubleValue], [lngString doubleValue]); 

    return locationFound; 
} 

然後我用CLGeocoder類

CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { 

     for(CLPlacemark *aPlacemark in placemarks){ 
      NSString *strZipCode = [aPlacemark.addressDictionary objectForKey:(NSString *)kABPersonAddressZIPKey]; 

     } 
    }]; 

希望這有助於..

1

挖掘到SDK後我發現,您可以使用FBGraphLocation API,這是FBGraph API的一部分,訪問用戶的郵政編碼和街道。

爲了得到一個FBGraphLocation你會接取一個FBGraphUser

/*! 
@property 
@abstract Typed access to the user's current city. 
*/ 
@property (retain, nonatomic) id<FBGraphLocation> location; 

的FBGraphLocation API的位置,酒店提供的那些屬性:

/*! 
@property 
@abstract Typed access to a location's street. 
*/ 
@property (retain, nonatomic) NSString *street; 

/*! 
@property 
@abstract Typed access to a location's zip code. 
*/ 
@property (retain, nonatomic) NSString *zip; 

這就是你需要什麼,對不對?

+0

是的,我需要的郵政編碼。我已經使用Facebook的SDK現在我需要整合fbgraphlocaion的權利?這是否包含在sdk本身中? – Heena

+0

我試圖NSLog'user.location'它再次給出位置:{ id = 1112342342345; name =「Cupertino,California」; }'也嘗試過'user.location.street',但給出錯誤 – Heena

+0

對於'id objlocation = user.location',它不兼容,因爲位置來自'FBGraphPlace' – Heena

相關問題