怎么利用天气预报接口根据城市名得到城市代码?


比如北京:怎么根据北京这个城市名得到北京的城市代码:101010100?

json ios

炮姐纯爷们 10 years, 3 months ago

你说的应该是用的中央气象台API所需要的城市代码吧。

先把代码表存储到一个plist文件中

然后:


 NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"cityList" ofType:@"plist"];
_areaidDictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

areaidDictionary是区域id的字典

之后用


 - (void)transToareaidfromLocation:(CLLocation *)location {
    self.areaid = nil;
    self.geocoder = [[CLGeocoder alloc] init];
    [self.geocoder reverseGeocodeLocation:location
    completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error == nil && [placemarks count] > 0) {
            CLPlacemark *placemark = [placemarks objectAtIndex:0];
            self.city = placemark.locality;
            self.areaid = self.areaidDictionary[self.city];
            NSLog(@"city:%@",self.city);
            NSLog(@"areaid:%@",self.areaid);
        } else if (error == nil && [placemarks count] == 0) {
            NSLog(@"No location results were returned");
        } else if (error != nil) {
            NSLog(@"An error occurred:%@",error.description);
        }
    }];
}

在反地理编码的同时,利用字典找到areaid。
areaid就是你要的城市代码。

sadddd answered 10 years, 3 months ago

话说你百度下《城市代码表》然后解析下存起来不就行了?用天气预报的多费事

loli狂 answered 10 years, 3 months ago

Your Answer