2010-12-21 79 views
0
-(CLLocationCoordinate2D) addressLocation { 

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

    return location; 
} 

StringWithContentsOfURL已弃用此链接警告....掌握的代码

回答

2

尝试代替: NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];

NSData *locationData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]; 
NSString *locationString = [[NSString alloc] initWithData:locationData encoding:NSASCIIStringEncoding]; 

关于这样的好事是使y您可以使用NSURLConnection来获取数据(异步)。

2

如果是什么让你感到困惑是这个“StringWithContentsOfURL已经过时”。苹果的文档中快速搜索表明:

stringWithContentsOfURL: 返回从给定的URL指定的文件中读取数据创建一个字符串。 (弃用在IOS 2.0使用stringWithContentsOfURL:编码:错误:stringWithContentsOfURL:usedEncoding:错误:代替)

一个例子是:

stringWithContentsOfURL:yourURL encoding:NSUTF8StringEncoding error:NULL