- (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:)];
// 點(diǎn)擊3下觸發(fā)
tap.numberOfTapsRequired = 3;
[self.mapView addGestureRecognizer:tap];
}
// 獲取用戶位置
- (void) getUserPosition:(UITapGestureRecognizer *)tap
{
CGPoint pt = [tap locationInView:tap.view];
// CLLocationCoordinate2D 枚舉類型, 經(jīng)緯度
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];
}];
}
// 按照鼠標(biāo)點(diǎn)擊的地方和需要放大的地方的中心點(diǎn)放大
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if(!self.updateUserLocation)
{
// 計(jì)算差距
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 -- 設(shè)置大頭貼
- (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;
// 在 地理信息左側(cè)添加圖片
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;
}
//如果不是則調(diào)用系統(tǒng)的
else
{
return nil;
}
}
// 路程導(dǎo)航(從用戶位置到目的地)
- (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-自定制 地圖大頭針
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 根據(jù)項(xiàng)目需求,需要自定制起點(diǎn)、途經(jīng)點(diǎn)以及終點(diǎn)的大頭針,下面看一下怎么去創(chuàng)建? 1.自定義MyPointAnnota...
- 要達(dá)到的效果圖 一.首先看下地圖中最基礎(chǔ)的大頭針的定制: 二.看下氣泡callout的自定制 最后在Viewcon...
- 前言學(xué)習(xí)地圖,我們必須要接觸兩個(gè)框架:Core Location,主要包含定位、地理編碼、反編碼功能,如需了解請移...
- ViewController.m(聲明文件中我沒有寫代碼) //// MyAnnotation.m// Add...
- 當(dāng)驕陽不屑地甩開層層云霧的束縛露出它那高傲的面龐時(shí),冷峻凝結(jié)成了藍(lán)軍隊(duì)長的特里臉上的表情,憧憬充盈著他望向遠(yuǎn)方的眼...