现在的位置: 首页 > 综合 > 正文

异步获取地址

2012年10月23日 ⁄ 综合 ⁄ 共 4257字 ⁄ 字号 评论关闭

/根绝经纬度获取地点信息

- (NSString*)GetAddressByCoordinate:(double)latitude longitude:(double)longitud

{

NSLog(@"加载eeeeee");

NSString *ld = [[NSString alloc] initWithFormat:@"%f",latitude];

NSString *lt = [[NSString alloc] initWithFormat:@"%f",longitud];

NSString *ldt=[NSString stringWithFormat:@"%@,%@",ld,lt];//[[ld stringByAppendingFormat:@","] stringByAppendingFormat:lt];

NSString *url1=@"http://maps.google.com/maps/api/geocode/json?latlng=";

NSString *url2=@"&sensor=true&language=zh-CN";

NSString *urlRequest=[NSString stringWithFormat:@"%@%@%@",url1,ldt,url2];//[[url1 stringByAppendingFormat:ldt] stringByAppendingFormat:url2];

NSLog(@"urlRequest:%@",urlRequest);

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:urlRequest]

  cachePolicy:NSURLRequestUseProtocolCachePolicy

  timeoutInterval:60.0];

// create the connection with the request

// and start loading the data

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if (theConnection) {

// Create the NSMutableData to hold the received data.

// receivedData is an instance variable declared elsewhere.

receivedData = [[NSMutableData data] retain];

} else {

// Inform the user that the connection failed.

NSLog(@"fail");

}

//NSString *jsonString = [[NSString alloc] 

// initWithBytes:[receivedData bytes] 

// length:[receivedData length] 

// encoding:NSASCIIStringEncoding];

// NSLog(@"jsonString",jsonString);

// NSString *jsonString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];  

// NSDictionary * jsonDic = [jsonString JSONValue];  

//    NSArray *dict = [jsonDic valueForKeyPath:@"results.formatted_address"];

//

// NSString *address=[dict objectAtIndex:0];

return @"address";

}

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

{

    // This method is called when the server has determined that it

    // has enough information to create the NSURLResponse.

    // It can be called multiple times, for example in the case of a

    // redirect, so each time we reset the data.

    // receivedData is an instance variable declared elsewhere.

    [receivedData setLength:0];

}

//接收NSData数据

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

{

    // Append the new data to receivedData.

    // receivedData is an instance variable declared elsewhere.

    [receivedData appendData:data];

}

- (void)connection:(NSURLConnection *)connection

  didFailWithError:(NSError *)error

{

    // release the connection, and the data object

    [connection release];

    // receivedData is declared as a method instance elsewhere

    [receivedData release];

    // inform the user

    NSLog(@"Connection failed! Error - %@ %@",

          [error localizedDescription],

          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);

}

//接收完毕,显示结果

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSLog(@"加载次数:dddddd");

    NSString *jsonString = [[NSString alloc

                         initWithBytes:[receivedData bytes

                         length:[receivedData length

                         encoding:NSUTF8StringEncoding];

NSLog(@"jsonString",jsonString);

//http://maps.google.com/maps?saddr={%E6%B7%B1%E5%9C%B3}&daddr={%E5%A4%A9%E6%B4%A5}  导航

//NSString *jsonString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];  

NSDictionary * jsonDic = [jsonString JSONValue];  

//: format not a string literal and no format arguments

//NSDictionary * dict = [jsonDic objectForKey:@"results"];//valueForKeyPath

    NSArray *dict = [jsonDic valueForKeyPath:@"results.formatted_address"];

NSString *address=[dict objectAtIndex:0];

curAddress=address;

[addAnnotation setTitle:@"当前位置"];

[addAnnotation setSubtitle:address];

[connection release];

    [receivedData release];

}    

系统自带方法

//MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc]  initWithCoordinate:newLocation.coordinate ];

// geoCoder.delegate = self;

// [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"zh-CN", nil] forKey:@"AppleLanguages"];

// [geoCoder start];

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{

    NSLog(@"MKReverseGeocoder has failed.");

}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {

    NSString *address = [NSString stringWithFormat:@"%@ %@ %@ %@ %@%@",

 

placemark.country,

 

placemark.administrativeArea,

 

placemark.locality,

 

placemark.subLocality,

 

placemark.thoroughfare,

 

placemark.subThoroughfare];

    NSLog(@"经纬度所对应的详细:%@", address);

[addAnnotation setTitle:@"当前位置"];

[addAnnotation setSubtitle:address];

    geocoder = nil;

}

抱歉!评论已关闭.