地标未提供邮政编码以查找天气信息

Posted

技术标签:

【中文标题】地标未提供邮政编码以查找天气信息【英文标题】:Placemark not giving zip code to find out weather information 【发布时间】:2012-11-22 10:33:18 【问题描述】:

我想知道当前位置的天气。

为此,我将代码用作

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
    [self.locationManager stopUpdatingLocation];

    self.location = newLocation;
//    NSLog(@"lat long = %f,%f",self.location.coordinate.latitude,self.location.coordinate.longitude);

    // Geocode coordinate (normally we'd use location.coordinate here instead of coord).
    // This will get us something we can query Google's Weather API with

    if (boolCurrentlyWorking == NO) 

        CLGeocoder* reverseGeocoder = [[CLGeocoder alloc] init];
        if(reverseGeocoder)
        
            [reverseGeocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) 
                CLPlacemark* placemark = [placemarks objectAtIndex:0];
                if (placemark) 
                    //Using blocks, get zip code
                    NSString *zipCode = [placemark.addressDictionary objectForKey:(NSString*)kABPersonAddressZIPKey];
                    NSLog(@"placemark : %@ zipcode : %@",placemark.addressDictionary,zipCode);
                
            ];

        else
            MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:self.location.coordinate];
            geocoder.delegate = self;
            [geocoder start];
        
     

    boolCurrentlyWorking = YES;

我这里没有邮政编码。

还发现didupdate location这个方法已经被弃用,新方法是

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

    CLLocation *location = [locations lastObject];
    CLLocation *newlocation = location;
    NSLog(@"location : %@",location);
    CLGeocoder* reverseGeocoder = [[CLGeocoder alloc] init];
    if(reverseGeocoder)
    
        [reverseGeocoder reverseGeocodeLocation:newlocation completionHandler:^(NSArray *placemarks, NSError *error) 
            for(CLPlacemark *placemark in placemarks)
            
                NSLog(@"plcaemark desc : %@",[placemark description]);
            
        ];
    

但它也不包含邮政编码。

我得到了这个描述


    Country = India;
    CountryCode = IN;
    FormattedAddressLines =     (
        NH8C,
        Gujarat,
        India
    );
    Name = NH8C;
    State = Gujarat;
    Street = NH8C;
    Thoroughfare = NH8C;

是否存在不提供邮政编码信息而我们必须构建它的情况?如果是那怎么办?

【问题讨论】:

您是否尝试过模拟现有位置以外的位置?我试过了,我没有从地理编码器收到印度许多地方的邮政编码。 是的邮政编码或邮政编码不适用于印度 【参考方案1】:

首先,我们没有得到任何印度邮政编码或邮政编码。

Google API 也已停止工作。

我使用 yahoo api 来查看天气。

这是可能对某人有所帮助的代码

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
    [self.locationManager stopUpdatingLocation];

    self.location = newLocation;



        NSString *linkForWoeid = [NSString stringWithFormat:@" http://where.yahooapis.com/geocode?location=%f,%f&flags=J&gflags=R&appid=zHgnBS4m",self.location.coordinate.latitude,self.location.coordinate.longitude];
        NSURL *woeidURL = [NSURL URLWithString:linkForWoeid];
        NSData *WoeidData = [NSData dataWithContentsOfURL:woeidURL];
        if (WoeidData != NULL)
        
            NSError *woeiderr = nil;
            NSDictionary *aDicWOEIDResp = [NSJSONSerialization JSONObjectWithData:WoeidData options:NSJSONReadingMutableContainers error:&woeiderr];
            NSDictionary *aDictWOEID = [[[[aDicWOEIDResp objectForKey:@"ResultSet"]objectForKey:@"Results"]objectAtIndex:0]objectForKey:@"woeid"];

            NSString *address=[NSString stringWithFormat:@"http://weather.yahooapis.com/forecastrss?w=%@",aDictWOEID];
            ICB_WeatherConditions *icbWeather = [[ICB_WeatherConditions alloc] initWithQuery:address];



#import "ICB_WeatherConditions.m"

- (ICB_WeatherConditions *)initWithQuery:(NSString *)query

    if (self = [super init])
    
        NSURL *url = [NSURL URLWithString:query];
        CXMLDocument *parser = [[[CXMLDocument alloc] initWithContentsOfURL:url options:0 error:nil] autorelease];

        NSDictionary *namespaceMedia = [NSDictionary dictionaryWithObject:@"http://xml.weather.yahoo.com/ns/rss/1.0" forKey:@"yweather"];
        NSArray *nodes = [parser nodesForXPath:@"//channel" error:nil];

        for (CXMLNode *node in nodes) 
            if ([node kind] == CXMLElementKind)
            
                CXMLElement *element = (CXMLElement *)node;
                for(int i=0;i<[element childCount];i++)
                
                    NSString *strKey = [[element childAtIndex:i] name];
                    if([strKey isEqual:@"location"])
                    
                        location = [self stringForXPath:@"@city" ofNode:[element childAtIndex:i] withNameSpace:namespaceMedia];
                    
                    else if([strKey isEqual:@"item"])
                    
                        NSArray *nodeItem = [element nodesForXPath:@"//item" error:nil];
                        CXMLElement *elementItem = [nodeItem objectAtIndex:0];
                        for(int j=0;j<[elementItem childCount];j++)
                            NSString *strKeyItem = [[elementItem childAtIndex:j] name];
                            if([strKeyItem isEqual:@"condition"])
                                condition =[self stringForXPath:@"@text" ofNode:[elementItem childAtIndex:j] withNameSpace:namespaceMedia];
                                currentTemp = [[self stringForXPath:@"@temp" ofNode:[elementItem childAtIndex:j] withNameSpace:namespaceMedia] intValue];
                            
                            else if([strKeyItem isEqual:@"forecast"])
                            
                                NSString *date = [self stringForXPath:@"@date" ofNode:[elementItem childAtIndex:j] withNameSpace:namespaceMedia];
                                NSDate *curDate = [NSDate date];
                                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                                dateFormatter.dateFormat = @"dd MMM yyyy";
                                NSString *strCurDate = [dateFormatter stringFromDate:curDate];
                                if([date isEqual:strCurDate])
                                
                                    highTemp = [[self stringForXPath:@"@high" ofNode:[elementItem childAtIndex:j] withNameSpace:namespaceMedia] intValue];
                                    lowTemp = [[self stringForXPath:@"@low" ofNode:[elementItem childAtIndex:j] withNameSpace:namespaceMedia] intValue];
                                
                            
                        
                    
                    else
                        continue;
                

            
      
    
    return self;

这就是我获取天气详细信息的方式。 就我而言,我只需要位置、条件、高温、低温、当前温度。

【讨论】:

以上是关于地标未提供邮政编码以查找天气信息的主要内容,如果未能解决你的问题,请参考以下文章

结合 2 个地点请求以获取邮政编码

php获取邮政编码的第一部分

连接天气 api 时出现数据错误

如何在 iOS SDK 上通过给定的邮政编码 (ZIP) 获取国家/地区名称

邮政编码和半径查找建议[关闭]

按邮政编码查找位置[关闭]