2012-11-22 86 views
0

我想從當前位置查找天氣。不提供郵政編碼查找天氣信息的地標

對於我所使用的代碼作爲

-(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位置的這種方法已被棄用,新的​​方法是

-(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; 
} 

有沒有像它不提供郵政編碼信息,我們必須建立呢?如果是,那麼如何?

+0

您是否嘗試過模擬其他位置以外的位置?我嘗試過自己,而且我沒有從地理編碼器接收印度許多地方的郵政編碼。 – Mark

+0

是印度沒有郵政編碼或郵政編碼 – Heena

回答

1

首先,我們沒有得到任何印度的郵政編碼或郵政編碼。

此外Google API已停止工作。

我用雅虎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; 
} 

這是我得到的氣象信息。 在我的情況下,我只需要位置,條件,高溫,低溫,當前溫度。

相關問題