簡體   English   中英

在運行時更改標記圖標

[英]Change marker icon during runtime

有沒有辦法在運行時更改 Google Maps Android API v2 標記的圖標,而無需刪除/重新添加我想更改其圖標的標記? 我可以對其應用轉換(如旋轉)嗎?

謝謝。

現在,我可以在更新到Google Play Services Rev 7后輕松地在運行時更改標記圖標

Marker.setIcon(BitmapDescriptor圖標)

是可用的,以前我刪除並添加標記來改變它的顏色。

目前,您無法在運行時更改標記,也不能對其應用旋轉。

您可以使用變通方法 - 我正在使用BlinkingMarker類,我必須在運行時調整標記圖像的不透明度。

現在唯一的解決方案是創建具有不同旋轉的位圖,然后定期添加/刪除它們。 此解決方案的問題是添加/刪除標記需要大量內存分配,因此會導致不斷的垃圾回收。 更好,更順暢的解決方法是預先創建所有圖像,並立即將所有圖像添加到地圖中。 之后,您可以使用Marker.setVisible(boolean)函數來顯示您當前需要的那個。

注意:在執行此操作之前測量位圖,因為添加大量大位圖會導致應用程序的內存大小增大。

您可以在此處查看我的解決方法: https//github.com/balazsbalazs/blinking-marker-mapsv2

這是閃爍的標記(更改位圖的不透明度),但在相同的行上,您可以應用任何類型的轉換。

文件在這個問題上非常明確 -

 Icon A bitmap that's displayed in place of the default marker image. You can't change the icon once you've created the marker. 

如果您想要更改標記,您可以選擇一些選項。 一個是,正如您所注意到的,刪除標記並添加另一個標記。 另一種是在同一位置放置多個標記,並在任何給定時間切換哪一個可見。

我可以對其應用變換(如旋轉)嗎?

使用圖像創建標記之前,您可以將任何您喜歡的變換應用於圖像。

20139月的Google Maps Android API v2版本中 ,現在有一個新的標記setRotation()方法,以及一個新的setFlat()方法。

這是Google對新功能的描述:

給你的標記一個方向感

我們添加了一個標記旋轉屬性,允許您圍繞它的錨點旋轉標記。 新的平面屬性允許您使標記平放在地圖表面上,而不是彈出以面向相機。 當地圖旋轉或傾斜時,這兩個新屬性對於指示羅盤方向特別有用。

是一篇描述新功能的文章

要在運行時更改選定的標記圖標,只需添加

      @Override
      public boolean onMarkerClick(Marker marker) {

       Marker.setIcon (BitmapDescriptor icon);
       }

僅針對 Android 進行了測試

為了在運行時更改標記的圖標,我通過刪除第 186 行中的“final”鍵將圖標變量從最終更改為非最終

final BitmapDescriptor icon;

BitmapDescriptor icon;

並將第 136 行中的標記構造函數從“const”更改為“non-const”:

