標記

選取平台: Android iOS JavaScript

標記是用來標出地圖上的某個位置。

根據預設,標記會使用標準圖示,搭配常見的 Google 地圖外觀, 感受。如要自訂標記,您可以變更 預設標記,或是使用自訂圖示取代標記圖片,或者變更其他 標記的屬性。

如要回應標記的點擊事件,您可以開啟資訊 視窗。資訊視窗會在對話方塊中顯示文字或圖片 標示位置。使用預設的資訊視窗來顯示文字,或建立 您自訂的資訊視窗,以完全控制其內容。

新增標記

如要新增標記,請建立一個包含 positionGMSMarker title,並設定其 map

以下範例說明如何在現有 GMSMapView 物件。標記建立於座標 10,10 處,顯示器則 「Hello World」字串在資訊視窗中按一下

Swift

let position = CLLocationCoordinate2D(latitude: 10, longitude: 10)
let marker = GMSMarker(position: position)
marker.title = "Hello World"
marker.map = mapView
      

Objective-C

CLLocationCoordinate2D position = CLLocationCoordinate2DMake(10, 10);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.title = @"Hello World";
marker.map = mapView;
      

您可以藉由設定 marker.appearAnimation 屬性即可:

  • kGMSMarkerAnimationPop 會導致標記從其 groundAnchor 彈出 。
  • kGMSMarkerAnimationFadeIn,讓標記在新增時淡入。

移除標記

如要移除地圖上的標記,您可以設定標記的 map 屬性 GMSMarkernil。您也可以移除所有疊加層 (包括標記) 目前在地圖上呼叫 GMSMapView clear 方法。

Swift

let camera = GMSCameraPosition.camera(
  withLatitude: -33.8683,
  longitude: 151.2086,
  zoom: 6
)
let mapView = GMSMapView.map(withFrame: .zero, camera: camera)
// ...
mapView.clear()
      

Objective-C

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.8683
                                                        longitude:151.2086
                                                             zoom:6];
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
// ...
[mapView clear];
      

如果要修改標記加到地圖之後,請 確保保留 GMSMarker 物件。您可以修改標記 變更這個物件

Swift

let position = CLLocationCoordinate2D(latitude: 10, longitude: 10)
let marker = GMSMarker(position: position)
marker.map = mapView
// ...
marker.map = nil
      

Objective-C

CLLocationCoordinate2D position = CLLocationCoordinate2DMake(10, 10);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.map = mapView;
// ...
marker.map = nil;
      

變更標記顏色

您可以要求加上色調來自訂預設標記圖片的顏色 使用 markerImageWithColor: 的預設圖示版本,並傳遞 產生的影像。GMSMarker 的圖示屬性。

Swift

marker.icon = GMSMarker.markerImage(with: .black)
      

Objective-C

marker.icon = [GMSMarker markerImageWithColor:[UIColor blackColor]];
      

自訂標記圖片

如要變更預設標記圖片,您可以設定自訂圖示,方法是使用 標記的 iconiconView 屬性。如果已設定 iconView,API 會忽略 icon 屬性。

使用標記的 icon 屬性

下方程式碼片段使用自訂圖示建立了一個標記, icon 屬性中的 UIImage。圖示以英國倫敦為中心。 程式碼片段假設應用程式包含一個名為「house.png」的圖片。

Swift

let positionLondon = CLLocationCoordinate2D(latitude: 51.5, longitude: -0.127)
let london = GMSMarker(position: positionLondon)
london.title = "London"
london.icon = UIImage(named: "house")
london.map = mapView
      

Objective-C

CLLocationCoordinate2D positionLondon = CLLocationCoordinate2DMake(51.5, -0.127);
GMSMarker *london = [GMSMarker markerWithPosition:positionLondon];
london.title = @"London";
london.icon = [UIImage imageNamed:@"house"];
london.map = mapView;
      

如果您要使用相同圖片建立多個標記,請使用同一個執行個體 每個標記的 UIImage 值。這樣不但能提高 顯示許多標記時的應用程式。

這張圖片可能包含多個影格。此外,alignmentRectInsets 屬性採用時,這特別適用於標記有陰影或其他 資源用量

使用標記的 iconView 屬性

下列程式碼片段透過設定 標記的 iconView 屬性,並加上標記顏色變化的動畫效果。 The snippet assumes that your application contains an image named "house.png".

Swift

import CoreLocation
import GoogleMaps

class MarkerViewController: UIViewController, GMSMapViewDelegate {
  var mapView: GMSMapView!
  var london: GMSMarker?
  var londonView: UIImageView?

  override func viewDidLoad() {
    super.viewDidLoad()

    let camera = GMSCameraPosition.camera(
      withLatitude: 51.5,
      longitude: -0.127,
      zoom: 14
    )
    let mapView = GMSMapView.map(withFrame: .zero, camera: camera)
    view = mapView

    mapView.delegate = self

    let house = UIImage(named: "House")!.withRenderingMode(.alwaysTemplate)
    let markerView = UIImageView(image: house)
    markerView.tintColor = .red
    londonView = markerView

    let position = CLLocationCoordinate2D(latitude: 51.5, longitude: -0.127)
    let marker = GMSMarker(position: position)
    marker.title = "London"
    marker.iconView = markerView
    marker.tracksViewChanges = true
    marker.map = mapView
    london = marker
  }

  func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
    UIView.animate(withDuration: 5.0, animations: { () -> Void in
      self.londonView?.tintColor = .blue
    }, completion: {(finished) in
      // Stop tracking view changes to allow CPU to idle.
      self.london?.tracksViewChanges = false
    })
  }
}
      

