解析地圖post及附近違章查詢2

業(yè)務(wù)處理層.h

//分享單例對(duì)象

+ (instancetype)shareLoadData;

//獲取數(shù)據(jù)

- (void)getData:(NSDictionary *)dic;

//定義block傳值

@property (nonatomic,strong)void (^dataDic)(NSDictionary *dataDictionary);


.m

//定義靜態(tài)變量

static LoadData *ld = nil;

//分享單例對(duì)象

+ (instancetype)shareLoadData

{

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

ld = [[LoadData alloc]init];

});

return ld;

}

+ (instancetype)allocWithZone:(struct _NSZone *)zone

{

if (!ld) {

ld = [super allocWithZone:zone];

}

return ld;

}

- (id)copy

{

return self;

}

- (id)mutableCopy

{

return self;

}

//獲取數(shù)據(jù)

- (void)getData:(NSDictionary *)dic

{

//創(chuàng)建請(qǐng)求網(wǎng)址

NSURL *url = [NSURL URLWithString:@"http://api.jisuapi.com/illegaladdr/coord"];

//創(chuàng)建請(qǐng)求對(duì)象

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

//設(shè)置請(qǐng)求方式

request.HTTPMethod = @"POST";

//設(shè)置請(qǐng)求參數(shù)

NSString *str = [NSString stringWithFormat:@"lat=%@&lng=%@&num=%@&range=%@&appkey=%@",dic[@"lat"],dic[@"lng"],dic[@"num"],dic[@"range"],dic[@"appkey"]];

//設(shè)置請(qǐng)求方法體

request.HTTPBody = [str dataUsingEncoding:NSUTF8StringEncoding];

//獲取會(huì)話對(duì)象

NSURLSession *session = [NSURLSession sharedSession];

//請(qǐng)求數(shù)據(jù)

NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

//解析json數(shù)據(jù)

NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];

//block傳值

dispatch_async(dispatch_get_main_queue(), ^{

self.dataDic(dataDic);

});

}];

//開始請(qǐng)求

[task resume];

}

第一個(gè)視圖

.h

#import "LoadData.h"

#import "DetailViewController.h"

#import<MapKit/MapKit.h>

#import<CoreLocation/CoreLocation.h>

<CLLocationManagerDelegate>

{

//定義變量地圖視圖矮固、定位對(duì)象张足、當(dāng)前位置

MKMapView *mv;

CLLocationManager *lm;

CLLocation *loc;


[super viewDidLoad];

//創(chuàng)建導(dǎo)航按鈕

UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"附近違章高發(fā)地" style:UIBarButtonItemStylePlain target:self action:@selector(itemClicked)];

self.navigationItem.rightBarButtonItem = item;

//初始化地圖視圖

mv = [[MKMapView alloc]initWithFrame:self.view.bounds];

mv.mapType = MKMapTypeStandard;

mv.zoomEnabled = YES;

mv.rotateEnabled = YES;

mv.scrollEnabled = YES;

mv.showsUserLocation = YES;

[self.view addSubview:mv];

//初始化定位管理器

lm = [[CLLocationManager alloc]init];

[lm requestWhenInUseAuthorization];

//設(shè)置代理

lm.delegate = self;

//設(shè)置精確度

lm.desiredAccuracy = kCLLocationAccuracyBest;

//色織過(guò)濾器

lm.distanceFilter = kCLDistanceFilterNone;

//開始定位

[lm startUpdatingLocation];

}

//設(shè)置導(dǎo)航按鈕響應(yīng)方法

- (void)itemClicked

{

//跳轉(zhuǎn)

DetailViewController *dvc = [[DetailViewController alloc]init];

dvc.loc = loc;

[self.navigationController pushViewController:dvc animated:YES];

}

//設(shè)置定位響應(yīng)方法- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(nonnull NSArray*)locations

{

loc = locations.lastObject;

//定位位置

MKCoordinateSpan span = {0.01,0.01};

MKCoordinateRegion region = {loc.coordinate,span};

[mv setRegion:region animated:YES];

//添加錨點(diǎn)

MKPointAnnotation *anno = [[MKPointAnnotation alloc]init];

anno.coordinate = loc.coordinate;

[mv addAnnotation:anno];

}