const Marker({

Marker({

要在包中進行更改,您必須按照此說明進行操作

使用 google_maps_flutter 存儲庫,您將收到類似“找不到 pubspec.yaml”的錯誤消息。 那是因為 google_maps_flutter 包放置在存儲庫的子目錄中。

要獲得正確的依賴項,您必須在 pubspec.yaml 中添加包的特定路徑:

 dependencies:
  # google_maps_flutter: ^2.1.1
  google_maps_flutter:
    git:
      url: https://github.com/[username]/plugins.git
      path: packages/google_maps_flutter/google_maps_flutter/

然后您可以通過直接將其設置在標記上來更改圖標。 我通過使用“OnTap”方法來做到這一點。

Marker marker = Marker(
  icon: firstIcon,
  markerId: MarkerId(id),
  position: position,
  infoWindow: InfoWindow(
    title: name,
    snippet: address,
  ),
  onTap: () {
    setState(() {
      _markers[name]?.icon = secondIcon;
    });
  }
);

整個 Marker 類:

@immutable
class Marker implements MapsObject {
  /// Creates a set of marker configuration options.
  ///
  /// Default marker options.
  ///
  /// Specifies a marker that
  /// * is fully opaque; [alpha] is 1.0
  /// * uses icon bottom center to indicate map position; [anchor] is (0.5, 1.0)
  /// * has default tap handling; [consumeTapEvents] is false
  /// * is stationary; [draggable] is false
  /// * is drawn against the screen, not the map; [flat] is false
  /// * has a default icon; [icon] is `BitmapDescriptor.defaultMarker`
  /// * anchors the info window at top center; [infoWindowAnchor] is (0.5, 0.0)
  /// * has no info window text; [infoWindowText] is `InfoWindowText.noText`
  /// * is positioned at 0, 0; [position] is `LatLng(0.0, 0.0)`
  /// * has an axis-aligned icon; [rotation] is 0.0
  /// * is visible; [visible] is true
  /// * is placed at the base of the drawing order; [zIndex] is 0.0
  /// * reports [onTap] events
  /// * reports [onDragEnd] events
  Marker({
    required this.markerId,
    this.alpha = 1.0,
    this.anchor = const Offset(0.5, 1.0),
    this.consumeTapEvents = false,
    this.draggable = false,
    this.flat = false,
    this.icon = BitmapDescriptor.defaultMarker,
    this.infoWindow = InfoWindow.noText,
    this.position = const LatLng(0.0, 0.0),
    this.rotation = 0.0,
    this.visible = true,
    this.zIndex = 0.0,
    this.onTap,
    this.onDrag,
    this.onDragStart,
    this.onDragEnd,
  }) : assert(alpha == null || (0.0 <= alpha && alpha <= 1.0));

  /// Uniquely identifies a [Marker].
  final MarkerId markerId;

  @override
  MarkerId get mapsId => markerId;

  /// The opacity of the marker, between 0.0 and 1.0 inclusive.
  ///
  /// 0.0 means fully transparent, 1.0 means fully opaque.
  final double alpha;

  /// The icon image point that will be placed at the [position] of the marker.
  ///
  /// The image point is specified in normalized coordinates: An anchor of
  /// (0.0, 0.0) means the top left corner of the image. An anchor
  /// of (1.0, 1.0) means the bottom right corner of the image.
  final Offset anchor;

  /// True if the marker icon consumes tap events. If not, the map will perform
  /// default tap handling by centering the map on the marker and displaying its
  /// info window.
  final bool consumeTapEvents;

  /// True if the marker is draggable by user touch events.
  final bool draggable;

  /// True if the marker is rendered flatly against the surface of the Earth, so
  /// that it will rotate and tilt along with map camera movements.
  final bool flat;

  /// A description of the bitmap used to draw the marker icon.
  BitmapDescriptor icon;

  /// A Google Maps InfoWindow.
  ///
  /// The window is displayed when the marker is tapped.
  final InfoWindow infoWindow;

  /// Geographical location of the marker.
  final LatLng position;

  /// Rotation of the marker image in degrees clockwise from the [anchor] point.
  final double rotation;

  /// True if the marker is visible.
  final bool visible;

  /// The z-index of the marker, used to determine relative drawing order of
  /// map overlays.
  ///
  /// Overlays are drawn in order of z-index, so that lower values means drawn
  /// earlier, and thus appearing to be closer to the surface of the Earth.
  final double zIndex;

  /// Callbacks to receive tap events for markers placed on this map.
  final VoidCallback? onTap;

  /// Signature reporting the new [LatLng] at the start of a drag event.
  final ValueChanged<LatLng>? onDragStart;

  /// Signature reporting the new [LatLng] at the end of a drag event.
  final ValueChanged<LatLng>? onDragEnd;

  /// Signature reporting the new [LatLng] during the drag event.
  final ValueChanged<LatLng>? onDrag;

  /// Creates a new [Marker] object whose values are the same as this instance,
  /// unless overwritten by the specified parameters.
  Marker copyWith({
    double? alphaParam,
    Offset? anchorParam,
    bool? consumeTapEventsParam,
    bool? draggableParam,
    bool? flatParam,
    BitmapDescriptor? iconParam,
    InfoWindow? infoWindowParam,
    LatLng? positionParam,
    double? rotationParam,
    bool? visibleParam,
    double? zIndexParam,
    VoidCallback? onTapParam,
    ValueChanged<LatLng>? onDragStartParam,
    ValueChanged<LatLng>? onDragParam,
    ValueChanged<LatLng>? onDragEndParam,
  }) {
    return Marker(
      markerId: markerId,
      alpha: alphaParam ?? alpha,
      anchor: anchorParam ?? anchor,
      consumeTapEvents: consumeTapEventsParam ?? consumeTapEvents,
      draggable: draggableParam ?? draggable,
      flat: flatParam ?? flat,
      icon: iconParam ?? icon,
      infoWindow: infoWindowParam ?? infoWindow,
      position: positionParam ?? position,
      rotation: rotationParam ?? rotation,
      visible: visibleParam ?? visible,
      zIndex: zIndexParam ?? zIndex,
      onTap: onTapParam ?? onTap,
      onDragStart: onDragStartParam ?? onDragStart,
      onDrag: onDragParam ?? onDrag,
      onDragEnd: onDragEndParam ?? onDragEnd,
    );
  }

  /// Creates a new [Marker] object whose values are the same as this instance.
  Marker clone() => copyWith();

  /// Converts this object to something serializable in JSON.
  Object toJson() {
    final Map<String, Object> json = <String, Object>{};

    void addIfPresent(String fieldName, Object? value) {
      if (value != null) {
        json[fieldName] = value;
      }
    }

    addIfPresent('markerId', markerId.value);
    addIfPresent('alpha', alpha);
    addIfPresent('anchor', _offsetToJson(anchor));
    addIfPresent('consumeTapEvents', consumeTapEvents);
    addIfPresent('draggable', draggable);
    addIfPresent('flat', flat);
    addIfPresent('icon', icon.toJson());
    addIfPresent('infoWindow', infoWindow._toJson());
    addIfPresent('position', position.toJson());
    addIfPresent('rotation', rotation);
    addIfPresent('visible', visible);
    addIfPresent('zIndex', zIndex);
    return json;
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    if (other.runtimeType != runtimeType) return false;
    final Marker typedOther = other as Marker;
    return markerId == typedOther.markerId &&
        alpha == typedOther.alpha &&
        anchor == typedOther.anchor &&
        consumeTapEvents == typedOther.consumeTapEvents &&
        draggable == typedOther.draggable &&
        flat == typedOther.flat &&
        icon == typedOther.icon &&
        infoWindow == typedOther.infoWindow &&
        position == typedOther.position &&
        rotation == typedOther.rotation &&
        visible == typedOther.visible &&
        zIndex == typedOther.zIndex;
  }

  @override
  int get hashCode => markerId.hashCode;

  @override
  String toString() {
    return 'Marker{markerId: $markerId, alpha: $alpha, anchor: $anchor, '
        'consumeTapEvents: $consumeTapEvents, draggable: $draggable, flat: $flat, '
        'icon: $icon, infoWindow: $infoWindow, position: $position, rotation: $rotation, '
        'visible: $visible, zIndex: $zIndex, onTap: $onTap, onDragStart: $onDragStart, '
        'onDrag: $onDrag, onDragEnd: $onDragEnd}';
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM