Podstawowe dostosowanie znacznika

Wybierz platformę: Android iOS JavaScript

Zaawansowane znaczniki używają 2 klas do ich definiowania: klasa AdvancedMarkerElement zawiera parametry podstawowe (position, title i map), a klasa PinElement zawiera opcje umożliwiające dalsze dostosowanie. Ten fragment kodu zawiera kod tworzący nowy obiekt PinElement, a następnie stosowany do znacznika.

// Create a pin element.
const pin = new PinElement({
    scale: 1.5,
});
// Create a marker and apply the element.
const marker = new AdvancedMarkerElement({
    map,
    position: { lat: 37.419, lng: -122.02 },
    content: pin.element,
});

W mapach utworzonych przy użyciu kodu HTML podstawowe parametry znacznika są deklarowane za pomocą elementu HTML gmp-advanced-marker. Wszelkie dostosowania korzystające z klasy PinElement muszą być stosowane automatycznie. W tym celu kod musi pobierać elementy gmp-advanced-marker ze strony HTML. Ten fragment kodu zawiera kod, który tworzy zapytanie o zbiór elementów gmp-advanced-marker, a następnie powtórz je, aby zastosować dostosowanie zadeklarowane w tabeli PinElement.

// Return an array of markers.
const advancedMarkers = [...document.querySelectorAll('gmp-advanced-marker')];

// Loop through the markers
for (let i = 0; i < advancedMarkers.length; i++) {
  const pin = new PinElement({
      scale: 2.0,
  });

  marker.appendChild(pin.element);
}

Na tej stronie znajdziesz następujące informacje na temat dostosowywania znaczników:

Schemat przedstawiający elementy znacznika zaawansowanego.
Rys. 1: Części znacznika zaawansowanego.

Dodaj tekst tytułu

Tekst tytułu pojawia się po najechaniu kursorem na znacznik. Tytuł jest czytelny dla czytników ekranu.

Aby dodać tekst tytułu automatycznie, użyj opcji AdvancedMarkerElement.title:


  
  
  
  
  
  

TypeScript

// Default marker with title text (no PinElement).
const markerViewWithText = new AdvancedMarkerElement({
    map,
    position: { lat: 37.419, lng: -122.03 },
    title: 'Title text for the marker at lat: 37.419, lng: -122.03',
});

JavaScript

// Default marker with title text (no PinElement).
const markerViewWithText = new AdvancedMarkerElement({
  map,
  position: { lat: 37.419, lng: -122.03 },
  title: "Title text for the marker at lat: 37.419, lng: -122.03",
});

Aby dodać tekst tytułu do znacznika utworzonego za pomocą kodu HTML, użyj atrybutu title:


  
  
  
  
<gmp-map
  center="43.4142989,-124.2301242"
  zoom="4"
  map-id="DEMO_MAP_ID"
  style="height: 400px"
>
  <gmp-advanced-marker
    position="37.4220656,-122.0840897"
    title="Mountain View, CA"
  ></gmp-advanced-marker>
  <gmp-advanced-marker
    position="47.648994,-122.3503845"
    title="Seattle, WA"
  ></gmp-advanced-marker>
</gmp-map>

Skaluj znacznik

Aby skalować znacznik, użyj opcji scale.

TypeScript

// Adjust the scale.
const pinScaled = new PinElement({
    scale: 1.5,
});
const markerViewScaled = new AdvancedMarkerElement({
    map,
    position: { lat: 37.419, lng: -122.02 },
    content: pinScaled.element,
});

JavaScript

// Adjust the scale.
const pinScaled = new PinElement({
  scale: 1.5,
});
const markerViewScaled = new AdvancedMarkerElement({
  map,
  position: { lat: 37.419, lng: -122.02 },
  content: pinScaled.element,
});

Zmienianie koloru tła

Aby zmienić kolor tła znacznika, użyj opcji PinElement.background:

TypeScript

// Change the background color.
const pinBackground = new PinElement({
    background: '#FBBC04',
});
const markerViewBackground = new AdvancedMarkerElement({
    map,
    position: { lat: 37.419, lng: -122.01 },
    content: pinBackground.element,
});

JavaScript

// Change the background color.
const pinBackground = new PinElement({
  background: "#FBBC04",
});
const markerViewBackground = new AdvancedMarkerElement({
  map,
  position: { lat: 37.419, lng: -122.01 },
  content: pinBackground.element,
});

Zmiana koloru obramowania

Aby zmienić kolor obramowania znacznika, użyj opcji PinElement.borderColor:

TypeScript

// Change the border color.
const pinBorder = new PinElement({
    borderColor: '#137333',
});
const markerViewBorder = new AdvancedMarkerElement({
    map,
    position: { lat: 37.415, lng: -122.03 },
    content: pinBorder.element,
});

JavaScript

// Change the border color.
const pinBorder = new PinElement({
  borderColor: "#137333",
});
const markerViewBorder = new AdvancedMarkerElement({
  map,
  position: { lat: 37.415, lng: -122.03 },
  content: pinBorder.element,
});

Zmienianie koloru glifu

Aby zmienić kolor glifu znacznika, użyj opcji PinElement.glyphColor:

TypeScript

// Change the glyph color.
const pinGlyph = new PinElement({
    glyphColor: 'white',
});
const markerViewGlyph = new AdvancedMarkerElement({
    map,
    position: { lat: 37.415, lng: -122.02 },
    content: pinGlyph.element,
});

JavaScript

// Change the glyph color.
const pinGlyph = new PinElement({
  glyphColor: "white",
});
const markerViewGlyph = new AdvancedMarkerElement({
  map,
  position: { lat: 37.415, lng: -122.02 },
  content: pinGlyph.element,
});

Ukryj glif

Aby ukryć glif znacznika, ustaw opcję PinElement.glyph na pusty ciąg:

TypeScript

// Hide the glyph.
const pinNoGlyph = new PinElement({
    glyph: '',
});
const markerViewNoGlyph = new AdvancedMarkerElement({
    map,
    position: { lat: 37.415, lng: -122.01 },
    content: pinNoGlyph.element,
});

JavaScript

// Hide the glyph.
const pinNoGlyph = new PinElement({
  glyph: "",
});
const markerViewNoGlyph = new AdvancedMarkerElement({
  map,
  position: { lat: 37.415, lng: -122.01 },
  content: pinNoGlyph.element,
});

Możesz też ustawić PinElement.glyphColor na tę samą wartość co PinElement.background. W efekcie glif jest ukrywany wizualnie.

Dalsze kroki: