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);
}
}];
}
-(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);
}
}];
}
相关文章推荐
- IOS 经纬度 转换地理位置信息
- iOS获取地理位置,根据经纬度获取省市街道
- iOS开发,定位当前所在地理位置偏移,坐标转换
- 调用高德的接口,地理位置和经纬度相互转换
- 转载]IOS LBS功能详解[0](获取经纬度)[1](获取当前地理位置文本 )
- iOS中利用系统地图获取定位信息(经纬度/地理位置)
- 百度地图 经纬度和地理位置的转换
- ios 定位经纬度获取位置信息,地理位置编码与反编码
- 地图网站MapBar地理位置标记ID转换为经纬度的Javascript解密算法
- iOS开发——定位获取经纬度坐标,并反编码获得地理位置信息(具体地址,国家、省、市等)
- iOS上地图通过地理位置经纬度找出城市等信息
- R实现地理位置与经纬度相互转换
- 获取定位,苹果IOS10以上不支持h5的geolocation获取不到地理位置信息解决办法
- IOS获取当前地理位置文本
- IOS-OC-百度地图-正向地理编码 位置NSString转坐标
- 一个百度定位例子,把自己的经纬度转换为具体位置
- 百度地图和谷歌地图使用的地理位置坐标标准以及转换
- Android 通过经纬度获取地理位置信息
- IOS 百度地图定位,显示地理位置
- 轻松实现坐标转换 不同地理位置系统转换入门