IOS通過長按手勢放置一個大頭針---by talent.L

#import "ViewController.h"

#import <MapKit/MapKit.h>

@interface ViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>

{? //地圖

MKMapView *mapView;

}

//地理管理器

@property (nonatomic,strong)CLLocationManager * manage;

@end

@implementation ViewController

//地理管理器

-(CLLocationManager *)manage{

if (_manage==nil) {

_manage =[[CLLocationManager alloc]init];

_manage.delegate =self;

//多遠(yuǎn)更新一次

_manage.distanceFilter =100;

//設(shè)置誤差

_manage.desiredAccuracy = kCLLocationAccuracyBest;

//設(shè)置地圖在條件允許下的情況自動停止定位(省電模式)

_manage.pausesLocationUpdatesAutomatically =YES;

}

return _manage;

}

- (void)viewDidLoad {

[super viewDidLoad];

//導(dǎo)入三個文件

//1.corelocation? ? ? 2.coregraphics? ? ? ? 3.MapKit

//? info.plist改 ? Privacy - Location Always Usage Description

//授權(quán)可以定位 總是允許

[self.manage requestAlwaysAuthorization];

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

//設(shè)置類型

mapView.mapType =MKMapTypeStandard;

//可旋轉(zhuǎn)

mapView.rotateEnabled =YES;

//可傾斜

mapView.pitchEnabled =YES;

//可捏合

mapView.zoomEnabled =YES;

//可滾動

mapView.scrollEnabled=YES;

//可見自己位置

mapView.showsUserLocation =YES;

//設(shè)置代理

mapView.delegate =self;

//跟蹤用戶方向

mapView.userTrackingMode =MKUserTrackingModeFollowWithHeading;

//添加父視圖

[self.view addSubview:mapView];

//設(shè)置長按手勢

UILongPressGestureRecognizer * longpress =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(click:)];

[mapView addGestureRecognizer:longpress];

}

-(void)click:(UILongPressGestureRecognizer *)reco

{

[self.manage startUpdatingLocation];

// 獲得手勢點(diǎn)擊的坐標(biāo)

CGPoint point = [reco locationInView:self.myMapView];

// 把坐標(biāo)點(diǎn)轉(zhuǎn)換為經(jīng)緯度信息

CLLocationCoordinate2D coor = [self.myMapView convertPoint:point toCoordinateFromView:self.myMapView];

NSLog(@"緯度:%g,經(jīng)度:%g",coor.latitude,coor.longitude);

// 添加錨點(diǎn)

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

pointAnno.coordinate = coor;

pointAnno.title = [NSString stringWithFormat:@"緯度:%g,經(jīng)度:%g",coor.latitude,coor.longitude];

// 根據(jù)經(jīng)緯度解析為地址字符串()

CLGeocoder *geocoder = [[CLGeocoder alloc] init];

CLLocation *location = [[CLLocation alloc] initWithLatitude:coor.latitude longitude:coor.longitude];

[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

CLPlacemark *placeMark = [placemarks lastObject];

pointAnno.subtitle = placeMark.name;

dispatch_async(dispatch_get_main_queue(), ^{

[self.myMapView addAnnotation:pointAnno];

});

}];

}

//代理方法 更新位置自動回調(diào)- (void)locationManager:(CLLocationManager *)manager? ? didUpdateLocations:(NSArray*)locations __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0){

//獲得地理中最后一個地理數(shù)據(jù)位置的經(jīng)緯度

CLLocationCoordinate2D coor = [locations lastObject].coordinate;

//設(shè)置顯示范圍精度

MKCoordinateSpan span;

span.latitudeDelta = 0.01;

span.longitudeDelta = 0.01;

//設(shè)置地區(qū)

MKCoordinateRegion? region = {coor,span};

//設(shè)置該位置為地圖可顯示位置

[mapView regionThatFits:region];

[mapView setRegion:region animated:YES];

}


//如果拖拽 往設(shè)置的經(jīng)緯度上放置大頭針

- (IBAction)aa:(id)sender {

[self.manage startUpdatingLocation];

// 把坐標(biāo)點(diǎn)轉(zhuǎn)換為經(jīng)緯度信息

CLLocationCoordinate2D coor = {[self.mytext1.text floatValue],[self.mytext2.text floatValue]};

MKCoordinateSpan span;

span.latitudeDelta = 0.01;

span.longitudeDelta = 0.01;

MKCoordinateRegion region = {coor,span};

// 添加錨點(diǎn)

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

pointAnno.coordinate = coor;

pointAnno.title = [NSString stringWithFormat:@"緯度:%g,經(jīng)度:%g",coor.latitude,coor.longitude];

// 根據(jù)經(jīng)緯度解析為地址字符串()

CLGeocoder *geocoder = [[CLGeocoder alloc] init];

CLLocation *location = [[CLLocation alloc] initWithLatitude:coor.latitude longitude:coor.longitude];

[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

CLPlacemark *placeMark = [placemarks lastObject];

pointAnno.subtitle = placeMark.name;

dispatch_async(dispatch_get_main_queue(), ^{

[self.mymapView addAnnotation:pointAnno];

});

}];

