您的位置:首页 > 移动开发 > IOS开发

ios 地理解码(地理位置转换经纬度)

2013-04-18 14:46 176 查看
//不太精确

-(void)getPositionLatitudeAndLongitude:(NSString*)addr

{

// NSString*address = @"tokyo";

NSString*address = @"beijing";

//查詢經緯度

NSString*output = @"csv";

NSString*key = @"YouKey";

NSString*urlStr = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=%@&key=%@",address,output,key];

NSURL *url =[NSURL URLWithString:urlStr];

NSString*retstr = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

NSArray*resultArray = [retstr componentsSeparatedByString:@","];

double doublelatitude = [[resultArray objectAtIndex:2] doubleValue];

double doublelongitude = [[resultArray objectAtIndex:3] doubleValue];

[[NSUserDefaults standardUserDefaults] setDouble:doublelatitude forKey:kShopLocationLatitude];

[[NSUserDefaults standardUserDefaults] setDouble:doublelongitude forKey:kShopLocationLongitude];

[[NSUserDefaults standardUserDefaults] synchronize];

TTOpenURL([NSString stringWithFormat:@"tt://singlemap/%@",@"querystore"]);

}

//还可以

+ (CLLocationCoordinate2D)getPostion:(NSString *)address

{

NSString *googleURL = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?address=%@&sensor=true",

[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

CLLocationCoordinate2D position;

position.latitude = 0.0;

position.longitude = 0.0;

NSError *error;

NSString *retstr = [NSString stringWithContentsOfURL:[NSURL URLWithString:googleURL] encoding:NSUTF8StringEncoding error:&error];

if (retstr)

{

// NSLog(@"retstr: %@", retstr);

NSDictionary *dict = [retstr JSONValue];

if (dict)

{

NSArray *results = [dict objectForKey:@"results"];

if (results && results.count > 0)

{

NSDictionary *geometry = [[results objectAtIndex:0] objectForKey:@"geometry"];

NSDictionary *location = [geometry objectForKey:@"location"];

position.latitude = [[location objectForKey:@"lat"] doubleValue];

position.longitude = [[location objectForKey:@"lng"] doubleValue];

}

}

}

else

{

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

}

NSLog(@"%f==%f",position.latitude,position.longitude);

return position;

}

//不错的方法

//@property (nonatomic, strong) CLGeocoder *myGeocoder;

-(void)viewDidLoad{

[super viewDidLoad];

/* We have our address */

NSString *oreillyAddress =

@"1005 Gravenstein Highway North, Sebastopol, CA 95472, USA";

self.myGeocoder = [[CLGeocoder alloc] init];

[self.myGeocoder

geocodeAddressString:oreillyAddress

completionHandler:^(NSArray *placemarks, NSError *error) {

if ([placemarks count] > 0 &&

error == nil){

NSLog(@"Found %lu placemark(s).", (unsigned long)[placemarks count]);

CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];

NSLog(@"Longitude = %f", firstPlacemark.location.coordinate.longitude);

NSLog(@"Latitude = %f", firstPlacemark.location.coordinate.latitude);

}

else if ([placemarks count] == 0 &&

error == nil){

NSLog(@"Found no placemarks.");

}

else if (error != nil){

NSLog(@"An error occurred = %@", error);

}

}];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: