รูปร่างและเส้น

เลือกแพลตฟอร์ม Android iOS JavaScript

คุณเพิ่มรูปร่างต่างๆ ลงในแผนที่ได้ รูปร่างคือวัตถุบนแผนที่ที่เชื่อมโยงกับพิกัดละติจูด/ลองจิจูด รูปทรงที่ใช้ได้มีดังนี้ เส้น รูปหลายเหลี่ยม วงกลม และสี่เหลี่ยมผืนผ้า นอกจากนี้ คุณยังกำหนดค่ารูปร่างเพื่อให้ผู้ใช้แก้ไขหรือลากรูปร่างได้

เส้นประกอบ

หากต้องการวาดเส้นบนแผนที่ ให้ใช้เส้นประกอบ คลาส Polyline จะกำหนดการวางซ้อนเชิงเส้นของส่วนของเส้นที่เชื่อมต่อกันบนแผนที่ ออบเจ็กต์ Polyline ประกอบด้วยอาร์เรย์ของตำแหน่ง LatLng และสร้างชุดส่วนของเส้นที่เชื่อมต่อตำแหน่งเหล่านั้นตามลําดับ

เพิ่มเส้นประกอบ

ตัวสร้าง Polyline จะรับชุด PolylineOptions ที่ระบุLatLng พิกัดของเส้นและชุดสไตล์เพื่อปรับลักษณะการแสดงผลของเส้นประกอบ

ระบบจะวาดวัตถุ Polyline เป็นชุดของเส้นตรงบนแผนที่ คุณสามารถระบุสี น้ำหนัก และความทึบที่กำหนดเองสำหรับเส้นโครงร่างของเส้นได้ภายในPolylineOptionsเมื่อPolylineOptionsสร้างเส้น หรือจะเปลี่ยนพร็อพเพอร์ตี้เหล่านั้นหลังจากสร้างก็ได้ เส้นประกอบรองรับรูปแบบเส้นขีดต่อไปนี้

  • strokeColor ระบุสี HTML แบบฐานสิบหกของรูปแบบ "#FFFFFF" คลาส Polyline ไม่รองรับสีที่มีชื่อ
  • strokeOpacity ระบุค่าตัวเลขระหว่าง 0.0 ถึง 1.0 เพื่อกำหนดระดับความทึบของสีเส้น ค่าเริ่มต้นคือ 1.0
  • strokeWeight ระบุความกว้างของเส้นเป็นพิกเซล

พร็อพเพอร์ตี้ editable ของเส้นประกอบจะระบุให้ผู้ใช้แก้ไขรูปร่างได้หรือไม่ ดูรูปร่างที่ผู้ใช้แก้ไขได้ด้านล่าง ในทำนองเดียวกัน คุณก็ตั้งค่าพร็อพเพอร์ตี้ draggable เพื่ออนุญาตให้ผู้ใช้ลากเส้นได้

TypeScript

// This example creates a 2-pixel-wide red polyline showing the path of // the first trans-Pacific flight between Oakland, CA, and Brisbane, // Australia which was made by Charles Kingsford Smith.  function initMap(): void {   const map = new google.maps.Map(     document.getElementById("map") as HTMLElement,     {       zoom: 3,       center: { lat: 0, lng: -180 },       mapTypeId: "terrain",     }   );    const flightPlanCoordinates = [     { lat: 37.772, lng: -122.214 },     { lat: 21.291, lng: -157.821 },     { lat: -18.142, lng: 178.431 },     { lat: -27.467, lng: 153.027 },   ];   const flightPath = new google.maps.Polyline({     path: flightPlanCoordinates,     geodesic: true,     strokeColor: "#FF0000",     strokeOpacity: 1.0,     strokeWeight: 2,   });    flightPath.setMap(map); }  declare global {   interface Window {     initMap: () => void;   } } window.initMap = initMap;

JavaScript

// This example creates a 2-pixel-wide red polyline showing the path of // the first trans-Pacific flight between Oakland, CA, and Brisbane, // Australia which was made by Charles Kingsford Smith. function initMap() {   const map = new google.maps.Map(document.getElementById("map"), {     zoom: 3,     center: { lat: 0, lng: -180 },     mapTypeId: "terrain",   });   const flightPlanCoordinates = [     { lat: 37.772, lng: -122.214 },     { lat: 21.291, lng: -157.821 },     { lat: -18.142, lng: 178.431 },     { lat: -27.467, lng: 153.027 },   ];   const flightPath = new google.maps.Polyline({     path: flightPlanCoordinates,     geodesic: true,     strokeColor: "#FF0000",     strokeOpacity: 1.0,     strokeWeight: 2,   });    flightPath.setMap(map); }  window.initMap = initMap;
ดูตัวอย่าง

ลองใช้ตัวอย่าง

นำโพลีไลน์ออก

หากต้องการนำเส้นประกอบออกจากแผนที่ ให้เรียกใช้เมธอด setMap() โดยส่ง null เป็นอาร์กิวเมนต์ ในตัวอย่างต่อไปนี้ flightPath คือออบเจ็กต์เส้นประกอบ

flightPath.setMap(null);

โปรดทราบว่าวิธีการข้างต้นจะไม่ลบเส้นประกอบ ซึ่งจะนําเส้นประกอบออกจากแผนที่ หากต้องการลบเส้นประกอบแทน คุณควรนำเส้นประกอบออกจากแผนที่ แล้วตั้งค่าเส้นประกอบเป็น null

ตรวจสอบเส้นประกอบ

เส้นประกอบจะระบุชุดพิกัดเป็นอาร์เรย์ของออบเจ็กต์ LatLng พิกัดเหล่านี้จะกําหนดเส้นทางของเส้น หากต้องการเรียกข้อมูลพิกัด ให้เรียกใช้ getPath() ซึ่งจะแสดงผลอาร์เรย์ประเภท MVCArray คุณสามารถดําเนินการและตรวจสอบอาร์เรย์ได้โดยใช้การดำเนินการต่อไปนี้

  • getAt() จะแสดงผล LatLng ที่ค่าดัชนีที่ระบุซึ่งเริ่มต้นที่ 0
  • insertAt() จะแทรก LatLng ที่ส่งมา ที่ค่าดัชนีฐาน 0 ที่ระบุ โปรดทราบว่าระบบจะย้ายพิกัดที่มีอยู่ทั้งหมดที่ค่าดัชนีนั้นไปข้างหน้า
  • removeAt() นำ LatLng ออกที่ค่าดัชนีที่ระบุซึ่งขึ้นต้นที่ 0

TypeScript

// This example creates an interactive map which constructs a polyline based on // user clicks. Note that the polyline only appears once its path property // contains two LatLng coordinates.  let poly: google.maps.Polyline; let map: google.maps.Map;  function initMap(): void {   map = new google.maps.Map(document.getElementById("map") as HTMLElement, {     zoom: 7,     center: { lat: 41.879, lng: -87.624 }, // Center the map on Chicago, USA.   });    poly = new google.maps.Polyline({     strokeColor: "#000000",     strokeOpacity: 1.0,     strokeWeight: 3,   });   poly.setMap(map);    // Add a listener for the click event   map.addListener("click", addLatLng); }  // Handles click events on a map, and adds a new point to the Polyline. function addLatLng(event: google.maps.MapMouseEvent) {   const path = poly.getPath();    // Because path is an MVCArray, we can simply append a new coordinate   // and it will automatically appear.   path.push(event.latLng as google.maps.LatLng);    // Add a new marker at the new plotted point on the polyline.   new google.maps.Marker({     position: event.latLng,     title: "#" + path.getLength(),     map: map,   }); }  declare global {   interface Window {     initMap: () => void;   } } window.initMap = initMap;

JavaScript

// This example creates an interactive map which constructs a polyline based on // user clicks. Note that the polyline only appears once its path property // contains two LatLng coordinates. let poly; let map;  function initMap() {   map = new google.maps.Map(document.getElementById("map"), {     zoom: 7,     center: { lat: 41.879, lng: -87.624 }, // Center the map on Chicago, USA.   });   poly = new google.maps.Polyline({     strokeColor: "#000000",     strokeOpacity: 1.0,     strokeWeight: 3,   });   poly.setMap(map);   // Add a listener for the click event   map.addListener("click", addLatLng); }  // Handles click events on a map, and adds a new point to the Polyline. function addLatLng(event) {   const path = poly.getPath();    // Because path is an MVCArray, we can simply append a new coordinate   // and it will automatically appear.   path.push(event.latLng);   // Add a new marker at the new plotted point on the polyline.   new google.maps.Marker({     position: event.latLng,     title: "#" + path.getLength(),     map: map,   }); }  window.initMap = initMap;
ดูตัวอย่าง

ลองใช้ตัวอย่าง

ปรับแต่งเส้นประกอบ

คุณสามารถเพิ่มรูปภาพแบบเวกเตอร์ลงในเส้นประกอบในรูปแบบสัญลักษณ์ได้ การใช้สัญลักษณ์ร่วมกับคลาส PolylineOptions จะช่วยให้คุณควบคุมรูปลักษณ์ของเส้นประกอบในแผนที่ได้ ดูข้อมูลเกี่ยวกับลูกศร เส้นประ สัญลักษณ์ที่กำหนดเอง และสัญลักษณ์เคลื่อนไหวได้ที่หัวข้อสัญลักษณ์

รูปหลายเหลี่ยม

รูปหลายเหลี่ยมแสดงถึงพื้นที่ที่ล้อมรอบด้วยเส้นทางปิด (หรือลูป) ซึ่งกำหนดโดยชุดพิกัด ออบเจ็กต์ Polygon คล้ายกับออบเจ็กต์ Polylineตรงที่ประกอบด้วยชุดพิกัดตามลําดับ รูปหลายเหลี่ยมจะวาดด้วยเส้นขีดและสีพื้น คุณสามารถกำหนดสี น้ำหนัก และความทึบแสงที่กำหนดเองสำหรับขอบของรูปหลายเหลี่ยม (เส้นขอบ) และสีและความทึบแสงที่กำหนดเองสำหรับพื้นที่ปิดล้อม (การเติม) ควรระบุสีในรูปแบบ HTML ฐานสิบหก ไม่รองรับชื่อสี

Polygon วัตถุสามารถอธิบายรูปร่างที่ซับซ้อนได้ ซึ่งรวมถึง

  • พื้นที่หลายแห่งที่ไม่ต่อเนื่องกันซึ่งกำหนดโดยรูปหลายเหลี่ยมรูปเดียว
  • บริเวณที่มีรู
  • จุดตัดของพื้นที่อย่างน้อย 1 พื้นที่

หากต้องการกําหนดรูปร่างที่ซับซ้อน ให้ใช้รูปหลายเหลี่ยมที่มีหลายเส้นทาง

หมายเหตุ: เลเยอร์ข้อมูลเป็นวิธีที่ง่ายในการวาดรูปหลายเหลี่ยม เครื่องมือนี้จะจัดการการเลี้ยวของรูปหลายเหลี่ยมให้คุณ ทำให้วาดรูปหลายเหลี่ยมที่มีรูได้ง่ายขึ้น ดูเอกสารประกอบสําหรับชั้นข้อมูล

เพิ่มรูปหลายเหลี่ยม

เนื่องจากพื้นที่รูปหลายเหลี่ยมอาจมีเส้นทางแยกกันหลายเส้นทาง พร็อพเพอร์ตี้ paths ของออบเจ็กต์ Polygon จึงระบุอาร์เรย์ของอาร์เรย์ ซึ่งแต่ละรายการมีประเภทเป็น MVCArray อาร์เรย์แต่ละรายการจะกำหนดลำดับพิกัด LatLng ที่เรียงลำดับแยกกัน

สําหรับรูปหลายเหลี่ยมธรรมดาที่มีเพียงเส้นทางเดียว คุณสามารถสร้าง Polygon โดยใช้อาร์เรย์เดียวของพิกัด LatLng Maps JavaScript API จะแปลงอาร์เรย์แบบง่ายเป็นอาร์เรย์ของอาร์เรย์เมื่อสร้างและจัดเก็บไว้ในพร็อพเพอร์ตี้ paths API มีวิธี getPath() แบบง่ายสำหรับรูปหลายเหลี่ยมที่ประกอบด้วยเส้นทางเดียว

พร็อพเพอร์ตี้ editable ของรูปหลายเหลี่ยมจะระบุว่าผู้ใช้จะแก้ไขรูปร่างได้หรือไม่ ดูรูปร่างที่ผู้ใช้แก้ไขได้ด้านล่าง ในทำนองเดียวกัน คุณก็ตั้งค่าพร็อพเพอร์ตี้ draggable เพื่ออนุญาตให้ผู้ใช้ลากรูปร่างได้

TypeScript

// This example creates a simple polygon representing the Bermuda Triangle.  function initMap(): void {   const map = new google.maps.Map(     document.getElementById("map") as HTMLElement,     {       zoom: 5,       center: { lat: 24.886, lng: -70.268 },       mapTypeId: "terrain",     }   );    // Define the LatLng coordinates for the polygon's path.   const triangleCoords = [     { lat: 25.774, lng: -80.19 },     { lat: 18.466, lng: -66.118 },     { lat: 32.321, lng: -64.757 },     { lat: 25.774, lng: -80.19 },   ];    // Construct the polygon.   const bermudaTriangle = new google.maps.Polygon({     paths: triangleCoords,     strokeColor: "#FF0000",     strokeOpacity: 0.8,     strokeWeight: 2,     fillColor: "#FF0000",     fillOpacity: 0.35,   });    bermudaTriangle.setMap(map); }  declare global {   interface Window {     initMap: () => void;   } } window.initMap = initMap;

JavaScript

// This example creates a simple polygon representing the Bermuda Triangle. function initMap() {   const map = new google.maps.Map(document.getElementById("map"), {     zoom: 5,     center: { lat: 24.886, lng: -70.268 },     mapTypeId: "terrain",   });   // Define the LatLng coordinates for the polygon's path.   const triangleCoords = [     { lat: 25.774, lng: -80.19 },     { lat: 18.466, lng: -66.118 },     { lat: 32.321, lng: -64.757 },     { lat: 25.774, lng: -80.19 },   ];   // Construct the polygon.   const bermudaTriangle = new google.maps.Polygon({     paths: triangleCoords,     strokeColor: "#FF0000",     strokeOpacity: 0.8,     strokeWeight: 2,     fillColor: "#FF0000",     fillOpacity: 0.35,   });    bermudaTriangle.setMap(map); }  window.initMap = initMap;
ดูตัวอย่าง

ลองใช้ตัวอย่าง

การเติมข้อความอัตโนมัติของ Polygon

Polygon ในตัวอย่างด้านบนประกอบด้วยชุดพิกัด LatLng 4 ชุด แต่สังเกตว่าชุดแรกและชุดสุดท้ายกำหนดตำแหน่งเดียวกัน ซึ่งทำให้ลูปสมบูรณ์ อย่างไรก็ตาม ในทางปฏิบัติ คุณไม่จำเป็นต้องระบุชุดพิกัดสุดท้ายเนื่องจากรูปหลายเหลี่ยมจะกำหนดพื้นที่ปิด Maps JavaScript API จะวาดรูปหลายเหลี่ยมให้เสร็จโดยอัตโนมัติด้วยการลากเส้นที่เชื่อมต่อตำแหน่งสุดท้ายกลับไปที่ตำแหน่งแรกสำหรับเส้นทางหนึ่งๆ

ตัวอย่างต่อไปนี้เหมือนกับตัวอย่างก่อนหน้า ยกเว้นไม่มี LatLng ตัวสุดท้าย ดูตัวอย่าง

นำรูปหลายเหลี่ยมออก

หากต้องการนำรูปหลายเหลี่ยมออกจากแผนที่ ให้เรียกใช้เมธอด setMap() โดยส่ง null เป็นอาร์กิวเมนต์ ในตัวอย่างต่อไปนี้ bermudaTriangle คือออบเจ็กต์รูปหลายเหลี่ยม

bermudaTriangle.setMap(null);

โปรดทราบว่าวิธีการข้างต้นจะไม่ลบรูปหลายเหลี่ยม ซึ่งจะนํารูปหลายเหลี่ยมออกจากแผนที่ หากต้องการลบรูปหลายเหลี่ยมแทน คุณควรนำรูปหลายเหลี่ยมออกจากแผนที่ แล้วตั้งค่ารูปหลายเหลี่ยมเป็น null

ตรวจสอบรูปหลายเหลี่ยม

รูปหลายเหลี่ยมจะระบุชุดพิกัดเป็นอาร์เรย์ของอาร์เรย์ โดยที่แต่ละอาร์เรย์เป็นประเภท MVCArray อาร์เรย์ "ใบ" แต่ละรายการคืออาร์เรย์ของLatLngพิกัด ที่ระบุเส้นทางเดียว หากต้องการเรียกข้อมูลพิกัดเหล่านี้ ให้เรียกใช้เมธอด getPaths() ของออบเจ็กต์ Polygon เนื่องจากอาร์เรย์เป็น MVCArray คุณจะต้องดำเนินการและตรวจสอบโดยใช้การดำเนินการต่อไปนี้

  • getAt() จะแสดงผล LatLng ที่ค่าดัชนีที่ระบุซึ่งเริ่มต้นที่ 0
  • insertAt() จะแทรก LatLng ที่ส่งมา ที่ค่าดัชนีฐาน 0 ที่ระบุ โปรดทราบว่าระบบจะย้ายพิกัดที่มีอยู่ทั้งหมดที่ค่าดัชนีนั้นไปข้างหน้า
  • removeAt() นำ LatLng ออกที่ค่าดัชนีที่ระบุซึ่งขึ้นต้นที่ 0

TypeScript

// This example creates a simple polygon representing the Bermuda Triangle. // When the user clicks on the polygon an info window opens, showing // information about the polygon's coordinates.  let map: google.maps.Map;  let infoWindow: google.maps.InfoWindow;  function initMap(): void {   map = new google.maps.Map(document.getElementById("map") as HTMLElement, {     zoom: 5,     center: { lat: 24.886, lng: -70.268 },     mapTypeId: "terrain",   });    // Define the LatLng coordinates for the polygon.   const triangleCoords: google.maps.LatLngLiteral[] = [     { lat: 25.774, lng: -80.19 },     { lat: 18.466, lng: -66.118 },     { lat: 32.321, lng: -64.757 },   ];    // Construct the polygon.   const bermudaTriangle = new google.maps.Polygon({     paths: triangleCoords,     strokeColor: "#FF0000",     strokeOpacity: 0.8,     strokeWeight: 3,     fillColor: "#FF0000",     fillOpacity: 0.35,   });    bermudaTriangle.setMap(map);    // Add a listener for the click event.   bermudaTriangle.addListener("click", showArrays);    infoWindow = new google.maps.InfoWindow(); }  function showArrays(event: any) {   // Since this polygon has only one path, we can call getPath() to return the   // MVCArray of LatLngs.   // @ts-ignore   const polygon = this as google.maps.Polygon;   const vertices = polygon.getPath();    let contentString =     "<b>Bermuda Triangle polygon</b><br>" +     "Clicked location: <br>" +     event.latLng.lat() +     "," +     event.latLng.lng() +     "<br>";    // Iterate over the vertices.   for (let i = 0; i < vertices.getLength(); i++) {     const xy = vertices.getAt(i);      contentString +=       "<br>" + "Coordinate " + i + ":<br>" + xy.lat() + "," + xy.lng();   }    // Replace the info window's content and position.   infoWindow.setContent(contentString);   infoWindow.setPosition(event.latLng);    infoWindow.open(map); }  declare global {   interface Window {     initMap: () => void;   } } window.initMap = initMap;

JavaScript

// This example creates a simple polygon representing the Bermuda Triangle. // When the user clicks on the polygon an info window opens, showing // information about the polygon's coordinates. let map; let infoWindow;  function initMap() {   map = new google.maps.Map(document.getElementById("map"), {     zoom: 5,     center: { lat: 24.886, lng: -70.268 },     mapTypeId: "terrain",   });    // Define the LatLng coordinates for the polygon.   const triangleCoords = [     { lat: 25.774, lng: -80.19 },     { lat: 18.466, lng: -66.118 },     { lat: 32.321, lng: -64.757 },   ];   // Construct the polygon.   const bermudaTriangle = new google.maps.Polygon({     paths: triangleCoords,     strokeColor: "#FF0000",     strokeOpacity: 0.8,     strokeWeight: 3,     fillColor: "#FF0000",     fillOpacity: 0.35,   });    bermudaTriangle.setMap(map);   // Add a listener for the click event.   bermudaTriangle.addListener("click", showArrays);   infoWindow = new google.maps.InfoWindow(); }  function showArrays(event) {   // Since this polygon has only one path, we can call getPath() to return the   // MVCArray of LatLngs.   // @ts-ignore   const polygon = this;   const vertices = polygon.getPath();   let contentString =     "<b>Bermuda Triangle polygon</b><br>" +     "Clicked location: <br>" +     event.latLng.lat() +     "," +     event.latLng.lng() +     "<br>";    // Iterate over the vertices.   for (let i = 0; i < vertices.getLength(); i++) {     const xy = vertices.getAt(i);      contentString +=       "<br>" + "Coordinate " + i + ":<br>" + xy.lat() + "," + xy.lng();   }    // Replace the info window's content and position.   infoWindow.setContent(contentString);   infoWindow.setPosition(event.latLng);   infoWindow.open(map); }  window.initMap = initMap;
ดูตัวอย่าง

