2013-03-15 41 views
0

你好 我使用谷歌API和這種方法來搜索一個位置,但現在它顯示 (610,0,0,0)作爲迴應?Google API for iphone獲取位置不工作?

這裏是我的代碼:

-(CLLocationCoordinate2D) addressLocation { 
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
       [addressField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]]; 
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 { 
    //Show error 
} 
CLLocationCoordinate2D location; 
location.latitude = latitude; 
location.longitude = longitude; 

return location; 
} 

回答

1

試試這個::

- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address 
{ 
    double latitude = 0.0; 
    double longitude = 0.0; 

    NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
    NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr]; 

    NSDictionary *googleResponse = [[NSString stringWithContentsOfURL: [NSURL URLWithString: req] encoding: NSUTF8StringEncoding error: NULL] JSONValue]; 

    NSDictionary *resultsDict = [googleResponse valueForKey: @"results"]; // get the results dictionary 
    NSDictionary *geometryDict = [resultsDict valueForKey: @"geometry"]; // geometry dictionary within the results dictionary 
    NSDictionary *locationDict = [geometryDict valueForKey: @"location"]; // location dictionary within the geometry dictionary 

    NSArray *latArray = [locationDict valueForKey: @"lat"]; 
    NSString *latString = [latArray lastObject];  // (one element) array entries provided by the json parser 

    NSArray *lngArray = [locationDict valueForKey: @"lng"]; 
    NSString *lngString = [lngArray lastObject];  // (one element) array entries provided by the json parser 

    CLLocationCoordinate2D location; 
    location.latitude = [latString doubleValue];// latitude; 
    location.longitude = [lngString doubleValue]; //longitude; 

    return location; 
} 

我發現同樣的問題早。

可能是有關Google API服務的問題。

希望它會對您有所幫助。

謝謝。

+0

好..很高興幫助你。享受編碼.. :) – 2013-03-19 07:34:19

相關問題