[self.mymapView regionThatFits:region];

[self.mymapView setRegion:region];

}

//解析反解析

//解析- (IBAction)a:(id)sender {? ?

//授權(quán)可以定位 總是允許??

[self.manage requestAlwaysAuthorization];? ? ? ??

[self.geocoder geocodeAddressString:_mytext.text completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error) {? ??

? // 只處理第一個解析結(jié)果炭懊,實(shí)際項(xiàng)目中可使用列表讓用戶選擇? ?

? ? CLPlacemark *place = placemarks[0];? ??

? // 獲得這個地址的位置信息? ?

? ? CLLocation *location = place.location;??

? ? //? 獲得經(jīng)緯度信息? ?

? ? CLLocationCoordinate2D coor = location.coordinate;??

? ? self.mytextView.text =[NSString stringWithFormat:@"%f,%f",coor.latitude,coor.longitude];? ? ? ? ? ? }];? ? }

//反解析- (IBAction)aaa:(id)sender {??

//授權(quán)可以定位 總是允許

? [self.manage requestAlwaysAuthorization];? ?

//將長按點(diǎn)的坐標(biāo)轉(zhuǎn)換為經(jīng)度僻造、緯度值? ??

? CLLocationCoordinate2D coord = {[self.mytextwei.text floatValue],[self.mytextjing.text floatValue]};? ? ??

//將經(jīng)度緯度值包裝為CLLocation對象? ??

? CLLocation *location=[[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];? ? ?

? //根據(jù)經(jīng)緯度反向解析地址??

? ? [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error) {

//獲取解析得到的第一個地址信息

CLPlacemark *placemark =[placemarks lastObject];

//獲取地址信息中的FromattedAddressLines對應(yīng)的詳細(xì)地址

//

//? ? ? ? NSArray *addArray=placemark.addressDictionary[@"formattedAddressLines"];

//

//? ? ? ? //將詳細(xì)地址拼接成一個字符串

//

//? ? ? ? NSMutableString *address=[[NSMutableString alloc] init];

//

//? ? ? ? for (int i = 0; i < addArray.count ; i ++) {

//

//? ? ? ? ? ? [address appendString:addArray[i]];

//

//? ? ? ? }

//? ? ? ? NSLog(@"11====================%@",address);

//? ? 副標(biāo)題? annotation.subtitle = address;

self.mytextView.text = [NSString stringWithFormat:@"%@%@%@",placemark.country,placemark.administrativeArea,placemark.name];

}] ;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末捧灰,一起剝皮案震驚了整個濱河市击罪,隨后出現(xiàn)的幾起案子乓旗,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,744評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異爸舒,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)稿蹲,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評論 3 392
  • 文/潘曉璐 我一進(jìn)店門扭勉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人苛聘,你說我怎么就攤上這事涂炎≈揖郏” “怎么了?”我有些...
    開封第一講書人閱讀 163,105評論 0 353
  • 文/不壞的土叔 我叫張陵唱捣,是天一觀的道長两蟀。 經(jīng)常有香客問我,道長震缭,這世上最難降的妖魔是什么赂毯? 我笑而不...
    開封第一講書人閱讀 58,242評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮拣宰,結(jié)果婚禮上党涕,老公的妹妹穿的比我還像新娘。我一直安慰自己巡社,他們只是感情好膛堤,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,269評論 6 389
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著重贺,像睡著了一般骑祟。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上气笙,一...
    開封第一講書人閱讀 51,215評論 1 299
  • 那天次企,我揣著相機(jī)與錄音,去河邊找鬼潜圃。 笑死缸棵,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的谭期。 我是一名探鬼主播堵第,決...
    沈念sama閱讀 40,096評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼隧出!你這毒婦竟也來了踏志?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,939評論 0 274
  • 序言:老撾萬榮一對情侶失蹤胀瞪,失蹤者是張志新(化名)和其女友劉穎针余,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體凄诞,經(jīng)...
    沈念sama閱讀 45,354評論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡圆雁,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,573評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了帆谍。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片伪朽。...
    茶點(diǎn)故事閱讀 39,745評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖汛蝙,靈堂內(nèi)的尸體忽然破棺而出烈涮,到底是詐尸還是另有隱情朴肺,我是刑警寧澤,帶...
    沈念sama閱讀 35,448評論 5 344
  • 正文 年R本政府宣布跃脊,位于F島的核電站宇挫,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏酪术。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,048評論 3 327
  • 文/蒙蒙 一翠储、第九天 我趴在偏房一處隱蔽的房頂上張望绘雁。 院中可真熱鬧,春花似錦援所、人聲如沸庐舟。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,683評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽挪略。三九已至,卻和暖如春滔岳,著一層夾襖步出監(jiān)牢的瞬間杠娱,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,838評論 1 269
  • 我被黑心中介騙來泰國打工谱煤, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留摊求,地道東北人。 一個月前我還...
    沈念sama閱讀 47,776評論 2 369
  • 正文 我出身青樓刘离,卻偏偏與公主長得像室叉,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子硫惕,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,652評論 2 354

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