ลองใช้ตัวอย่าง

เจาะรูในรูปหลายเหลี่ยม

หากต้องการสร้างพื้นที่ว่างภายในรูปหลายเหลี่ยม คุณต้องสร้างเส้นทาง 2 เส้น โดยให้เส้นหนึ่งอยู่ภายในอีกเส้นหนึ่ง หากต้องการสร้างรู พิกัดที่กำหนดเส้นทางด้านในต้องอยู่ในลำดับที่ตรงข้ามกับพิกัดที่กำหนดเส้นทางด้านนอก เช่น หากพิกัดของเส้นทางด้านนอกอยู่ในลําดับตามเข็มนาฬิกา เส้นทางด้านในจะต้องอยู่ในลําดับทวนเข็มนาฬิกา

หมายเหตุ: เลเยอร์ข้อมูลจะจัดการลําดับของเส้นทางด้านในและด้านนอกให้คุณ ทำให้วาดรูปหลายเหลี่ยมที่มีรูได้ง่ายขึ้น ดูเอกสารประกอบเกี่ยวกับชั้นข้อมูล

ตัวอย่างต่อไปนี้วาดรูปหลายเหลี่ยมที่มี 2 เส้นทาง โดยเส้นทางด้านในจะขดสวนทางกับเส้นทางด้านนอก

TypeScript

// This example creates a triangular polygon with a hole in it.  function initMap(): void {   const map = new google.maps.Map(     document.getElementById("map") as HTMLElement,     {       zoom: 5,       center: { lat: 24.886, lng: -70.268 },     }   );    // Define the LatLng coordinates for the polygon's  outer path.   const outerCoords = [     { lat: 25.774, lng: -80.19 },     { lat: 18.466, lng: -66.118 },     { lat: 32.321, lng: -64.757 },   ];    // Define the LatLng coordinates for the polygon's inner path.   // Note that the points forming the inner path are wound in the   // opposite direction to those in the outer path, to form the hole.   const innerCoords = [     { lat: 28.745, lng: -70.579 },     { lat: 29.57, lng: -67.514 },     { lat: 27.339, lng: -66.668 },   ];    // Construct the polygon, including both paths.   const bermudaTriangle = new google.maps.Polygon({     paths: [outerCoords, innerCoords],     strokeColor: "#FFC107",     strokeOpacity: 0.8,     strokeWeight: 2,     fillColor: "#FFC107",     fillOpacity: 0.35,   });    bermudaTriangle.setMap(map); }  declare global {   interface Window {     initMap: () => void;   } } window.initMap = initMap;

JavaScript

// This example creates a triangular polygon with a hole in it. function initMap() {   const map = new google.maps.Map(document.getElementById("map"), {     zoom: 5,     center: { lat: 24.886, lng: -70.268 },   });   // Define the LatLng coordinates for the polygon's  outer path.   const outerCoords = [     { lat: 25.774, lng: -80.19 },     { lat: 18.466, lng: -66.118 },     { lat: 32.321, lng: -64.757 },   ];   // Define the LatLng coordinates for the polygon's inner path.   // Note that the points forming the inner path are wound in the   // opposite direction to those in the outer path, to form the hole.   const innerCoords = [     { lat: 28.745, lng: -70.579 },     { lat: 29.57, lng: -67.514 },     { lat: 27.339, lng: -66.668 },   ];   // Construct the polygon, including both paths.   const bermudaTriangle = new google.maps.Polygon({     paths: [outerCoords, innerCoords],     strokeColor: "#FFC107",     strokeOpacity: 0.8,     strokeWeight: 2,     fillColor: "#FFC107",     fillOpacity: 0.35,   });    bermudaTriangle.setMap(map); }  window.initMap = initMap;
ดูตัวอย่าง

ลองใช้ตัวอย่าง

สี่เหลี่ยมผืนผ้า

นอกจากคลาส Polygon ทั่วไปแล้ว Google Maps JavaScript API ยังมีคลาสที่เฉพาะเจาะจงสำหรับออบเจ็กต์ Rectangle เพื่อลดความซับซ้อนในการสร้างออบเจ็กต์

เพิ่มสี่เหลี่ยมผืนผ้า

Rectangle คล้ายกับ Polygon ตรงที่คุณสามารถกำหนดสี ความหนา และระดับความทึบที่กำหนดเองสำหรับขอบของสี่เหลี่ยมผืนผ้า (เส้นโครงร่าง) รวมถึงสีและความทึบที่กำหนดเองสำหรับพื้นที่ภายในสี่เหลี่ยมผืนผ้า (การเติม) สีควรระบุในรูปแบบตัวเลขฐาน 16 ของ HTML

คุณไม่จำเป็นต้องกำหนด paths สำหรับ Rectangle ต่างจาก Polygon แต่สี่เหลี่ยมผืนผ้าจะมีพร็อพเพอร์ตี้ bounds ซึ่งกำหนดรูปร่างโดยระบุ google.maps.LatLngBounds สำหรับสี่เหลี่ยมผืนผ้า

พร็อพเพอร์ตี้ editable ของสี่เหลี่ยมผืนผ้าจะระบุว่าผู้ใช้จะแก้ไขรูปร่างได้หรือไม่ ดูรูปร่างที่ผู้ใช้แก้ไขได้ด้านล่าง ในทำนองเดียวกัน คุณก็ตั้งค่าพร็อพเพอร์ตี้ draggable ได้เพื่ออนุญาตให้ผู้ใช้ลากสี่เหลี่ยมผืนผ้า

TypeScript

// This example adds a red rectangle to a map.  function initMap(): void {   const map = new google.maps.Map(     document.getElementById("map") as HTMLElement,     {       zoom: 11,       center: { lat: 33.678, lng: -116.243 },       mapTypeId: "terrain",     }   );    const rectangle = new google.maps.Rectangle({     strokeColor: "#FF0000",     strokeOpacity: 0.8,     strokeWeight: 2,     fillColor: "#FF0000",     fillOpacity: 0.35,     map,     bounds: {       north: 33.685,       south: 33.671,       east: -116.234,       west: -116.251,     },   }); }  declare global {   interface Window {     initMap: () => void;   } } window.initMap = initMap;

JavaScript

// This example adds a red rectangle to a map. function initMap() {   const map = new google.maps.Map(document.getElementById("map"), {     zoom: 11,     center: { lat: 33.678, lng: -116.243 },     mapTypeId: "terrain",   });   const rectangle = new google.maps.Rectangle({     strokeColor: "#FF0000",     strokeOpacity: 0.8,     strokeWeight: 2,     fillColor: "#FF0000",     fillOpacity: 0.35,     map,     bounds: {       north: 33.685,       south: 33.671,       east: -116.234,       west: -116.251,     },   }); }  window.initMap = initMap;
ดูตัวอย่าง

ลองใช้ตัวอย่าง

โค้ดต่อไปนี้จะสร้างสี่เหลี่ยมผืนผ้าทุกครั้งที่ผู้ใช้เปลี่ยนการซูมบนแผนที่ ขนาดของสี่เหลี่ยมผืนผ้าจะกำหนดโดยวิวพอร์ต

TypeScript

// This example creates a rectangle based on the viewport // on any 'zoom-changed' event.  function initMap(): void {   const map = new google.maps.Map(     document.getElementById("map") as HTMLElement,     {       zoom: 11,       center: { lat: 40.74852, lng: -73.981687 },       mapTypeId: "terrain",     }   );    const rectangle = new google.maps.Rectangle();    map.addListener("zoom_changed", () => {     // Get the current bounds, which reflect the bounds before the zoom.     rectangle.setOptions({       strokeColor: "#FF0000",       strokeOpacity: 0.8,       strokeWeight: 2,       fillColor: "#FF0000",       fillOpacity: 0.35,       map,       bounds: map.getBounds() as google.maps.LatLngBounds,     });   }); }  declare global {   interface Window {     initMap: () => void;   } } window.initMap = initMap;

JavaScript

// This example creates a rectangle based on the viewport // on any 'zoom-changed' event. function initMap() {   const map = new google.maps.Map(document.getElementById("map"), {     zoom: 11,     center: { lat: 40.74852, lng: -73.981687 },     mapTypeId: "terrain",   });   const rectangle = new google.maps.Rectangle();    map.addListener("zoom_changed", () => {     // Get the current bounds, which reflect the bounds before the zoom.     rectangle.setOptions({       strokeColor: "#FF0000",       strokeOpacity: 0.8,       strokeWeight: 2,       fillColor: "#FF0000",       fillOpacity: 0.35,       map,       bounds: map.getBounds(),     });   }); }  window.initMap = initMap;
ดูตัวอย่าง

ลองใช้ตัวอย่าง

นำสี่เหลี่ยมผืนผ้าออก

หากต้องการนำสี่เหลี่ยมผืนผ้าออกจากแผนที่ ให้เรียกใช้เมธอด setMap() โดยส่ง null เป็นอาร์กิวเมนต์

rectangle.setMap(null);

โปรดทราบว่าวิธีการข้างต้นจะไม่ลบสี่เหลี่ยมผืนผ้า ซึ่งจะนำสี่เหลี่ยมผืนผ้าออกจากแผนที่ หากต้องการลบสี่เหลี่ยมผืนผ้าแทน คุณควรนำสี่เหลี่ยมผืนผ้าออกจากแผนที่ แล้วตั้งค่าสี่เหลี่ยมผืนผ้าเป็น null

วงกลม

นอกจากคลาส Polygon ทั่วไปแล้ว Google Maps JavaScript API ยังมีคลาสเฉพาะสำหรับออบเจ็กต์ Circle เพื่อลดความซับซ้อนในการสร้างออบเจ็กต์

เพิ่มวงกลม

Circle คล้ายกับ Polygon ตรงที่คุณสามารถกําหนดสี ความหนา และความทึบแสงที่กําหนดเองสําหรับขอบของวงกลม (เส้นโครงร่าง) และสีและความทึบแสงที่กําหนดเองสําหรับพื้นที่ภายในวงกลม (การเติม) สีควรระบุในรูปแบบตัวเลขฐาน 16 ของ HTML