跳轉(zhuǎn)

.h

//定義屬性當(dāng)前位置

@property (nonatomic,strong)CLLocation *loc;


.h

<UITableViewDataSource,UITableViewDelegate>

{

//定義變量數(shù)據(jù)字典碴倾、表格

NSArray *arr;

UITableView *table;

}

//設(shè)置請(qǐng)求參數(shù)

NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%f",self.loc.coordinate.latitude],@"lat",[NSString stringWithFormat:@"%f",self.loc.coordinate.longitude],@"lng",@"1000",@"range",@"10",@"num",@"de394933e1a3e2db",@"appkey", nil];

//調(diào)用業(yè)務(wù)處理類請(qǐng)求數(shù)據(jù)

[[LoadData shareLoadData]getData:dic];

//初始化表格

table = [[UITableView alloc]initWithFrame:self.view.bounds];

table.dataSource = self;

table.delegate = self;

[self.view addSubview:table];


- (void)viewWillAppear:(BOOL)animated

{

//更新數(shù)據(jù)字典

[LoadData shareLoadData].dataDic = ^(NSDictionary *dataDictionary) {

arr = dataDictionary[@"result"];

//更新表格

[table reloadData];

};

}

//設(shè)置行數(shù)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return arr.count;

}

//設(shè)置單元格內(nèi)容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *cellid = @"cellid";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];

if (!cell) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];

}

NSDictionary *dic = arr[indexPath.row];

cell.textLabel.text = [NSString stringWithFormat:@"%@%@%@",dic[@"province"],dic[@"city"],dic[@"address"]];

cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",dic[@"content"]];

return cell;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市跃惫,隨后出現(xiàn)的幾起案子荚守,更是在濱河造成了極大的恐慌骇扇,老刑警劉巖尉剩,帶你破解...
    沈念sama閱讀 221,406評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡榕吼,警方通過(guò)查閱死者的電腦和手機(jī)饿序,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,395評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)友题,“玉大人嗤堰,你說(shuō)我怎么就攤上這事《然拢” “怎么了踢匣?”我有些...
    開封第一講書人閱讀 167,815評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)戈抄。 經(jīng)常有香客問(wèn)我离唬,道長(zhǎng),這世上最難降的妖魔是什么划鸽? 我笑而不...
    開封第一講書人閱讀 59,537評(píng)論 1 296
  • 正文 為了忘掉前任输莺,我火速辦了婚禮,結(jié)果婚禮上裸诽,老公的妹妹穿的比我還像新娘嫂用。我一直安慰自己,他們只是感情好丈冬,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,536評(píng)論 6 397
  • 文/花漫 我一把揭開白布嘱函。 她就那樣靜靜地躺著,像睡著了一般埂蕊。 火紅的嫁衣襯著肌膚如雪往弓。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,184評(píng)論 1 308
  • 那天蓄氧,我揣著相機(jī)與錄音函似,去河邊找鬼。 笑死喉童,一個(gè)胖子當(dāng)著我的面吹牛撇寞,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播泄朴,決...
    沈念sama閱讀 40,776評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼重抖,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了祖灰?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,668評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤畔规,失蹤者是張志新(化名)和其女友劉穎局扶,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,212評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡三妈,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,299評(píng)論 3 340
  • 正文 我和宋清朗相戀三年畜埋,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片畴蒲。...
    茶點(diǎn)故事閱讀 40,438評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡悠鞍,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出模燥,到底是詐尸還是另有隱情咖祭,我是刑警寧澤,帶...
    沈念sama閱讀 36,128評(píng)論 5 349
  • 正文 年R本政府宣布蔫骂,位于F島的核電站么翰,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏辽旋。R本人自食惡果不足惜浩嫌,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,807評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望补胚。 院中可真熱鬧码耐,春花似錦、人聲如沸溶其。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,279評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)握联。三九已至桦沉,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間金闽,已是汗流浹背纯露。 一陣腳步聲響...
    開封第一講書人閱讀 33,395評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留代芜,地道東北人埠褪。 一個(gè)月前我還...
    沈念sama閱讀 48,827評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像挤庇,于是被迫代替她去往敵國(guó)和親钞速。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,446評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容