Google API to Get Weather Information

后端 未结 4 1015
心在旅途
心在旅途 2021-02-04 20:16

I need to fetch the weather information based on the user\'s current location. Please provide me the Google API to get the weather info.

I am using http://www.google.com/

4条回答
  •  太阳男子
    2021-02-04 21:11

    Google Weather API is down. Check out the following alternatives:

    Open Weather Map

    National Digital Forecast

    Weather.com

    I've been using the first link. Its free like Open Street Maps.

    EDIT:

    Here's the basic code to get the JSON:

    NSError *error = nil;
    NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://openweathermap.org/data/2.1/find/city?lat=32.85190&lon=57.65878&cnt=1&type=JSON"]];
    
    if (jsonData) {
    
        id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    
        if (error) {
            NSLog(@"error is %@", [error localizedDescription]);
    
            // Handle Error and return
            return;
    
        }
    
        NSArray *keys = [jsonObjects allKeys];
        // values in foreach loopDic]
        for (NSString *key in keys) {
            NSLog(@"%@ is %@",key, [jsonObjects objectForKey:key]);
        }
    
    
    } else {
        // Handle Error
    }
    

    Add that to the viewDidLoad

提交回复
热议问题