通過系統(tǒng)定位獲取天氣信息

首先,需要通過系統(tǒng)方法獲取當(dāng)前經(jīng)緯度

引入頭文件 #import <coreLocation/CoreLocation.h>

創(chuàng)建manager對(duì)象,用來獲取當(dāng)前位置

@property (strong, nonatomic)CLLocationManager *locationManager;

然后,寫一個(gè)實(shí)例方法,來統(tǒng)一獲取定位、溫度

#pragma mark --定位&溫度

- (void)locationAndTemperature{

// 初始化位置管理器

if (![CLLocationManager locationServicesEnabled])

{

//定位未開啟,溫度無法顯示 (筆者并未完善,讀者此處可以根據(jù)需求進(jìn)行修改)

}

else

{

_locationManager = [[CLLocationManager alloc] init];

_locationManager.delegate = self;

[self.locationManager requestWhenInUseAuthorization];

[_locationManager startUpdatingLocation];

}

}

開始系統(tǒng)定位以后,系統(tǒng)會(huì)以非常高的頻率執(zhí)行更新定位方法

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray*)locations{

此處的locations 就是當(dāng)前定位的數(shù)組,里面存的是CLLocation 類型的位置信息 包含經(jīng)緯度

此時(shí),筆者使用的是與蘋果系統(tǒng)相同的天氣API,需要通過城市名獲取當(dāng)前天氣信息的JSON文件,因此,此時(shí)可以通過系統(tǒng)的反向地理編碼獲取當(dāng)前經(jīng)緯度所在的城市。

// 獲取當(dāng)前所在的城市名

CLGeocoder *geocoder = [[CLGeocoder alloc] init];

for (CLLocation *loca in _locations) {

[geocoder reverseGeocodeLocation:loca completionHandler:^(NSArray *array, NSError *error){

if (array.count > 0)

{

CLPlacemark *placemark = [array objectAtIndex:0];

NSString *city = placemark.locality;

NSString *city1 = [city stringByReplacingCharactersInRange:NSMakeRange(city.length-1,1) withString:@""];

//更改為英文 并調(diào)用天氣api進(jìn)行查詢 (此處調(diào)用的類方法實(shí)現(xiàn),在此方法下方)同時(shí),

[self transform:city1]用來將中文的城市名轉(zhuǎn)換為英文

[WeatherService getTemperatureWithCityName:[self transform:city1] completion:^(NSError *error, id responseObject) {

if (responseObject != nil) {

[self.temperatureButton setTitle:[NSString stringWithFormat:@"%@℃",responseObject] forState:UIControlStateNormal];

;

[_locationManager stopUpdatingLocation];

}

LOG("溫度%@",responseObject);

}];

}

else if (error == nil && [array count] == 0)

{

LOG("無數(shù)據(jù)返回");

}

else if (error != nil)

{

LOG("錯(cuò)誤:%@", error);

}

}];

}


}

--此處是將中文轉(zhuǎn)換為英文的方法

//將中文轉(zhuǎn)換為拼音

- (NSString *)transform:(NSString *)chinese

{

if (chinese != nil) {

NSMutableString *pinyin = [chinese mutableCopy];

CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);

CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO);

return [[pinyin uppercaseString] stringByReplacingOccurrencesOfString:@" " withString:@""];

}

return nil;

}


-- 此處是筆者單獨(dú)封裝的Weather類,用來請(qǐng)求數(shù)據(jù)

+ (AFHTTPRequestOperation *)getTemperatureWithCityName:(NSString *)cityName completion:(SCServiceCompletion)completion{

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

__block NSString *temperature = @"";

AFHTTPRequestOperation * DataTask = [manager GET:[NSString stringWithFormat:@"http://api.wunderground.com/api/你的API參數(shù)/conditions/q/CA/%@.json",cityName] parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id? _Nonnull responseObject) {

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];

NSDictionary *dic1 = [dic objectForKey:@"current_observation"];

temperature = [dic1 objectForKey:@"feelslike_c"];

//? ? ? ? dispatch_queue_t queue = dispatch_queue_create("WEATHER", DISPATCH_QUEUE_CONCURRENT);

dispatch_async(dispatch_get_main_queue(), ^{

dispatch_async(dispatch_get_main_queue(), ^{

if (completion) {

completion(nil, temperature);

}

});

});

} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {

}];

return DataTask;

}


此時(shí),即可獲取到手機(jī)定位所在地的體感溫度

API鏈接地址 : http://api.wunderground.com/weather/api/d/docs?d=data/index

筆者QQ 268081059 如有不妥之處,歡迎指出


本文作者原創(chuàng),如需轉(zhuǎn)載,請(qǐng)注明出處。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容