天天看点

根据经纬度获得城市

- (NSString *)getCityByGeocodingWithLatitude:(double)latitude longitude:(double)longitude

{

    NSString *urlStr = [NSString stringWithFormat:@"http://api.map.baidu.com/geocoder?output=json&location=%f,%f&key=%@",latitude,longitude,GEOCODING_KEY];

    NSURL *url = [NSURL URLWithString:urlStr];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    NSURLResponse *response = nil;

    NSError *error = nil;

    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseerror:&error];

    SBJsonParser *jsonParser = [[SBJsonParser alloc] init];

    NSDictionary *dataDict = [jsonParser objectWithData:data];

    NSLog(@"%@",dataDict);

    //city 市   district 县  street 街道  street_number 街道号

    //    NSString *tmpStr = [[[dataDict objectForKey:@"result"]objectForKey:@"addressComponent"]objectForKey:@"city"];

    NSString *tmpStr = [[[dataDictobjectForKey:@"result"]objectForKey:@"addressComponent"]objectForKey:@"city"];//市

    [jsonParser release];

    return tmpStr;

}