Tổng quan
Để tạo kiểu cho màu nền và nét vẽ cho đa giác ranh giới, hãy sử dụng FeatureStyleOptions để xác định các thuộc tính kiểu và đặt thuộc tính style trên một lớp đối tượng thành google.maps.FeatureStyleFunction, chứa logic tạo kiểu.
Bản đồ ví dụ sau đây minh hoạ cách làm nổi bật đa giác ranh giới cho một khu vực.
Áp dụng kiểu cho các đối tượng ranh giới bằng cách đặt thuộc tính style thành google.maps.FeatureStyleFunction (có thể chứa logic định kiểu). Hàm kiểu được chạy trên mọi đối tượng trong lớp đối tượng chịu ảnh hưởng và được áp dụng tại thời điểm bạn đặt thuộc tính kiểu. Để cập nhật, bạn phải đặt lại thuộc tính kiểu.
Để tạo kiểu đồng nhất cho tất cả đối tượng của một lớp đối tượng, hãy đặt thuộc tính style thành google.maps.FeatureStyleOptions. Trong trường hợp này, bạn không cần sử dụng hàm kiểu đối tượng vì không cần logic.
Hàm kiểu phải luôn trả về kết quả nhất quán khi được áp dụng cho các đối tượng. Ví dụ: nếu bạn muốn tô màu ngẫu nhiên cho một nhóm đối tượng, thì phần ngẫu nhiên không được diễn ra trong hàm kiểu đối tượng, vì điều đó sẽ gây ra kết quả ngoài ý muốn.
Vì hàm này chạy trên mọi đối tượng trong một lớp, nên việc tối ưu hoá là rất quan trọng. Để tránh ảnh hưởng đến thời gian kết xuất, hãy làm như sau:
- Chỉ bật những lớp bạn cần.
- Đặt
stylethànhnullkhi một lớp không còn được sử dụng.
Để tạo kiểu cho một đa giác trong lớp đối tượng địa phương, hãy làm theo các bước sau:
- Nếu bạn chưa thực hiện, hãy làm theo các bước trong phần Bắt đầu để tạo mã bản đồ và kiểu bản đồ mới. Hãy nhớ bật lớp đối tượng Địa phương.
Tham chiếu đến lớp đối tượng địa phương khi bản đồ khởi chạy.
Tạo định nghĩa kiểu thuộc loại
google.maps.FeatureStyleFunction.Đặt thuộc tính
styletrên lớp đối tượng thànhFeatureStyleFunction. Ví dụ sau đây cho thấy cách xác định một hàm để chỉ áp dụng kiểu chogoogle.maps.Featurecó Mã địa điểm trùng khớp:TypeScript
// Define a style with purple fill and border. const featureStyleOptions: google.maps.FeatureStyleOptions = { strokeColor: '#810FCB', strokeOpacity: 1.0, strokeWeight: 3.0, fillColor: '#810FCB', fillOpacity: 0.5, }; // Apply the style to a single boundary. featureLayer.style = (options: google.maps.FeatureStyleFunctionOptions) => { const feature = options.feature as google.maps.PlaceFeature; if (feature.placeId === 'ChIJ0zQtYiWsVHkRk8lRoB1RNPo') { // Hana, HI return featureStyleOptions; } return null; };
JavaScript
// Define a style with purple fill and border. const featureStyleOptions = { strokeColor: '#810FCB', strokeOpacity: 1.0, strokeWeight: 3.0, fillColor: '#810FCB', fillOpacity: 0.5, }; // Apply the style to a single boundary. featureLayer.style = (options) => { const feature = options.feature; if (feature.placeId === 'ChIJ0zQtYiWsVHkRk8lRoB1RNPo') { // Hana, HI return featureStyleOptions; } return null; };
Nếu không tìm thấy mã địa điểm được chỉ định hoặc mã địa điểm đó không khớp với loại đối tượng đã chọn, thì kiểu sẽ không được áp dụng. Ví dụ:
nếu bạn cố gắng tạo kiểu cho một lớp POSTAL_CODE khớp với mã địa điểm của "Thành phố
New York", thì hệ thống sẽ không áp dụng kiểu nào.
Xoá kiểu khỏi một lớp
Để xoá kiểu khỏi một lớp, hãy đặt style thành null:
featureLayer.style = null;
Tra cứu mã địa điểm để nhắm đến các đối tượng địa lý
Cách lấy mã địa điểm cho các khu vực:
- Sử dụng Places API và Mã hoá địa lý để tìm kiếm khu vực theo tên và lấy mã địa điểm cho các khu vực trong ranh giới đã chỉ định.
- Lấy dữ liệu từ sự kiện nhấp chuột. Thao tác này sẽ trả về Đối tượng địa lý tương ứng với một khu vực được nhấp vào, cho phép truy cập vào mã địa điểm, tên hiển thị và danh mục loại đối tượng địa lý của khu vực đó.
Phạm vi cung cấp sẽ khác nhau tuỳ theo khu vực. Hãy xem Phạm vi ranh giới của Google để biết thông tin chi tiết.
Tên địa lý được lấy từ nhiều nguồn, chẳng hạn như Uỷ ban Địa danh Hoa Kỳ (USGS), và Tệp Gazetteer của Hoa Kỳ.
Mã ví dụ hoàn chỉnh
TypeScript
let featureLayer: google.maps.FeatureLayer; async function init() { // Request needed libraries. await google.maps.importLibrary('maps'); // Get the gmp-map element. const mapElement = document.querySelector('gmp-map')!; // Get the inner map. const innerMap = mapElement.innerMap; // Get the feature layer. featureLayer = innerMap.getFeatureLayer('LOCALITY'); // Define a style with purple fill and border. const featureStyleOptions: google.maps.FeatureStyleOptions = { strokeColor: '#810FCB', strokeOpacity: 1.0, strokeWeight: 3.0, fillColor: '#810FCB', fillOpacity: 0.5, }; // Apply the style to a single boundary. featureLayer.style = (options: google.maps.FeatureStyleFunctionOptions) => { const feature = options.feature as google.maps.PlaceFeature; if (feature.placeId === 'ChIJ0zQtYiWsVHkRk8lRoB1RNPo') { // Hana, HI return featureStyleOptions; } return null; }; } void init();
JavaScript
let featureLayer; async function init() { // Request needed libraries. await google.maps.importLibrary('maps'); // Get the gmp-map element. const mapElement = document.querySelector('gmp-map'); // Get the inner map. const innerMap = mapElement.innerMap; // Get the feature layer. featureLayer = innerMap.getFeatureLayer('LOCALITY'); // Define a style with purple fill and border. const featureStyleOptions = { strokeColor: '#810FCB', strokeOpacity: 1.0, strokeWeight: 3.0, fillColor: '#810FCB', fillOpacity: 0.5, }; // Apply the style to a single boundary. featureLayer.style = (options) => { const feature = options.feature; if (feature.placeId === 'ChIJ0zQtYiWsVHkRk8lRoB1RNPo') { // Hana, HI return featureStyleOptions; } return null; }; } void init();
CSS
/* * Always set the map height explicitly to define the size of the div element * that contains the map. */ gmp-map { height: 100%; } /* * Optional: Makes the sample page fill the window. */ html, body { height: 100%; margin: 0; padding: 0; }
HTML
<html>
<head>
<title>Boundaries Simple</title>
<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
<script>
// prettier-ignore
(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
key: "GOOGLE_MAPS_API_KEY"
});
</script>
</head>
<body>
<gmp-map
center="20.773,-156.01"
zoom="12"
map-id="8b37d7206ccf0121d4414bb0"></gmp-map>
</body>
</html>