通过经纬度获取城市yahoo天气预报(最新2013-4-17)

 原文:http://blog.sina.com.cn/s/blog_87a824440101bqcl.html


注:原来的版本不可以用了,yahoo改了写东西,现在只有通过yql来实现对天气的获取woeid的值,请大家注意看看:改法如下:
原来是 :
#define URL_LOCATION @ "http://where.yahooapis.com/geocode? location=%f+%f&gflags=R&appid=yourappid"
现在是 : iphone <wbr>通过经纬度获取城市yahoo天气预报(最新2013-4-17)
(用宏定义的转义字符,居然在新浪博客显示不出来,只有裁图了,不裁图显示这个: #define URL_LOCATION @"http://query.yahooapis.com/v1/public/yql?q=select  * from geo.placefinder where text="%f,%f" and gflags="R"",晕 ),但这样做还是会出错,解决方法请看下面的: - (void)xmlReadConnect函数



首先声明一下: 由于大陆的GPS定位的经纬度有误差,导致你所在的位置的经纬度有偏差,但能确定是在你当前所在的城市,因为我们只要所在城市,再通过城市获取yahoo天气预报,因此只要你在的城市是正确的即可。


Weather.h 文件

#import <UIKit/UIKit.h>

@interface Weather : UIViewController<CLLocationManagerDelegate,NSXMLParserDelegate,NSURLConnectionDelegate,NSURLConnectionDataDelegate>{

    CLLocationManager *lma; 

     NSXMLParser *xmlParser;

    NSURLConnection *urlConn;

    NSMutableData *webData;

    NSURLRequest *request;

}

 

@property (assign) CLLocationManager *lma;

 

@property (nonatomic,retain)   NSString *getCurrentElement;

 

@property (nonatomic,retain)   NSDictionary *Dic_weathForecast;

 

@property (nonatomic,retain)   NSString *getwoeidValue;

- (void)xmlReadConnect;
-(void)mapLocationPreLoad;



Weather.m 文件

#import "Weather.h"

@implementation Weather

 

@synthesize lma;

 

@synthesize getCurrentElement;

 

@synthesize Dic_weathForecast;

 

@synthesize getwoeidValue;

#define URL_FORECAST @"http://xml.weather.yahoo.com/forecastrss?w=%@&u=%@"

//以前的版本用:

//#define URL_LOCATION @"http://where.yahooapis.com/geocode?location=%f+%f&gflags=R&appid=yourappid"

//2013-4-17 现在用yql, 这里用到宏定义的转意字符 \ ,这里只有裁图了 iphone <wbr>通过经纬度获取城市yahoo天气预报(最新2013-4-17)

//被新浪拦截了转义字符后是这样的,#define URL_LOCATION @"http://query.yahooapis.com/v1/public/yql?q=select * from geo.placefinder where text="%f,%f" and gflags="R"" 但这样做还是会出错,原因请看下面的 - (void)xmlReadConnect函数方法
-(void)mapLocationPreLoad{

    

    lma = [[CLLocationManager alloc] init];

    lma.delegate = self;

    lma.desiredAccuracy = kCLLocationAccuracyBest;

    lma.distanceFilter = 5;

}


- (void)xmlReadConnect

{

//被注释的部分,是之前的版本  

   //CLLocationCoordinate2D coordinate = [lm.location coordinate];      

    //NSString *URLString = [NSStringstringWithFormat:URL_LOCATION,coordinate.latitude, coordinate.longitude];//通过经纬度获取城市的woeid

    //request = [NSURLRequest requestWithURL:[NSURLURLWithString:URLString]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

   // urlConn = [[NSURLConnection alloc] initWithRequest:requestdelegate:self];

    //if (urlConn

    //{

    //    webData = [[NSMutableData allocinit];

   // }

 

 

//(2013-4-17改成这样了)改动之后注意:首先你要启动你的GPS,获取你所在地的经纬度,如果经纬度获取成功之后,然后通过经纬度获取woeid,之前的方法现在用不了了,现在yahoo通过经纬度获取woeid的方法改了,需要用yql来获取,yql的使用可以参考yahoo的yql用法。 

    

   //URL编码中 iphone <wbr>通过经纬度获取城市yahoo天气预报(最新2013-4-17)表示空格符 ,  iphone <wbr>通过经纬度获取城市yahoo天气预报(最新2013-4-17) 表示双引号 ,不懂的请自己去查询 URL编码手册啊 

  
//本来URLString = @"http://query.yahooapis.com/v1/public/yql?q=select * from geo.placefinder where text="%f,%f" and gflags="R"" 就可以了,但这样写会出错,因为这写%f参数和双引号的原因,所以要用URL编码来处理,方法如下:

   //改动之后是:

iphone <wbr>通过经纬度获取城市yahoo天气预报(最新2013-4-17)
//上面的方法只有通过三个字连接起来的

//这里只能用插图了,不好意思,因为新浪博客会拦截URL编码

}


 


- (void)viewDidLoad

{  

[self mapLocationPreLoad];

[self xmlReadConnect];

 

}



 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{    

     [webData setLength:0];

}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

     [webData appendData:data];

}


