免費開源的iOS開發學習平臺

                                                MapKit框架:6-復雜地圖標注示例

                                                本節的示例代碼,在MKMapView上實現了添加自定義樣式的地圖標注,該地圖標注的圖標使用自定義圖片,點擊圖標后,可以在附加視圖的左右兩側各添加一個按鈕。

                                                準備工作

                                                在進行地圖標注樣式定制之前,需要提前完成如下代碼的編寫:

                                                • 初始化一個MKMapView類的對象,并設置屬性;
                                                • 初始化一個MKPointAnnotation類的對象,設置其屬性,并添加到MKMapView對象上;
                                                • 設置MKMapView對象的代理對象,通常為控制器對象,然后設置控制器對象遵守MKMapViewDelegate協議
                                                • 添加MKMapView到控制器視圖上。

                                                實現地圖標注的定制

                                                使用自定義樣式的地圖標注,需要在MKMapViewDelegate代理協議中的mapView:viewForAnnotation:方法中完成。

                                                - (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
                                                    
                                                    if ([annotation isKindOfClass:[MKPointAnnotation class]]) {
                                                        MKAnnotationView *customAnnotationView = (MKAnnotationView*)[mapView                                                                   dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
                                                        if (customAnnotationView == nil){
                                                            customAnnotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation                                                            reuseIdentifier:@"CustomPinAnnotationView"];
                                                        }
                                                        //設置標注的圖片
                                                        customAnnotationView.image = [UIImage imageNamed:@"99logo"];
                                                        //設置點擊標注可以顯示更多信息
                                                        customAnnotationView.canShowCallout = YES;
                                                        //右側按鈕定制
                                                        UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
                                                        rightButton.backgroundColor = [UIColor redColor];
                                                        [rightButton setTitle:@"前往" forState:UIControlStateNormal];
                                                        customAnnotationView.rightCalloutAccessoryView = rightButton;
                                                        //左側按鈕定制
                                                        UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
                                                        leftButton.backgroundColor = [UIColor blueColor];
                                                        [leftButton setTitle:@"詳情" forState:UIControlStateNormal];
                                                        customAnnotationView.leftCalloutAccessoryView = leftButton;
                                                        return customAnnotationView;
                                                    }
                                                    return nil;
                                                }
                                                

                                                運行后,我們可以看到地圖標注的樣式如下所示:

                                                示例代碼

                                                https://github.com/99ios/17.3.6


                                                欧美超级精品一级A片|欧美一级黄色视频|国产精品自在在线午夜免费|4444亚洲人成无码网在线观看|亚洲ⅴa曰本va欧美va视频