ios地圖錨點(diǎn)

1.話(huà)不多說(shuō),效果

Untitled2.gif

2.注意點(diǎn)

跟大家多嘮叨一下,設(shè)置錨點(diǎn)的意義就是可以進(jìn)行詳情展示,大家可以設(shè)置錨點(diǎn)的樣式。效果中的樣式是一張圖片,沒(méi)有圖片顯示的就是我們俗稱(chēng)的大頭針。
錨點(diǎn).gif

3.代碼展示

//地理編碼
@property(nonatomic,strong)CLGeocoder *geocoder;
self.geocoder = [[CLGeocoder alloc]init];
    
//viewDidLoad
    //設(shè)置地圖的顯示風(fēng)格
    self.mapView.mapType = MKMapTypeStandard;
    //設(shè)置地圖可縮放
    self.mapView.zoomEnabled = YES;
    //設(shè)置地圖可滾動(dòng)
    self.mapView.scrollEnabled = YES;
    //設(shè)置地圖可旋轉(zhuǎn)
    self.mapView.rotateEnabled = YES;
    //設(shè)置顯示用戶(hù)當(dāng)前位置
    self.mapView.showsUserLocation = YES;
    //為mapView設(shè)置代理
    self.mapView.delegate = self;
    
    [self locateTolatitude:23.126272 longitude:113.395568];
    
    //創(chuàng)建一個(gè)手勢(shì)處理器,用于檢測(cè),處理長(zhǎng)按手勢(shì)
    //長(zhǎng)按手勢(shì)識(shí)別器
    UILongPressGestureRecognizer *longPressGes = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
    
    //為該控件添加手勢(shì)
    [self.view addGestureRecognizer:longPressGes];
#pragma mark - 手勢(shì)回調(diào)
-(void)longPress:(UILongPressGestureRecognizer *)gestu
{
    //獲取長(zhǎng)按點(diǎn)的坐標(biāo)
    CGPoint pos = [gestu locationInView:self.mapView];
    
    //把獲取到的坐標(biāo)轉(zhuǎn)換成經(jīng)度緯度 值
    CLLocationCoordinate2D coor2D = [self.mapView convertPoint:pos toCoordinateFromView:self.mapView];
    //再把經(jīng)緯度值添加到CLLocation(定位)對(duì)象
    CLLocation *location = [[CLLocation alloc]initWithLatitude:coor2D.latitude longitude:coor2D.longitude];
    
    //根據(jù)經(jīng)緯度解析地址(反向解析)
    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
       
        if (placemarks.count > 0) {
            CLPlacemark *placmark = placemarks[0];
            
            NSArray *addrArray = [placmark.addressDictionary objectForKey:@"FormattedAddressLines"];
            //將詳細(xì)地址拼接成一個(gè)字符串
            NSMutableString *address = [[NSMutableString alloc]init];
            for (int i = 0 ; i < addrArray.count ; i++) {
                [address appendString:addrArray[i]];
            }
            //創(chuàng)建MKPointAnnotation對(duì)象---代表一個(gè)錨點(diǎn)
            MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
            //設(shè)置標(biāo)題
            annotation.title = placmark.name;
            //設(shè)置子標(biāo)題
            annotation.subtitle = address;
            //把坐標(biāo)放入錨點(diǎn)里面
            annotation.coordinate = coor2D;
            //添加錨點(diǎn)
            [self.mapView addAnnotation:annotation];
        }
        
    }];
   
    
}
- (IBAction)gogogo:(id)sender {
    //關(guān)閉兩個(gè)文本框的虛擬鍵盤(pán)
    [self.weidu resignFirstResponder];
    [self.jingdu resignFirstResponder];
    NSString* latitudeStr = self.weidu.text;
    NSString* longtitudeStr = self.jingdu.text;
    //如果用戶(hù)輸入的經(jīng)緯度不為空
    if (latitudeStr != nil && latitudeStr.length > 0
        && longtitudeStr != nil && longtitudeStr.length > 0)
    {
        // 調(diào)用自己實(shí)現(xiàn)的方法設(shè)置地圖的顯示位置和顯示區(qū)域
        [self locateTolatitude:latitudeStr.floatValue
                     longitude:longtitudeStr.floatValue];
    }
}
-(void)locateTolatitude:(CGFloat)latitude longitude:(CGFloat)longitude
{
    CLLocationCoordinate2D coor2D = {latitude,longitude};
    
        //設(shè)置地圖的顯示范圍
        MKCoordinateSpan span;
        //地圖顯示范圍越小,越清楚
        span.latitudeDelta = 0.01;
        span.longitudeDelta = 0.01;
        // 創(chuàng)建MKCoordinateRegion對(duì)象,該對(duì)象代表了地圖的顯示中心和顯示范圍。
        MKCoordinateRegion region = {coor2D,span};
        // 設(shè)置當(dāng)前地圖的顯示中心和顯示范圍
        [self.mapView setRegion:region animated:YES];
    //將用戶(hù)輸入的經(jīng)緯度封裝成CLLocation對(duì)象
    CLLocation *location = [[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
    
    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        //如果解析結(jié)果的集合元素大于1,表明解析得到了經(jīng)度,維度信息
        if (placemarks.count > 0) {
            CLPlacemark *placmark = placemarks[0];
            NSLog(@"===%@",placmark.addressDictionary);
            //獲取詳細(xì)地址信息
            NSArray *addArr = [placmark.addressDictionary objectForKey:@"FormattedAddressLines"];
            //將詳細(xì)地址拼接成一個(gè)字符串
            NSMutableString *addr = [[NSMutableString alloc]init];
            for (int i = 0; i < addArr.count; i ++) {
                [addr appendString:addArr[i]];
            }
            //創(chuàng)建MKPointAnnotation對(duì)象---代表一個(gè)錨點(diǎn)
            MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
            //設(shè)置標(biāo)題
            annotation.title = placmark.name;
            //設(shè)置子標(biāo)題
            annotation.subtitle = addr;
            //把坐標(biāo)放入錨點(diǎn)里面
            annotation.coordinate = coor2D;
            //添加錨點(diǎn)
            [self.mapView addAnnotation:annotation];

            
        }
        
    }];

    
    
}

#pragma mark -MKMapViewDelegate(地圖代理)
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    static NSString *annoId = @"fkAnno";
    //獲取可重用的錨點(diǎn)控件
    MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:annoId];
    if (!annoView) {
        
        annoView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annoId];
    }
    /*
     如果不想改變錨點(diǎn)控件的圖片,只想改變顏色,則可創(chuàng)建MKPinAnnotationView實(shí)例
     再修改MKPinAnnotationView對(duì)象的pinColor屬性即可。
     */
    annoView.image = [UIImage imageNamed:@"錨點(diǎn).gif"];
     // 設(shè)置該錨點(diǎn)控件是否可顯示氣泡信息
    annoView.canShowCallout = YES;
     // 定義一個(gè)按鈕,用于為錨點(diǎn)控件設(shè)置附加控件
    //UIButtonTypeDetailDisclosure 詳細(xì)說(shuō)明藍(lán)色小箭頭按鈕
    UIButton *b = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [b addTarget:self action:@selector(butomTape:) forControlEvents:UIControlEventTouchUpInside];
     // 可通過(guò)錨點(diǎn)控件的rightCalloutAccessoryView、leftCalloutAccessoryView設(shè)置附加控件
    annoView.rightCalloutAccessoryView = b;
    
    return annoView;
}
-(void)butomTape:(UIButton *)sender

{
    NSLog(@"您點(diǎn)擊了錨點(diǎn)信息!");
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容