การปรับแต่งเครื่องหมายพื้นฐาน

Markers API สำหรับแผนที่ 3 มิติใช้ 2 คลาสเพื่อกำหนดเครื่องหมาย ได้แก่ คลาส Marker3DElement ซึ่งมีพารามิเตอร์พื้นฐาน (position, label และ map) และคลาส PinElement ซึ่งมีตัวเลือกสำหรับการปรับแต่งเพิ่มเติม

หากต้องการเพิ่มเครื่องหมายลงในแผนที่ คุณต้องโหลดไลบรารีเครื่องหมายก่อน ซึ่งจะให้คลาส Marker3DElement และ PinElement

ข้อมูลโค้ดต่อไปนี้แสดงโค้ดเพื่อสร้าง PinElement ใหม่ จากนั้นนําไปใช้กับเครื่องหมาย

// Adjust the scale.
const pinScaled = new PinElement({
    scale: 1.5,
});
const markerWithScale = new Marker3DElement({
    position: { lat: 37.419, lng: -122.02 },
});
markerWithScale.append(pinScaled);

ในแผนที่ที่สร้างโดยใช้ HTML พารามิเตอร์พื้นฐานสำหรับเครื่องหมายจะประกาศโดยใช้ องค์ประกอบ HTML gmp-marker-3d ส่วนการปรับแต่งใดๆ ที่ใช้คลาส PinElement จะต้องใช้โดยการเขียนโปรแกรม หากต้องการทำเช่นนี้ โค้ดของคุณต้องดึงองค์ประกอบ gmp-marker-3d จากหน้า HTML ข้อมูลโค้ดต่อไปนี้แสดงโค้ดสำหรับ ค้นหาคอลเล็กชันขององค์ประกอบ gmp-marker-3d จากนั้นวนซ้ำผลลัพธ์ เพื่อใช้การปรับแต่งที่ประกาศไว้ใน gmp-marker-3d

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

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

  markers[i].appendChild(pin.element);
}

หน้านี้แสดงวิธีปรับแต่งเครื่องหมายด้วยวิธีต่อไปนี้

ปรับขนาดเครื่องหมาย

หากต้องการปรับขนาดเครื่องหมาย ให้ใช้ตัวเลือก scale ดังนี้

// Adjust the scale.
const pinScaled = new PinElement({
    scale: 1.5,
});
const markerWithScale = new Marker3DElement({
    position: { lat: 37.419, lng: -122.02 },
});
markerWithScale.append(pinScaled);

เปลี่ยนสีพื้นหลัง

ใช้ตัวเลือก PinElement.background เพื่อเปลี่ยนสีพื้นหลังของเครื่องหมาย

// Change the background color.
const pinBackground = new PinElement({
    background: '#FBBC04',
});

const markerWithBackground = new Marker3DElement({
    position: { lat: 37.419, lng: -122.01 },
});
markerWithBackground.append(pinBackground);

เปลี่ยนสีของเส้นขอบ

ใช้ตัวเลือก PinElement.borderColor เพื่อเปลี่ยนสีเส้นขอบของเครื่องหมาย

// Change the border color.
const pinBorder = new PinElement({
    borderColor: '#FFFFFF',
});
const markerWithBorder = new Marker3DElement({
    position: { lat: 37.415, lng: -122.035 },
});
markerWithBorder.append(pinBorder);

เปลี่ยนสีของรูปอักขระ

ใช้ตัวเลือก PinElement.glyphColor เพื่อเปลี่ยนสีรูปอักขระของตัวทำเครื่องหมาย

// Change the glyph color.
const pinGlyph = new PinElement({
    glyphColor: 'white',
});
const markerWithGlyphColor = new Marker3DElement({
    position: { lat: 37.415, lng: -122.025 },
});
markerWithGlyphColor.append(pinGlyph);

เพิ่มข้อความลงในรูปอักขระ

ใช้ตัวเลือก PinElement.glyph เพื่อแทนที่รูปอักขระเริ่มต้นด้วยอักขระข้อความ รูปอักขระข้อความของ PinElement จะปรับขนาดตาม PinElement และ สีเริ่มต้นจะตรงกับ glyphColor เริ่มต้นของ PinElement

// Change many elements together and extrude marker.
const pinTextGlyph = new PinElement({
    background: '#F0F6FC',
    glyphText: 'E',
    glyphColor: 'red',
    borderColor: '#0000FF',
});
const markerWithGlyphText = new Marker3DElement({
    position: { lat: 37.415, lng: -122.015, altitude: 50 },
    extruded: true,
    altitudeMode: 'RELATIVE_TO_GROUND',
});
markerWithGlyphText.append(pinTextGlyph);

เปลี่ยนระดับความสูง

ใช้ตัวเลือก Marker3DElement.position.altitude ร่วมกับ Marker3DElement.altitudeMode และ Marker3DElement.extruded เพื่อเปลี่ยน ระดับความสูงของตัวทำเครื่องหมาย และเพิ่มเส้นที่ยื่นออกมาจากพื้นดินไปยังตัวทำเครื่องหมาย

// Change a marker's altitude and add an extrusion.
const extrudedMarker = new Marker3DElement({
    position: { lat: 37.4239163, lng: -122.0947209, altitude: 100 },
    altitudeMode: 'RELATIVE_TO_GROUND',
    extruded: true,
});

นำเครื่องหมายออก

วิธีใช้ Marker3DElement.remove() เพื่อนำเครื่องหมายออกจากแผนที่

const marker = document.querySelector('gmp-marker-3d');
marker.remove();

ขั้นตอนถัดไป