คุณไม่จำเป็นต้องกำหนด paths สำหรับ Circle ต่างจาก Polygon แต่วงกลมจะมีพร็อพเพอร์ตี้เพิ่มเติม 2 รายการซึ่งกำหนดรูปร่างของวงกลม ดังนี้

  • center ระบุ google.maps.LatLng ของจุดศูนย์กลางของวงกลม
  • radius ระบุรัศมีของวงกลมเป็นเมตร

พร็อพเพอร์ตี้ editable ของวงกลมจะระบุว่าผู้ใช้จะแก้ไขรูปร่างได้หรือไม่ ดูรูปร่างที่ผู้ใช้แก้ไขได้ด้านล่าง ในทำนองเดียวกัน คุณก็ตั้งค่าพร็อพเพอร์ตี้ draggable เพื่ออนุญาตให้ผู้ใช้ลากวงกลมได้

TypeScript

// This example creates circles on the map, representing populations in North // America.  // First, create an object containing LatLng and population for each city.  interface City {   center: google.maps.LatLngLiteral;   population: number; }  const citymap: Record<string, City> = {   chicago: {     center: { lat: 41.878, lng: -87.629 },     population: 2714856,   },   newyork: {     center: { lat: 40.714, lng: -74.005 },     population: 8405837,   },   losangeles: {     center: { lat: 34.052, lng: -118.243 },     population: 3857799,   },   vancouver: {     center: { lat: 49.25, lng: -123.1 },     population: 603502,   }, };  function initMap(): void {   // Create the map.   const map = new google.maps.Map(     document.getElementById("map") as HTMLElement,     {       zoom: 4,       center: { lat: 37.09, lng: -95.712 },       mapTypeId: "terrain",     }   );    // Construct the circle for each value in citymap.   // Note: We scale the area of the circle based on the population.   for (const city in citymap) {     // Add the circle for this city to the map.     const cityCircle = new google.maps.Circle({       strokeColor: "#FF0000",       strokeOpacity: 0.8,       strokeWeight: 2,       fillColor: "#FF0000",       fillOpacity: 0.35,       map,       center: citymap[city].center,       radius: Math.sqrt(citymap[city].population) * 100,     });   } }  declare global {   interface Window {     initMap: () => void;   } } window.initMap = initMap;

JavaScript

const citymap = {   chicago: {     center: { lat: 41.878, lng: -87.629 },     population: 2714856,   },   newyork: {     center: { lat: 40.714, lng: -74.005 },     population: 8405837,   },   losangeles: {     center: { lat: 34.052, lng: -118.243 },     population: 3857799,   },   vancouver: {     center: { lat: 49.25, lng: -123.1 },     population: 603502,   }, };  function initMap() {   // Create the map.   const map = new google.maps.Map(document.getElementById("map"), {     zoom: 4,     center: { lat: 37.09, lng: -95.712 },     mapTypeId: "terrain",   });    // Construct the circle for each value in citymap.   // Note: We scale the area of the circle based on the population.   for (const city in citymap) {     // Add the circle for this city to the map.     const cityCircle = new google.maps.Circle({       strokeColor: "#FF0000",       strokeOpacity: 0.8,       strokeWeight: 2,       fillColor: "#FF0000",       fillOpacity: 0.35,       map,       center: citymap[city].center,       radius: Math.sqrt(citymap[city].population) * 100,     });   } }  window.initMap = initMap;
ดูตัวอย่าง

ลองใช้ตัวอย่าง

นำแวดวงออก

หากต้องการนำวงกลมออกจากแผนที่ ให้เรียกใช้เมธอด setMap() โดยส่ง null เป็นอาร์กิวเมนต์

circle.setMap(null);

โปรดทราบว่าวิธีการข้างต้นจะไม่ลบวงกลม ซึ่งจะนำวงกลมออกจากแผนที่ หากต้องการลบวงกลมแทน คุณควรนำวงกลมออกจากแผนที่ แล้วตั้งค่าวงกลมเป็น null

รูปร่างที่ผู้ใช้แก้ไขและลากได้

การทำรูปร่างให้แก้ไขได้จะเพิ่มแถบแฮนเดิลลงในรูปร่าง ซึ่งผู้ใช้สามารถใช้เพื่อเปลี่ยนตำแหน่ง ปรับรูปร่าง และปรับขนาดรูปร่างบนแผนที่ได้โดยตรง นอกจากนี้ คุณยังทำให้รูปร่างลากได้เพื่อให้ผู้ใช้ย้ายรูปร่างไปยังตำแหน่งอื่นบนแผนที่ได้อีกด้วย

การเปลี่ยนแปลงที่ผู้ใช้ทำกับออบเจ็กต์จะหายไปเมื่อเซสชันสิ้นสุดลง หากต้องการบันทึกการแก้ไขของผู้ใช้ คุณต้องบันทึกและจัดเก็บข้อมูลด้วยตนเอง

ทำให้แก้ไขรูปร่างได้

คุณตั้งค่ารูปร่างใดก็ได้ (เส้นประกอบ รูปหลายเหลี่ยม วงกลม และสี่เหลี่ยมผืนผ้า) ให้ผู้ใช้แก้ไขได้โดยตั้งค่า editable เป็น true ในตัวเลือกของรูปร่าง

var bounds = {   north: 44.599,   south: 44.490,   east: -78.443,   west: -78.649 };  // Define a rectangle and set its editable property to true. var rectangle = new google.maps.Rectangle({   bounds: bounds,   editable: true });

ดูตัวอย่าง

ทําให้รูปร่างลากได้

โดยค่าเริ่มต้น รูปร่างที่วาดในแผนที่จะยึดตำแหน่งไว้ หากต้องการอนุญาตให้ผู้ใช้ลากรูปร่างไปยังตำแหน่งอื่นบนแผนที่ ให้ตั้งค่า draggable เป็น true ในตัวเลือกรูปร่าง

var redCoords = [   {lat: 25.774, lng: -80.190},   {lat: 18.466, lng: -66.118},   {lat: 32.321, lng: -64.757} ];  // Construct a draggable red triangle with geodesic set to true. new google.maps.Polygon({   map: map,   paths: redCoords,   strokeColor: '#FF0000',   strokeOpacity: 0.8,   strokeWeight: 2,   fillColor: '#FF0000',   fillOpacity: 0.35,   draggable: true,   geodesic: true });

เมื่อเปิดใช้การลากในรูปหลายเหลี่ยมหรือเส้นประกอบ คุณควรพิจารณาทำให้รูปหลายเหลี่ยมหรือเส้นประกอบเป็นเส้น geodesic ด้วย โดยการตั้งค่าพร็อพเพอร์ตี้ geodesic ให้เป็น true

รูปหลายเหลี่ยมเชิงเรขาคณิตจะคงรูปร่างทางภูมิศาสตร์จริงไว้เมื่อมีการย้าย ทำให้รูปหลายเหลี่ยมบิดเบี้ยวเมื่อย้ายไปทางเหนือหรือใต้ในการฉาย Mercator รูปหลายเหลี่ยมที่ไม่ใช่รูปทรงเรขาคณิตเชิงพื้นที่จะยังคงรูปลักษณ์เดิมบนหน้าจอเสมอ

ในรูปหลายเส้นเชิงเรขาคณิต ส่วนของรูปหลายเส้นจะวาดเป็นเส้นทางที่สั้นที่สุดระหว่างจุด 2 จุดบนพื้นผิวโลก โดยสมมติว่าโลกเป็นรูปทรงกลม ต่างจากเส้นตรงในการแสดงผล Mercator

ดูข้อมูลเพิ่มเติมเกี่ยวกับระบบพิกัดได้ในคู่มือพิกัดแผนที่และไทล์

แผนที่ต่อไปนี้แสดงสามเหลี่ยม 2 รูปที่มีขนาดและมิติข้อมูลใกล้เคียงกัน สามเหลี่ยมสีแดงมีการตั้งค่าพร็อพเพอร์ตี้ geodesic เป็น true สังเกตว่ารูปร่างของพายุเปลี่ยนแปลงไปอย่างไรเมื่อเคลื่อนตัวไปทางเหนือ

ดูตัวอย่าง

ฟังเหตุการณ์การแก้ไข

เมื่อแก้ไขรูปร่าง ระบบจะเรียกเหตุการณ์เมื่อการแก้ไขเสร็จสมบูรณ์ เหตุการณ์เหล่านี้แสดงอยู่ในรายการด้านล่าง

รูปร่าง กิจกรรม
วงกลม radius_changed
center_changed
รูปหลายเหลี่ยม insert_at
remove_at
set_at

คุณต้องตั้งค่า Listener ในเส้นทางของรูปหลายเหลี่ยม หากรูปหลายเหลี่ยมมีเส้นทางหลายเส้นทาง คุณต้องตั้งค่า Listener ในแต่ละเส้นทาง

เส้นประกอบ insert_at
remove_at
set_at

คุณต้องตั้งค่า Listener ในเส้นทางของเส้นประกอบ

สี่เหลี่ยมผืนผ้า bounds_changed

ข้อมูลโค้ดที่มีประโยชน์บางส่วน

google.maps.event.addListener(circle, 'radius_changed', function() {   console.log(circle.getRadius()); });  google.maps.event.addListener(outerPath, 'set_at', function() {   console.log('Vertex moved on outer path.'); });  google.maps.event.addListener(innerPath, 'insert_at', function() {   console.log('Vertex removed from inner path.'); });  google.maps.event.addListener(rectangle, 'bounds_changed', function() {   console.log('Bounds changed.'); });

ดูตัวอย่างการจัดการเหตุการณ์การแก้ไขสี่เหลี่ยมผืนผ้า ดูตัวอย่าง

รอรับเหตุการณ์การลาก

เมื่อมีการลากรูปร่าง ระบบจะเรียกเหตุการณ์เมื่อเริ่มและสิ้นสุดการลาก รวมถึงระหว่างการลาก ระบบจะเรียกเหตุการณ์ต่อไปนี้สําหรับเส้นประกอบ รูปหลายเหลี่ยม วงกลม และสี่เหลี่ยมผืนผ้า

กิจกรรม คำอธิบาย
dragstart เริ่มทํางานเมื่อผู้ใช้เริ่มลากรูปร่าง
drag เริ่มทํางานซ้ำๆ ขณะที่ผู้ใช้ลากรูปร่าง
dragend เริ่มทํางานเมื่อผู้ใช้หยุดลากรูปร่าง

ดูข้อมูลเพิ่มเติมเกี่ยวกับการจัดการเหตุการณ์ได้ที่เอกสารประกอบเกี่ยวกับเหตุการณ์