ios地理位置定位

研究了一段时间,需要苹果机得到经纬度信息,然后根据google提供的api得到省市街道,需用到asihttp,研究过网络的都知道哈

不多说,上代码 h文件

//
//  AppDelegate.h
//  PhotoUpload
//
//  Created by benjiaxiaoxu  on 13-3-25.
//  Copyright (c) 2013年 benjiaxiaoxu . All rights reserved.
//

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import "ASIHTTPRequest.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate,CLLocationManagerDelegate,ASIHTTPRequestDelegate>
{
    CLLocationManager * gpsManager;
    CLLocation * location;
    CLLocationDegrees latitude;
    CLLocationDegrees longitude;
    BOOL isgps;

}

@property (strong, nonatomic) UIWindow *window;

@end
m文件


//
//  AppDelegate.m
//  PhotoUpload
//
//  Created by benjiaxiaoxu on 13-3-25.
//  Copyright (c) 2013年 benjiaxiaoxu . All rights reserved.
//

#import "AppDelegate.h"
#import "JSON.h"
#import "FileManagerClass.h"

@implementation AppDelegate

- (void)dealloc
{
    [gpsManager release],gpsManager = nil;
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//初始化
    [FileManagerClass deleteFile];

    [self initMananger];
    isgps = YES;
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}
-(void)initMananger
{
    gpsManager = [[CLLocationManager alloc]init];
    gpsManager.desiredAccuracy = kCLLocationAccuracyBest;
    gpsManager.delegate = self;
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    
    location = [locations objectAtIndex:0];
    latitude = location.coordinate.latitude;
    longitude = location.coordinate.longitude;
  
    if (isgps) {
        NSString * url = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?latlng=%f,%f&language=zh-CN&sensor=true",latitude,longitude];
        ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
        [request setRequestMethod:@"GET"];
        [request setTag:201];
        [request startAsynchronous];
        [request setDelegate:self];
        NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
        NSMutableDictionary * lpDic = [[NSMutableDictionary alloc]init];
        [lpDic setObject:[NSString stringWithFormat:@"%lf",latitude] forKey:@"latitude"];
        [lpDic setObject:[NSString stringWithFormat:@"%lf",longitude] forKey:@"longitude"];
        [userDefaults setObject:lpDic forKey:@"location"];
        [lpDic release];
        [userDefaults synchronize];
        
    }
    //设置请求定位只定位一次
    isgps = NO;
    
}
-(void)requestFailed:(ASIHTTPRequest *)request
{
    
    UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"对不起!!" message:@"地理位置读取失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alertView show];
    [alertView release];
    
}
-(void)requestFinished:(ASIHTTPRequest *)request
{
    
    NSMutableDictionary * lpDic = [[NSMutableDictionary alloc]init];

    
    NSString * messag = [request responseString];
    NSDictionary * jsonary = [messag JSONValue];
    NSArray * results = [jsonary objectForKey:@"results"];
    NSDictionary * temp1 = [results objectAtIndex:0];
    NSArray * address_components = [temp1 objectForKey:@"address_components"];
    UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"gps" message:messag delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alertView show];
    [alertView release];
    if ([address_components count] > 4) {
        //路
        NSDictionary * routeDic = [address_components objectAtIndex:0];
        //县
        NSDictionary * subCityDic = [address_components objectAtIndex:1];
        //市
        NSDictionary * cityDic = [address_components objectAtIndex:2];
        //省
        NSDictionary * provDic = [address_components objectAtIndex:3];
        //国
        NSDictionary * countryDic = [address_components objectAtIndex:4];
        [lpDic setObject:[NSNumber numberWithInt:5] forKey:@"count"];
        [lpDic setObject:[routeDic objectForKey:@"long_name"] forKey:@"route"];
        [lpDic setObject:[subCityDic objectForKey:@"long_name"] forKey:@"subCity"];
        [lpDic setObject:[cityDic objectForKey:@"long_name"] forKey:@"city"];
        [lpDic setObject:[provDic objectForKey:@"long_name"] forKey:@"prov"];
        [lpDic setObject:[countryDic objectForKey:@"long_name"] forKey:@"country"];

        
    }else
    {
        NSDictionary * routeDic = [address_components objectAtIndex:0];
        //市
        NSDictionary * cityDic = [address_components objectAtIndex:1];
        //省
        NSDictionary * provDic = [address_components objectAtIndex:2];
        //国
        NSDictionary * countryDic = [address_components objectAtIndex:3];
        [lpDic setObject:[NSNumber numberWithInt:4] forKey:@"count"];
        [lpDic setObject:[routeDic objectForKey:@"long_name"] forKey:@"route"];
        [lpDic setObject:[cityDic objectForKey:@"long_name"] forKey:@"city"];
        [lpDic setObject:[provDic objectForKey:@"long_name"] forKey:@"prov"];
        [lpDic setObject:[countryDic objectForKey:@"long_name"] forKey:@"country"];
        

    }
    
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];

    [userDefaults setObject:lpDic forKey:@"mapLocation"];
    [userDefaults synchronize];
    [lpDic release];
    [gpsManager stopUpdatingLocation];
}
//程序要进入后台
- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
//已经进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults removeObjectForKey:@"mapLocation"];
}
//程序后台唤醒
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    
    isgps = YES;
    
}
//每次程序打开都走
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    [gpsManager startUpdatingLocation];
    
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值