Objective-C

@import CoreLocation;
@import GoogleMaps;

@interface MarkerViewController : UIViewController <GMSMapViewDelegate>
@property (strong, nonatomic) GMSMapView *mapView;
@end

@implementation MarkerViewController {
  GMSMarker *_london;
  UIImageView *_londonView;
}

- (void)viewDidLoad {
  [super viewDidLoad];

  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:51.5
                                                          longitude:-0.127
                                                               zoom:14];
  _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  self.view = _mapView;

  _mapView.delegate = self;

  UIImage *house = [UIImage imageNamed:@"House"];
  house = [house imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
  _londonView = [[UIImageView alloc] initWithImage:house];
  _londonView.tintColor = [UIColor redColor];

  CLLocationCoordinate2D position = CLLocationCoordinate2DMake(51.5, -0.127);
  _london = [GMSMarker markerWithPosition:position];
  _london.title = @"London";
  _london.iconView = _londonView;
  _london.tracksViewChanges = YES;
  _london.map = self.mapView;
}

- (void)mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position {
  [UIView animateWithDuration:5.0
                   animations:^{
    self->_londonView.tintColor = [UIColor blueColor];
  }
                   completion:^(BOOL finished) {
    // Stop tracking view changes to allow CPU to idle.
    self->_london.tracksViewChanges = NO;
  }];
}

@end
      

因為 iconView 接受 UIView,所以您可以使用標準 UI 階層 定義標記的控制項,每個檢視畫面都有標準動畫組合 即便沒有技術背景,也能因這些工具的功能而受益您可以為標記大小、顏色和 Alpha 加入變更。 並套用任意轉換iconView 屬性 支援 UIView 所有可繪製屬性的動畫,但 framecenter

使用 iconView 的注意事項如下:

  • 設定 tracksViewChanges 時,UIView 可能會要求耗用資源 變更為 YES,裝置可能會比較耗電。相較之下, 單一影格 UIImage 為靜態,不需要重新轉譯。
  • 如果部分裝置中有許多標記,可能無法顯示地圖 而且每個標記都有專屬的 UIView,而所有標記都會追蹤 同時做出變更
  • iconView 不會回應使用者互動,因為是快照 檢視畫面。
  • 無論 clipsToBounds 設為 YES,無論 實際價值您可以套用在邊界外使用的轉換,但 您繪製的物件必須在物件的邊界內。所有語言 系統會監控並套用轉換/轉換。簡單來說,子觀看次數必須是 包含在檢視畫面中

建議量體重,以決定 tracksViewChanges 屬性的設定時機 重新繪製標記的好處與效能考量 。例如:

  • 如要進行一系列變更,可以將屬性變更為 YES,然後再返回 NO
  • 顯示動畫或正在載入內容時 而在操作之前,您應將屬性設為 YES。 。

變更標記的不透明度

您可以使用標記的 opacity 屬性控制標記的不透明度。請 指定不透明度,為 0.0 和 1.0 之間的浮動值,其中 0 為完全透明 而 1 表示完全不透明

Swift

marker.opacity = 0.6
      

Objective-C

marker.opacity = 0.6;
      

你可以使用 [核心動畫]Core 為標記的不透明度加上動畫效果 使用 GMSMarkerLayer 的動畫

固定標記

標記圖示通常是根據裝置螢幕繪製而來,而非 因此旋轉、傾斜或縮放地圖不一定 變更標記的方向。

你可以設置一個標記的方向是平貼在地上。平地 地圖旋轉時,標記會跟著旋轉,而當地圖採用 傾斜度。和一般標記一樣,平放標記在地圖 放大或縮小地圖。

如要變更標記的方向,請將標記的 flat 屬性設為 YEStrue

Swift

let positionLondon = CLLocationCoordinate2D(latitude: 51.5, longitude: -0.127)
let londonMarker = GMSMarker(position: positionLondon)
londonMarker.isFlat = true
londonMarker.map = mapView
      

Objective-C

CLLocationCoordinate2D positionLondon = CLLocationCoordinate2DMake(51.5, -0.127);
GMSMarker *londonMarker = [GMSMarker markerWithPosition:positionLondon];
londonMarker.flat = YES;
londonMarker.map = mapView;
      

旋轉標記

如要讓標記繞著錨點旋轉,請設定 rotation 資源。將旋轉指定為 CLLocationDegrees 類型,測量單位為 度。當標記平放在地圖上時, 預設為北方朝上

以下範例會將標記旋轉 90°。設定 groundAnchor 屬性在 0.5,0.5 中會使標記繞著其中心旋轉 與這個模型的結構有關

Swift

let degrees = 90.0
londonMarker.groundAnchor = CGPoint(x: 0.5, y: 0.5)
londonMarker.rotation = degrees
londonMarker.map = mapView
      

Objective-C

CLLocationDegrees degrees = 90;
londonMarker.groundAnchor = CGPointMake(0.5, 0.5);
londonMarker.rotation = degrees;
londonMarker.map = mapView;
      

處理標記的事件

您可以監聽地圖上發生的事件,例如使用者輕觸 標記。若要監聽事件,您必須實作 GMSMapViewDelegate 通訊協定。請參閱標記事件和 手勢:瞭解如何處理 特定的標記事件事件指南也提供 GMSMapViewDelegate 方法。如需街景服務事件,請參閱: GMSPanoramaViewDelegate