- (void)viewDidLoad {
[super viewDidLoad];
if(self.userData.city.length < 2)
{
return;
}
self.mapView.delegate = self;
// CLGeocoder 解析地址
CLGeocoder *geoCoder = [CLGeocoder new];
// 地址解碼
[geoCoder geocodeAddressString:self.userData.city completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
self.placemark = placemarks[0];
// 插入大頭針
FLAnnotation *ann = [FLAnnotation new];
ann.coordinate = self.placemark.location.coordinate;
ann.title = self.userData.name;
ann.subtitle = self.userData.city;
ann.iconUrl = self.userData.iconUrl;
[self.mapView addAnnotation:ann];
self.mapView.showsUserLocation = YES;
}];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(getUserPosition:)];
// 點擊3下觸發(fā)
tap.numberOfTapsRequired = 3;
[self.mapView addGestureRecognizer:tap];
}
// 獲取用戶位置
- (void) getUserPosition:(UITapGestureRecognizer *)tap
{
CGPoint pt = [tap locationInView:tap.view];
// CLLocationCoordinate2D 枚舉類型, 經緯度
CLLocationCoordinate2D co = [self.mapView convertPoint:pt toCoordinateFromView:self.mapView];
CLGeocoder *geoCoder = [CLGeocoder new];
CLLocation *location = [[CLLocation alloc] initWithLatitude:co.latitude longitude:co.longitude];
[geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark * placemark = placemarks[0];
// 大頭貼的信息
MKPointAnnotation * ann = [MKPointAnnotation new];
ann.coordinate = co;
ann.title = [NSString stringWithFormat:@"%@ %@", placemark.country, placemark.administrativeArea];
ann.subtitle = placemark.name;
[self.mapView addAnnotation:ann];
}];
}
// 按照鼠標點擊的地方和需要放大的地方的中心點放大
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if(!self.updateUserLocation)
{
// 計算差距
MKCoordinateSpan span = MKCoordinateSpanMake(fabs(userLocation.coordinate.latitude-self.placemark.location.coordinate.latitude)+1, fabs(userLocation.coordinate.longitude-self.placemark.location.coordinate.longitude)+1);
// 中心
CLLocationCoordinate2D center = CLLocationCoordinate2DMake((userLocation.coordinate.latitude+self.placemark.location.coordinate.latitude)/2, (userLocation.coordinate.longitude+self.placemark.location.coordinate.longitude)/2);
// 范圍
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[self.mapView setRegion:region animated:YES];
self.updateUserLocation = YES;
}
}
#pragma mark -- 設置大頭貼
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if([annotation isKindOfClass:[FLAnnotation class]])
{
static NSString *annID = @"FL_ANN_ID";
MKAnnotationView *annVIew = [mapView dequeueReusableAnnotationViewWithIdentifier:annID];
if(!annVIew)
{
annVIew = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annID];
annVIew.canShowCallout = YES;
// 在 地理信息左側添加圖片
UIImageView *leftImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
annVIew.leftCalloutAccessoryView = leftImage;
UIImageView *rightImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
annVIew.rightCalloutAccessoryView = rightImage;
rightImage.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(navi2:)];
[rightImage addGestureRecognizer:tap];
}
// 給大頭針添加左右圖片
FLAnnotation *fla = (FLAnnotation *)annotation;
UIImageView *iconImageView = (UIImageView *)annVIew.leftCalloutAccessoryView;
[iconImageView sd_setImageWithURL:[NSURL URLWithString:fla.iconUrl]];
annVIew.image = [UIImage imageNamed:@"icon_marker"];
UIImageView * rightView = (UIImageView *)annVIew.rightCalloutAccessoryView;
objc_setAssociatedObject(rightView, &annStoreKey, fla, OBJC_ASSOCIATION_ASSIGN);
return annVIew;
}
//如果不是則調用系統(tǒng)的
else
{
return nil;
}
}
// 路程導航(從用戶位置到目的地)
- (void) navi2:(UITapGestureRecognizer *)gesture
{
FLAnnotation * ann = objc_getAssociatedObject(gesture.view, &annStoreKey);
MKMapItem * fromItem = [MKMapItem mapItemForCurrentLocation];
MKMapItem * toItem = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:ann.coordinate addressDictionary:nil]];
toItem.name = ann.title;
[MKMapItem openMapsWithItems:@[fromItem, toItem]
launchOptions:[NSDictionary dictionaryWithObjects:@[MKLaunchOptionsDirectionsModeDriving, @(YES)]
forKeys:@[MKLaunchOptionsDirectionsModeKey, MKLaunchOptionsShowsTrafficKey]]];
}
iOS-自定制 地圖大頭針
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門护奈,熙熙樓的掌柜王于貴愁眉苦臉地迎上來缔莲,“玉大人,你說我怎么就攤上這事霉旗〕兆啵” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵厌秒,是天一觀的道長读拆。 經常有香客問我,道長鸵闪,這世上最難降的妖魔是什么檐晕? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮蚌讼,結果婚禮上辟灰,老公的妹妹穿的比我還像新娘。我一直安慰自己篡石,他們只是感情好芥喇,可當我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著凰萨,像睡著了一般继控。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上胖眷,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼簿晓!你這毒婦竟也來了眶拉?” 一聲冷哼從身側響起千埃,我...
- 正文 年R本政府宣布续滋,位于F島的核電站,受9級特大地震影響孵奶,放射性物質發(fā)生泄漏疲酌。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一了袁、第九天 我趴在偏房一處隱蔽的房頂上張望朗恳。 院中可真熱鬧,春花似錦载绿、人聲如沸粥诫。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽臀脏。三九已至,卻和暖如春冀自,著一層夾襖步出監(jiān)牢的瞬間揉稚,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內容
- 根據(jù)項目需求猜拾,需要自定制起點、途經點以及終點的大頭針佣盒,下面看一下怎么去創(chuàng)建挎袜? 1.自定義MyPointAnnota...
- 要達到的效果圖 一.首先看下地圖中最基礎的大頭針的定制: 二.看下氣泡callout的自定制 最后在Viewcon...
- 前言學習地圖,我們必須要接觸兩個框架:Core Location,主要包含定位盯仪、地理編碼紊搪、反編碼功能,如需了解請移...
- ViewController.m(聲明文件中我沒有寫代碼) //// MyAnnotation.m// Add...
- 當驕陽不屑地甩開層層云霧的束縛露出它那高傲的面龐時全景,冷峻凝結成了藍軍隊長的特里臉上的表情耀石,憧憬充盈著他望向遠方的眼...