使用谷歌地图API为谷歌地图上的不同位置提供不同的颜色

Different colours for different locations on Google Maps using Google Maps API

本文关键字:谷歌地图 颜色 位置 API      更新时间:2023-09-26

在此代码中,它用一种颜色标记所有位置。任何人都可以帮我修改代码以用不同的颜色标记每个位置

Source :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>