iphone 地图 根据地址获取经纬度

//不太精确

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

     }

     

   }];

  

}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值