Iphone Placemark未提供邮政编码以了解天气信息
我想从当前位置了解天气情况 为此,我将代码用作Iphone Placemark未提供邮政编码以了解天气信息,iphone,objective-c,cllocationmanager,cllocation,clgeocoder,Iphone,Objective C,Cllocationmanager,Cllocation,Clgeocoder,我想从当前位置了解天气情况 为此,我将代码用作 -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { [self.locationManager stopUpdatingLocation]; self.location = newLocation; //
-(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]);
}
}];
}
}
但它也不包含zipcode
我得到了这个描述
{
Country = India;
CountryCode = IN;
FormattedAddressLines = (
NH8C,
Gujarat,
India
);
Name = NH8C;
State = Gujarat;
Street = NH8C;
Thoroughfare = NH8C;
}
有没有像它不提供zipcode信息,我们必须建立它?如果是,那怎么办?首先,我们得不到印度的邮政编码 谷歌API也已经停止工作 我使用YahooAPI来了解天气 下面是可能对某人有所帮助的代码
-(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;
}
-(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation{
[self.locationManager停止更新位置];
self.location=新位置;
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:woeiddURL];
if(WoeidData!=NULL)
{
n错误*woeiderr=nil;
NSDictionary*aDicWOEIDResp=[NSJSONSerialization JSONObjectWithData:WoeidData选项:NSJSONReadingMutableContainers错误:&WOEIDRR];
NSDictionary*aDictWOEID=[[[aDicWOEIDResp objectForKey:@“ResultSet”]objectForKey:@“Results”]objectAtIndex:0]objectForKey:@“woeid”];
NSString*地址=[NSString stringWithFormat:@”http://weather.yahooapis.com/forecastrss?w=%@“,adictwoiid];
ICB_WeatherConditions*icbWeather=[[ICB_WeatherConditions alloc]initWithQuery:address];
}
#导入“ICB_WeatherConditions.m”
-(ICB_WeatherConditions*)initWithQuery:(NSString*)查询
{
if(self=[super init])
{
NSURL*url=[NSURL URLWithString:query];
CXMLDocument*解析器=[[CXMLDocument alloc]initWithContentsOfURL:url选项:0错误:无]自动删除];
NSDictionary*namespaceMedia=[NSDictionary Dictionary WithObject:@”http://xml.weather.yahoo.com/ns/rss/1.0“福基:@“yweather”];
NSArray*节点=[parser nodesForXPath:@”//channel“错误:nil];
用于(CXMLNode*节点中的节点){
if([node kind]==CXMLElementKind)
{
CXMLElement*元素=(CXMLElement*)节点;
对于(int i=0;我是否尝试过模拟您已经模拟过的位置以外的其他位置?我自己尝试过,我没有从地理编码器处收到印度许多地方的邮政编码。是的,印度没有zipcode或邮政编码