- (void)connection:(NSURLConnection *)connection didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge*)challenge

{

    

}


- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

       

    [urlConn release];

}


- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge*)challenge

{

   

}


- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

{

  

}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    NSString *xmlString = [[[NSString alloc] initWithBytes:[webDatamutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]retain];   

    if (xmlParser)

    {

        [xmlParser release];

    }

    xmlParser = [[NSXMLParser alloc] initWithData:webData];

    [xmlParser setDelegate:self];

    [xmlParser setShouldResolveExternalEntities:YES];

    [xmlParser parse];

}


 

bool flagNow = YES;

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString*)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict

{    

    getCurrentElement = elementName;  

    [getCurrentElement retain];

    if ([elementName isEqualToString:@"yweather:condition"])//获取天气的信息

    {

        self.Dic_weathCondition = attributeDict;

        currentWeatherValue = [[[NSString stringWithFormat:@"%@",[attributeDict objectForKey:@"text"]] uppercaseString] retain];

        currentTempValue    = [[[NSString stringWithFormat:@"%@",[attributeDict objectForKey:@"temp"]] uppercaseString] retain];

NSLog(@"currentWeatherValue = %@, currentTempValue = %@",[attributeDict objectForKey:@"text"],[attributeDict objectForKey:@"temp"]);

      

    }

    if ([elementName isEqualToString:@"yweather:forecast"])

    {

        if (flagNow == YES

        {

            self.Dic_weathForecast = attributeDict;             

            flagNow = NO;

            NSString *stringWeather  = [NSStringstringWithFormat:@"%@",[attributeDict objectForKey:@"text"]];

            NSString *stringHighTemp = [NSStringstringWithFormat:@"%@",[attributeDict objectForKey:@"high"]];

            NSString *stringLowTemp  = [NSStringstringWithFormat:@"%@",[attributeDict objectForKey:@"low"]];

                   NSLog(@"stringWeather= %@,stringHighTemp = %@,stringLowTemp = %@",stringWeather,stringHighTemp,stringLowTemp);

            return ;

        }

        else

        {

            flagNow = YES;

        }

    }

}


 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString*)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

{

    if (qName)

    {

        elementName = qName;

    }

}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString*)string

{

    if ([[getCurrentElement lowercaseString]isEqualToString:@"woeid"])

    {

        getwoeidValue = string;  //获取woeid的值     

        if (getwoeidValue.length>0

        {                        

            NSString *URLString = [NSStringstringWithFormat:URL_FORECAST,getwoeidValue@"c"];               

            request = [NSURLRequest requestWithURL:[NSURLURLWithString:URLString]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

            urlConn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

            if (urlConn

            {                   

                webData = [[NSMutableData allocinit];

            }

        }

        [getwoeidValue retain];

    }    

}


@end


//内存泄漏问题大家自己改了,我只提高实现获取yahoo天气方法


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值