Do definiowania znaczników używane są 2 klasy: klasa AdvancedMarkerElement
udostępnia podstawowe parametry (position
, title
i map
), a klasa PinElement
zawiera opcje umożliwiające dalsze dostosowanie. Poniższy fragment kodu pokazuje kod, który pozwala utworzyć nowy element PinElement
, a następnie zastosować go 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 tworzonych za pomocą 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 pobrać elementy gmp-advanced-marker
ze strony HTML. Poniższy fragment kodu pokazuje kod, który wykonuje zapytanie dotyczące zbioru elementów gmp-advanced-marker
, a następnie powtórza wyniki, aby zastosować dostosowanie zadeklarowane w 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 opisujemy, jak dostosować znaczniki w następujący sposób:
- Dodawanie tekstu tytułu
- Przeskaluj znacznik
- Zmienianie koloru tła
- Zmienianie koloru obramowania
- Zmienianie koloru glifu
- Ukryj glif
Dodaj tekst tytułu
Tekst tytułu pojawia się po najechaniu kursorem na znacznik. Tytuł jest czytelny przez czytniki 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 w kodzie 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 zmienić skalę znacznika, 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,
});
Zmień kolor 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
Ustaw opcję PinElement.glyph
na pusty ciąg znaków, aby ukryć glif znacznika:
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 ukryty.
Dalsze kroki:
O ile nie stwierdzono inaczej, treść tej strony jest objęta licencją Creative Commons – uznanie autorstwa 4.0, a fragmenty kodu są dostępne na licencji Apache 2.0. Szczegółowe informacje na ten temat zawierają zasady dotyczące witryny Google Developers. Java jest zastrzeżonym znakiem towarowym firmy Oracle i jej podmiotów stowarzyszonych.
Ostatnia aktualizacja: 2024-05-14 UTC.
[{
"type": "thumb-down",
"id": "missingTheInformationINeed",
"label":"Brak potrzebnych mi informacji"
},{
"type": "thumb-down",
"id": "tooComplicatedTooManySteps",
"label":"Zbyt skomplikowane / zbyt wiele czynności do wykonania"
},{
"type": "thumb-down",
"id": "outOfDate",
"label":"Nieaktualne treści"
},{
"type": "thumb-down",
"id": "translationIssue",
"label":"Problem z tłumaczeniem"
},{
"type": "thumb-down",
"id": "samplesCodeIssue",
"label":"Problem z przykładami/kodem"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"Inne"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"Łatwo zrozumieć"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"Rozwiązało to mój problem"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"Inne"
}]