简体   繁体   English

使用基于要素属性的外部按钮切换 Google Maps API 数据层要素

[英]Toggle Google Maps API data layer features with external buttons based on feature properties

The documentation for the Google Maps API Data Layer - Dynamic Styling explains how to add an event listener to a feature so when that feature is clicked, we can change its property. Google Maps API Data Layer - Dynamic Styling的文档解释了如何向功能添加事件侦听器,以便在单击该功能时,我们可以更改其属性。

How can I do something similar with a button external to the map?如何使用地图外部的按钮执行类似操作? In the fiddle example, how can I turn the letters on the map that have the "blue" property to blue by clicking the "blue" button?在小提琴示例中,如何通过单击“蓝色”按钮将地图上具有“蓝色”属性的字母变为蓝色?

Fiddle example小提琴示例

<!DOCTYPE html>
<html>
  <head>
    <title>Data Layer: Dynamic Styling</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <style>#map {
    height: 500px;
  }
  </style>
  </head>
  <body>
<button id="blue-button">blue</button>
<button id="red-button">red</button>
<button id="green-button">green</button>
<button id="yellow-button">yellow</button>
<div id="map"></div>
    <script>

      var map;
      function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
          zoom: 4,
          center: {lat: -28, lng: 137}
        });

        // Load GeoJSON.
        map.data.loadGeoJson(
            'https://storage.googleapis.com/mapsdevsite/json/google.json');

        // Color each letter gray. Change the color when the isColorful property
        // is set to true.
        map.data.setStyle(function(feature) {

          var color = 'gray';

          if (feature.getProperty('isColorful')) {
            color = feature.getProperty('color');
            console.log(color)
          }

          return ({
            fillColor: color,
            strokeColor: color,
            strokeWeight: 2
          });

        });

        // When the user clicks, set 'isColorful', changing the color of the letters.
        map.data.addListener('click', function(event) {
          event.feature.setProperty('isColorful', true);
        });

      }
    </script>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB9BG3aO_hV9r8qaGkYmcE5eSx7c4K7be4&callback=initMap">
    </script>
  </body>
</html>

Add a listener for your button click.为您的按钮单击添加一个侦听器。 This can be done in different ways.这可以通过不同的方式完成。 One of them is to use the Google Maps addDomListener .其中之一是使用谷歌地图addDomListener

Then you must loop through all features and set the appropriate style, for example:然后您必须遍历所有功能并设置适当的样式,例如:

 var map; function initMap() { map = new google.maps.Map(document.getElementById('map-canvas'), { zoom: 4, center: { lat: -28, lng: 137 } }); // Load GeoJSON. map.data.loadGeoJson( 'https://storage.googleapis.com/mapsdevsite/json/google.json'); // Color each letter gray. Change the color when the isColorful property // is set to true. map.data.setStyle(function(feature) { var color = 'gray'; if (feature.getProperty('isColorful')) { color = feature.getProperty('color'); console.log(color) } return ({ fillColor: color, strokeColor: color, strokeWeight: 2 }); }); // When the user clicks, set 'isColorful', changing the color of the letters. map.data.addListener('click', function(event) { event.feature.setProperty('isColorful', true); }); google.maps.event.addDomListener(document.getElementById('blue-button'), 'click', function() { map.data.setStyle(function(feature) { if (feature.getProperty('color') == 'blue') { return ({ fillColor: 'blue', strokeColor: 'blue', strokeWeight: 2 }); } else { return ({ fillColor: 'grey', fillOpacity: .5, strokeColor: 'grey', strokeWeight: 2 }); } }); }); }
 #map-canvas { height: 160px; }
 <button id="blue-button">blue</button> <div id="map-canvas"></div> <!-- Replace the value of the key parameter with your own API key. --> <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap"> </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM