標記

選取平台: Android iOS JavaScript

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

標記預設會使用標準 Google 地圖的外觀和風格圖示。如要自訂標記,您可以變更預設標記的顏色、以自訂圖示取代標記圖片,或是變更標記的其他屬性。

為回應標記的點擊事件,您可以開啟資訊視窗。資訊視窗會在標記上方的彈出式視窗中顯示文字或圖片。您可以使用預設資訊視窗來顯示文字,也可以建立自訂資訊視窗來完全控制內容。

新增標記

如要新增標記,請建立包含 positiontitleGMSMarker 物件,並設定其 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:加入標記時,標記會淡入。

移除標記

如要將標記從地圖中移除,您可以將 GMSMarkermap 屬性設為 nil。或者,您也可以呼叫 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 屬性。只要您設定了 iconView,系統則無法辨識目前 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 設為 YES 時,UIView 可能會需要資源,但可能會導致耗電量增加。相較之下,單一影格 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;
      

您可以透過 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