javascript - 使用 Google Maps API 在 Google map 上的不同位置使用不同的颜色

标签 javascript google-maps

在此代码中,它用一种颜色标记所有位置。谁能帮我修改代码以用不同颜色标记每个位置

来源:https://developers.google.com/maps/documentation/javascript/examples/circle-simple

 <!DOCTYPE html>
    <html>
      <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
        <meta charset="utf-8">
        <title>Circles</title>
        <style>
          html, body, #map-canvas {
            height: 100%;
            margin: 0px;
            padding: 0px
          }
        </style>
        <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
        <script>
    // This example creates circles on the map, representing
    // populations in North America.

    // First, create an object containing LatLng and population for each city.
    var citymap = {};
    citymap['chicago'] = {
      center: new google.maps.LatLng(41.878113, -87.629798),
      population: 2714856
    };
    citymap['newyork'] = {
      center: new google.maps.LatLng(40.714352, -74.005973),
      population: 8405837
    };
    citymap['losangeles'] = {
      center: new google.maps.LatLng(34.052234, -118.243684),
      population: 3857799
    };
    citymap['vancouver'] = {
      center: new google.maps.LatLng(49.25, -123.1),
      population: 603502
    };

    var cityCircle;

    function initialize() {
      // Create the map.
      var mapOptions = {
        zoom: 4,
        center: new google.maps.LatLng(37.09024, -95.712891),
        mapTypeId: google.maps.MapTypeId.TERRAIN
      };

      var map = new google.maps.Map(document.getElementById('map-canvas'),
          mapOptions);

      // Construct the circle for each value in citymap.
      // Note: We scale the area of the circle based on the population.
      for (var city in citymap) {
        var populationOptions = {
          strokeColor: '#FF0000',
          strokeOpacity: 0.8,
          strokeWeight: 2,
          fillColor: '#FF0000',
          fillOpacity: 0.35,
          map: map,
          center: citymap[city].center,
          radius: Math.sqrt(citymap[city].population) * 100
        };
        // Add the circle for this city to the map.
        cityCircle = new google.maps.Circle(populationOptions);
      }
    }

    google.maps.event.addDomListener(window, 'load', initialize);

        </script>
      </head>
      <body>
        <div id="map-canvas"></div>
      </body>
    </html>

对于每个城市,我希望在代码中使用不同的颜色。我是 Javascripting 的新手,所以任何人都请帮助我

最佳答案

你可以试试这个方法:

  var citymap = {};
    citymap['chicago'] = {
      center: new google.maps.LatLng(41.878113, -87.629798),
      population: 2714856
    };
    citymap['newyork'] = {
      center: new google.maps.LatLng(40.714352, -74.005973),
      population: 8405837
    };
    citymap['losangeles'] = {
      center: new google.maps.LatLng(34.052234, -118.243684),
      population: 3857799
    };
    citymap['vancouver'] = {
      center: new google.maps.LatLng(49.25, -123.1),
      population: 603502
    };

    var cityCircle;

    function initialize() {
      // Create the map.
      var mapOptions = {
        zoom: 4,
        center: new google.maps.LatLng(37.09024, -95.712891),
        mapTypeId: google.maps.MapTypeId.TERRAIN
      };

      var map = new google.maps.Map(document.getElementById('map-canvas'),
          mapOptions);

      // Construct the circle for each value in citymap.
      // Note: We scale the area of the circle based on the population.
      var fillcolor=[];
      fillcolor[0]='#FF0000';fillcolor[1]='#FFFF00'; fillcolor[2]='#FF00FF'; fillcolor[3]='#00FF00';
      var loop=0;
      for (var city in citymap) {
         
        var populationOptions = {
          strokeColor: '#FF0000',
          strokeOpacity: 0.8,
          strokeWeight: 2,
          fillColor: fillcolor[loop],
          fillOpacity: 0.35,
          map: map,
          center: citymap[city].center,
          radius: Math.sqrt(citymap[city].population) * 100
        };
        // Add the circle for this city to the map.
        cityCircle = new google.maps.Circle(populationOptions);
        loop=loop+1;;
      }
    }

    google.maps.event.addDomListener(window, 'load', initialize);
<!DOCTYPE html>
    <html>
      <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
        <meta charset="utf-8">
        <title>Circles</title>
        <style>
          html, body, #map-canvas {
            height: 100%;
            margin: 0px;
            padding: 0px
          }
        </style>
        <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
        <script>
    // This example creates circles on the map, representing
    // populations in North America.

    // First, create an object containing LatLng and population for each city.
  

        </script>
      </head>
      <body>
        <div id="map-canvas"></div>
      </body>
    </html>

关于javascript - 使用 Google Maps API 在 Google map 上的不同位置使用不同的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27644833/

相关文章:

javascript - 如何解决错误的 jQuery 动画

javascript - 最大左定位

java - 相机停止移动一段时间后更新谷歌地图

android - 如何定位城市或地区?

android - 退出谷歌地图 Intent 在android中

javascript - 如何在运行时正确附加div?

javascript - 定期测试页面加载后添加到 DOM 的元素

ios - 如何更改集群的图标

javascript - 与 window.onload 混淆

google-maps - Google map 服务从v2升级到v3时如何使用PL/SQL生成数字签名?