如何在谷歌地图上叠加条形图?

时间:2017-04-26 07:00:06

标签: javascript google-maps google-maps-api-3 charts

我有一张谷歌地图,其中不同的城市标有条形图,显示在特定城市/城镇销售的食物的百分比。

在这个城市的每个城市,我都需要一个显示这些统计数据的条形图。 “条形图图标”应略大,以便可见。应根据与该特定城市相关的实数绘制每个条形图。

目前我正试图将“绘制的图表”“转换”为“动态”的png图像,然后计划将该图像叠加到谷歌地图上。但是代码没有用。

1)是否可以使这种方法有效?我的最终目标是在谷歌地图上叠加真正的条形图。有没有其他方法来实现此功能。 2)转换的png图像需要具有透明背景。

感谢。 代码是:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Adding a Custom Overlay</title>
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
	        #chart_div {
        display: none;
		height:100px;
		width: 100px;
		background-color: #00f; 
		margin: 0px; 
		padding: 0px;
      }
    </style>
	
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBGCabKYKRUI0-5zvdIcA_mRMImMecdNKM"></script>
	
	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script>
      
	        // Load the Visualization API and the corechart package.
      google.charts.load('current', {'packages':['corechart']});

      // Set a callback to run when the Google Visualization API is loaded.
      google.charts.setOnLoadCallback(initMap);
	  // This example creates a custom overlay called USGSOverlay, containing
      // a U.S. Geological Survey (USGS) image of the relevant area on the map.

      // Set the custom overlay object's prototype to a new instance
      // of OverlayView. In effect, this will subclass the overlay class therefore
      // it's simpler to load the API synchronously, using
      // google.maps.event.addDomListener().
      // Note that we set the prototype to an instance, rather than the
      // parent class itself, because we do not wish to modify the parent class.

	  
      var overlay;
      USGSOverlay.prototype = new google.maps.OverlayView();

      // Initialize the map and the custom overlay.

      function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 11,
          center: {lat: 62.323907, lng: -150.109291},
          mapTypeId: 'satellite'
        });

        var bounds = new google.maps.LatLngBounds(
            new google.maps.LatLng(62.281819, -150.287132),
            new google.maps.LatLng(62.400471, -150.005608));

        // The photograph is courtesy of the U.S. Geological Survey.
        var srcImage = 'https://developers.google.com/maps/documentation/' +
            'javascript/examples/full/images/talkeetna.png';

	        // Create the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Topping');
        data.addColumn('number', 'Slices');
        data.addRows([
          ['Mushrooms', 45],
          ['Onions', 9],
          ['Olives', 11],
          ['Zucchini', 15],
          ['Pepperoni', 20]
        ]);

        // Set chart options
        var options = {'title':'How Much Pizza I Ate Last Night',
                       'width':"100px",
                       'height':"100px"};

 /*       // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
      //  chart.draw(data, options);
				
	*/

		var chart_div = document.getElementById('chart_div');
      var chart = new google.visualization.ColumnChart(chart_div);
	  
	    google.visualization.events.addListener(chart, 'ready', function () {
	    chart_div.innerHTML = '<img src="' + chart.getImageURI() + '">';
        console.log(chart_div.innerHTML);
		console.log( chart.getImageURI());
		     // The custom USGSOverlay object contains the USGS image,
        // the bounds of the image, and a reference to the map.
       // overlay = new USGSOverlay(bounds, chart.getImageURI(), map);
	   overlay = new USGSOverlay(bounds, chart.getImageURI(), map);
      });
		
     chart.draw(data, options);
      }

	
      /** @constructor */
      function USGSOverlay(bounds, image, map) {

        // Initialize all properties.
        this.bounds_ = bounds;
        this.image_ = image;
        this.map_ = map;

        // Define a property to hold the image's div. We'll
        // actually create this div upon receipt of the onAdd()
        // method so we'll leave it null for now.
        this.div_ = null;

        // Explicitly call setMap on this overlay.
        this.setMap(map);
      }

      /**
       * onAdd is called when the map's panes are ready and the overlay has been
       * added to the map.
       */
      USGSOverlay.prototype.onAdd = function() {

        var div = document.createElement('div');
        div.style.borderStyle = 'none';
        div.style.borderWidth = '0px';
        div.style.position = 'absolute';

        // Create the img element and attach it to the div.
        var img = document.createElement('img');
        img.src = this.image_;
        img.style.width = '100%';
        img.style.height = '100%';
        img.style.position = 'absolute';
        div.appendChild(img);

        this.div_ = div;

        // Add the element to the "overlayLayer" pane.
        var panes = this.getPanes();
        panes.overlayLayer.appendChild(div);
      };

      USGSOverlay.prototype.draw = function() {

        // We use the south-west and north-east
        // coordinates of the overlay to peg it to the correct position and size.
        // To do this, we need to retrieve the projection from the overlay.
        var overlayProjection = this.getProjection();

        // Retrieve the south-west and north-east coordinates of this overlay
        // in LatLngs and convert them to pixel coordinates.
        // We'll use these coordinates to resize the div.
        var sw = overlayProjection.fromLatLngToDivPixel(this.bounds_.getSouthWest());
        var ne = overlayProjection.fromLatLngToDivPixel(this.bounds_.getNorthEast());

        // Resize the image's div to fit the indicated dimensions.
        var div = this.div_;
        div.style.left = sw.x + 'px';
        div.style.top = ne.y + 'px';
        div.style.width = (ne.x - sw.x) + 'px';
        div.style.height = (sw.y - ne.y) + 'px';
      };

      // The onRemove() method will be called automatically from the API if
      // we ever set the overlay's map property to 'null'.
      USGSOverlay.prototype.onRemove = function() {
        this.div_.parentNode.removeChild(this.div_);
        this.div_ = null;
      };

      google.maps.event.addDomListener(window, 'load', initMap);
    </script>
  </head>
  <body>
    <div id="chart_div"></div>
	
	<div id="map"></div>
	
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

请参阅此处了解您在Google图表Google Charts (JS) - is there a way of using a transparent background on a chart?

上的透明度解决方案
// In Chart options include {'backgroundColor': 'transparent'}

http://jsfiddle.net/farrukhsubhani/d2ruuhgu/链接是从其中一个答案中分出来的,以确保我可以参考它。信用仍然归于原始答案和作者。