{"id":581,"date":"2018-02-23T23:40:11","date_gmt":"2018-02-23T15:40:11","guid":{"rendered":"http:\/\/60.205.208.74\/2018\/02\/23\/ios%e5%ae%9a%e4%bd%8d%e6%93%8d%e4%bd%9c\/"},"modified":"2018-02-23T23:40:11","modified_gmt":"2018-02-23T15:40:11","slug":"ios%e5%ae%9a%e4%bd%8d%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/www.puzi7.net\/?p=581","title":{"rendered":"iOS\u5b9a\u4f4d\u64cd\u4f5c"},"content":{"rendered":"<span itemprop=\"description\"><div class=\"article-body\">\n<div class=\"article-intro\" id=\"content\">\n<h1>IOS\u5b9a\u4f4d\u64cd\u4f5c<\/h1>\n<hr>\n<h2>\u7b80\u4ecb<\/h2>\n<p>\u5728IOS\u4e2d\u901a\u8fc7CoreLocation\u5b9a\u4f4d\uff0c\u53ef\u4ee5\u83b7\u53d6\u5230\u7528\u6237\u5f53\u524d\u4f4d\u7f6e\uff0c\u540c\u65f6\u80fd\u5f97\u5230\u88c5\u7f6e\u79fb\u52a8\u4fe1\u606f\u3002<\/p>\n<h3>\u5b9e\u4f8b\u6b65\u9aa4<\/h3>\n<p>1\u3001\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684View based application\uff08\u89c6\u56fe\u5e94\u7528\u7a0b\u5e8f\uff09\u3002<\/p>\n<p>2\u3001\u62e9\u9879\u76ee\u6587\u4ef6\uff0c\u7136\u540e\u9009\u62e9\u76ee\u6807\uff0c\u7136\u540e\u6dfb\u52a0CoreLocation.framework,\u5982\u4e0b\u6240\u793a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"attachment-full\" alt=\"CoreLocation_Library_Addition\" src=\"http:\/\/www.runoob.com\/wp-content\/uploads\/2013\/11\/CoreLocation_Library_Addition.jpg\" width=\"450\" height=\"501\" data-pinit=\"registered\"><\/p>\n<p>3\u3001\u5728ViewController.xib\u4e2d\u6dfb\u52a0\u4e24\u4e2a\u6807\u7b7e\uff0c\u521b\u5efaibOutlet\u540d\u4e3alatitudeLabel\u548clongtitudeLabel\u7684\u6807\u7b7e<\/p>\n<p>4\u3001\u73b0\u5728\u901a\u8fc7\u9009\u62e9&#8221; File-&gt; New -&gt; File&#8230; -&gt; &#8220;\u9009\u62e9Objective C class \u5e76\u5355\u51fb\u4e0b\u4e00\u6b65<\/p>\n<p>5\u3001\u628a&#8221;sub class of&#8221;\u4f5c\u4e3aNSObject\uff0c\u5c06\u7c7b\u547d\u540d\u4e3aLocationHandler<\/p>\n<p>6\u3001\u9009\u62e9\u521b\u5efa<\/p>\n<p>7\u3001\u66f4\u65b0LocationHandler.h\uff0c\u5982\u4e0b\u6240\u793a<\/p>\n<pre>\n#import &lt;Foundation\/Foundation.h&gt;\n#import &lt;CoreLocation\/CoreLocation.h&gt;\n\n@protocol LocationHandlerDelegate &lt;NSObject&gt;\n\n@required\n-(void) didUpdateToLocation:(CLLocation*)newLocation \n fromLocation:(CLLocation*)oldLocation;\n@end\n\n@interface LocationHandler : NSObject&lt;CLLocationManagerDelegate&gt;\n{\n    CLLocationManager *locationManager;\n}\n@property(nonatomic,strong) id&lt;LocationHandlerDelegate&gt; delegate;\n\n+(id)getSharedInstance;\n-(void)startUpdating;\n-(void) stopUpdating;\n\n@end\n<\/pre>\n<p>8\u3001\u66f4\u65b0LocationHandler.m,\u5982\u4e0b\u6240\u793a\n<\/p>\n<pre>\n#import \"LocationHandler.h\"\nstatic LocationHandler *DefaultManager = nil;\n\n@interface LocationHandler()\n\n-(void)initiate;\n\n@end\n\n@implementation LocationHandler\n\n+(id)getSharedInstance{\n    if (!DefaultManager) {\n        DefaultManager = [[self allocWithZone:NULL]init];\n        [DefaultManager initiate];\n    }\n    return DefaultManager;\n}\n-(void)initiate{\n    locationManager = [[CLLocationManager alloc]init];\n    locationManager.delegate = self;\n}\n\n-(void)startUpdating{\n    [locationManager startUpdatingLocation];\n}\n\n-(void) stopUpdating{\n    [locationManager stopUpdatingLocation];\n}\n-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:\n (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{\n    if ([self.delegate respondsToSelector:@selector\n    (didUpdateToLocation:fromLocation:)]) \n    {\n        [self.delegate didUpdateToLocation:oldLocation \n        fromLocation:newLocation];\n\n    }\n}\n\n@end\n<\/pre>\n<p>9\u3001\u66f4\u65b0ViewController.h,\u5982\u4e0b\u6240\u793a<\/p>\n<pre>\n#import &lt;UIKit\/UIKit.h&gt;\n#import \"LocationHandler.h\"\n@interface ViewController : UIViewController&lt;LocationHandlerDelegate&gt;\n{\n    IBOutlet UILabel *latitudeLabel;\n    IBOutlet UILabel *longitudeLabel;\n}\n@end\n<\/pre>\n<p>10\u3001\u66f4\u65b0ViewController.m,\u5982\u4e0b\u6240\u793a<\/p>\n<pre>\n#import \"ViewController.h\"\n\n@interface ViewController ()\n\n@end\n\n@implementation ViewController\n\n- (void)viewDidLoad\n{\n    [super viewDidLoad];\n    [[LocationHandler getSharedInstance]setDelegate:self];\n    [[LocationHandler getSharedInstance]startUpdating];\n}\n\n- (void)didReceiveMemoryWarning\n{\n    [super didReceiveMemoryWarning];\n    \/\/ Dispose of any resources that can be recreated.\n}\n\n-(void)didUpdateToLocation:(CLLocation *)newLocation \n fromLocation:(CLLocation *)oldLocation{\n    [latitudeLabel setText:[NSString stringWithFormat:\n    @\"Latitude: %f\",newLocation.coordinate.latitude]];\n    [longitudeLabel setText:[NSString stringWithFormat:\n    @\"Longitude: %f\",newLocation.coordinate.longitude]];\n\n}\n\n@end\n<\/pre>\n<h3>\u8f93\u51fa<\/h3>\n<p>\u5f53\u6211\u4eec\u8fd0\u884c\u8be5\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f1a\u5f97\u5230\u5982\u4e0b\u7684\u8f93\u51fa:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"attachment-full\" alt=\"locationOutput\" src=\"http:\/\/www.runoob.com\/wp-content\/uploads\/2013\/11\/locationOutput.jpg\" width=\"250\" height=\"495\" data-pinit=\"registered\"><\/p>\n<\/p><\/div>\n<\/p><\/div>\n<span>","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\"><span itemprop=\"description\">IOS\u5b9a\u4f4d\u64cd\u4f5c \u7b80\u4ecb \u5728IOS\u4e2d\u901a\u8fc7CoreLocation\u5b9a\u4f4d\uff0c\u53ef\u4ee5\u83b7\u53d6\u5230\u7528\u6237\u5f53\u524d\u4f4d\u7f6e\uff0c&hellip;<\/span><\/p>\n<p class=\"more-link-p btn-align-right\"><a class=\"green zoom-btn\" href=\"https:\/\/www.puzi7.net\/?p=581\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,26],"tags":[74,93],"class_list":["post-581","post","type-post","status-publish","format-standard","hentry","category-ios","category-mobile","tag-ios","tag-mobile","zoom-theme-has-thumb"],"_links":{"self":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/581","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=581"}],"version-history":[{"count":0,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/581\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}