javascript - 带有 D3.geo.path 的谷歌地图

标签 javascript google-maps d3.js

我正在尝试按照本视频中的示例进行操作:

https://www.youtube.com/watch?v=wqPGFs0cqxI

这是关于使用 D3.js 将路径绘制到 Google Maps API 中。控制台向我显示错误 Uncaught TypeError: Object#<PolylineContext>" has no method 'setCurrent' .

index.html

<head>
  <title>App</title>
  <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map-canvas { height: 100%; }
    </style>
  <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false">
    </script>
    <script src="http://d3js.org/d3.v3.js" charset="utf-8"></script>
    <script src="polyline_context.js"></script>
    <script type="text/javascript">
    var map;
    var polyline;
      function initialize() {
        var mapOptions = {
          center: new google.maps.LatLng(53.567, 9.944),
          zoom: 2,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map-canvas"),
            mapOptions);
        polyline = new google.maps.Polyline({
          map: map
        });

        d3init();
      }
      var context;
      var width;
      var height;
      var path;
      var graticule;
      var equator;
      var projection;

      function d3init() {
        width = map.getDiv().offsetWidth;
        height = map.getDiv().offsetHeight;

        projection = d3.geo.equirectangular()
          .translate([0, 0])
          .scale(52.29578)
          .precision(2)

        context = new PolylineContext();
        path = d3.geo.path().projection(projection).context(context);
        equator = {type: 'LineString', coordinates: [[-180, 20], [-90, 0], [0, -20], [90, 0], [180, 20]]};

        render();
      }

      function render() {
        polyline.setOptions({
          strokeColor: 'red',
          strokeWeight: 2
        });
        context.setCurrent(polyline.getPath());

        path(equator);
      }

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


    </script>
</head>

<body>

    <div id="map-canvas">
    </div>
</body>

Polyline_context.js

'use strict';
function PolylineContext () {
    this.currentPath = null;
    this.currentIndex = 0;
}

PolylineContext.prototype.beginPath = function() {};

PolylineContext.prototype.moveTo = function(x, y) {
    if (this.currentPath) {
        var latLng = new google.maps.LatLng(y, x);
        this.currentPath.setAt(this.currentIndex, latLng);
        this.currentIndex++;
    }
};

PolylineContext.prototype.lineTo = function(x, y) {
    if (this.currentPath) {
        var latLng = new google.maps.LatLng(y, x);
        this.currentPath.setAt(this.currentIndex, latLng);
        this.currentIndex++;
    }
};

PolylineContext.prototype.arc = function(x, y, radius, startAngle, endAngle) {};

PolylineContext.prototype.closePath = function() {};

你知道这里出了什么问题吗?

最佳答案

相当古老的问题。但是刚刚开始使用相同的主题。以防万一其他人偶然发现了这一点。只需将其放入 polyline_context.js 文件中,即可看到它生效:

PolylineContext.prototype.setCurrent = function (path) {
    this.currentPath = path;
};

视频中展示的和需要实现的不一定同步。

关于javascript - 带有 D3.geo.path 的谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18854851/

相关文章:

android - Android 中的 INSTALL_FAILED_MISSING_SHARED_LIBRARY 错误

d3.js - 如何在 D3 中使用 XHR 加载数据

javascript - 使用 Canvas 在 Javascript 中制作动画

javascript - 我的脚本没有显示真正可见的元素

javascript - 为什么IE在使用ajax时不喜欢越南字符?

javascript - 如何使用 Handlebars 将输入名称属性设置为等于变量?

javascript - Google map API 有错误 "uncaught referenceerror: system is not defined"

reactjs - google-map-react 和 google-maps-react 之间的区别?

javascript - 使用 queue.js 将 HTTP header 添加到 d3.json

javascript - D3.js 分组条形图渲染 x 轴不正确