免費開源的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


                                                              97人人超人人超碰超中文|免看一级a一级久久|国产熟睡乱子伦午夜|卡通动漫久久五月天 亚洲国产免费综合网日韩 亚洲日本中文字幕乱码在线 伊人久久大香线蕉av蜜芽 久久综合一香蕉老鬼色一个 91 中文字幕 国产 日韩 全彩3d啪啪无码本子全彩千乐网 亚洲中文字幕无码永久 三级三级久久三级 国产日产在线不卡省流观看