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();