地圖錨點(diǎn)、定位傻铣、block章贞、解析、post請(qǐng)求

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

import <Foundation/Foundation.h>

@interface LoadData : NSObject
//分享單例對(duì)象

  • (instancetype)shareLoadData;
    //獲取數(shù)據(jù)
  • (void)getData:(NSDictionary *)dic;
    //定義block傳值
    @property (nonatomic,strong)void (^dataDic)(NSDictionary *dataDictionary);
    @end

.m文件

import "LoadData.h"

//定義靜態(tài)變量
static LoadData *ld = nil;
@implementation LoadData
//分享單例對(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;
    }
    //http://api.jisuapi.com/illegaladdr/coord?lat=31.3004&lng=121.484&range=1000&num=10&appkey=de394933e1a3e2db
    //獲取數(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];
    NSLog(@"=============%@",dataDic);
    //block傳值
    dispatch_async(dispatch_get_main_queue(), ^{
    self.dataDic(dataDic);
    });
    }];

    //開始請(qǐng)求
    [task resume];
    }
    @end

//#import "ViewController.m"

import "ViewController.h"

import "LoadData.h"

import "DetailViewController.h"

import <MapKit/MapKit.h>

import <CoreLocation/CoreLocation.h>

@interface ViewController ()<CLLocationManagerDelegate>
{
//定義變量地圖視圖非洲、定位對(duì)象鸭限、當(dāng)前位置
MKMapView *mv;
CLLocationManager *lm;
CLLocation *loc;
}
@end

@implementation ViewController

  • (void)viewDidLoad {
    [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<CLLocation *> *)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)頁(yè)面.h

import <UIKit/UIKit.h>

import <CoreLocation/CoreLocation.h>

@interface DetailViewController : UIViewController
//定義屬性當(dāng)前位置
@property (nonatomic,strong)CLLocation *loc;
@end

//跳轉(zhuǎn)頁(yè)面.m

import "DetailViewController.h"

import "LoadData.h"

@interface DetailViewController ()<UITableViewDataSource,UITableViewDelegate>
{
//定義變量數(shù)據(jù)字典、表格
NSArray *arr;
UITableView *table;
}
@end

@implementation DetailViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    //設(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"];

      NSLog(@"-----------%@",arr);
      
      //更新表格
      [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閱讀 212,542評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異帕识,居然都是意外死亡泛粹,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,596評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門肮疗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)晶姊,“玉大人,你說(shuō)我怎么就攤上這事伪货∶茄茫” “怎么了?”我有些...
    開封第一講書人閱讀 158,021評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵碱呼,是天一觀的道長(zhǎng)蒙挑。 經(jīng)常有香客問我,道長(zhǎng)巍举,這世上最難降的妖魔是什么脆荷? 我笑而不...
    開封第一講書人閱讀 56,682評(píng)論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮懊悯,結(jié)果婚禮上蜓谋,老公的妹妹穿的比我還像新娘。我一直安慰自己炭分,他們只是感情好桃焕,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,792評(píng)論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著捧毛,像睡著了一般观堂。 火紅的嫁衣襯著肌膚如雪让网。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,985評(píng)論 1 291
  • 那天师痕,我揣著相機(jī)與錄音溃睹,去河邊找鬼。 笑死胰坟,一個(gè)胖子當(dāng)著我的面吹牛因篇,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播笔横,決...
    沈念sama閱讀 39,107評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼竞滓,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了吹缔?” 一聲冷哼從身側(cè)響起商佑,我...
    開封第一講書人閱讀 37,845評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎厢塘,沒想到半個(gè)月后茶没,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,299評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡晚碾,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,612評(píng)論 2 327
  • 正文 我和宋清朗相戀三年礁叔,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片迄薄。...
    茶點(diǎn)故事閱讀 38,747評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖煮岁,靈堂內(nèi)的尸體忽然破棺而出讥蔽,到底是詐尸還是另有隱情,我是刑警寧澤画机,帶...
    沈念sama閱讀 34,441評(píng)論 4 333
  • 正文 年R本政府宣布冶伞,位于F島的核電站,受9級(jí)特大地震影響步氏,放射性物質(zhì)發(fā)生泄漏响禽。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,072評(píng)論 3 317
  • 文/蒙蒙 一荚醒、第九天 我趴在偏房一處隱蔽的房頂上張望芋类。 院中可真熱鬧,春花似錦界阁、人聲如沸侯繁。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,828評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)贮竟。三九已至丽焊,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間咕别,已是汗流浹背技健。 一陣腳步聲響...
    開封第一講書人閱讀 32,069評(píng)論 1 267
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留惰拱,地道東北人雌贱。 一個(gè)月前我還...
    沈念sama閱讀 46,545評(píng)論 2 362
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像弓颈,于是被迫代替她去往敵國(guó)和親帽芽。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,658評(píng)論 2 350

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