【问题标题】:Load Heatmap LatLng Coordinates Google Maps API from MySQL database从 MySQL 数据库加载 Heatmap LatLng Coordinates Google Maps API
【发布时间】:2017-09-23 07:36:54
【问题描述】:

我有项目 GIS。我想使用 Google Maps API 中的 Google Heatmap 创建热点犯罪地图。我在 MySQL 数据库上有一堆设置犯罪位置(包括纬度和经度)的数据。我已阅读 Google Maps API Doc 示例:https://developers.google.com/maps/documentation/javascript/heatmaplayer

有一个使用函数 getPoints() 创建热图的示例,但使用 LatLng 硬编码,而不是来自数据库。将 LatLng 从 SQL 数据库和数组加载到新的 google.maps.LatLng 的任何可能方式?

这是我的 Heatmap Google Maps API 源代码:

<script async defer src="https://maps.googleapis.com/maps/api/js?key=API&libraries=visualization&callback=initMap">

    <div id="floating-panel">
      <button onclick="toggleHeatmap()">Toggle Heatmap</button>
      <button onclick="changeGradient()">Change gradient</button>
      <button onclick="changeRadius()">Change radius</button>
      <button onclick="changeOpacity()">Change opacity</button>
    </div>
    <div id="map"></div>
    <script>
      var map, heatmap;
      function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
          zoom: 2,
          center: {lat: 51.508530, lng: -0.076132},
          mapTypeId: 'satellite'
        });
        heatmap = new google.maps.visualization.HeatmapLayer({
          data: getPoints(),
          map: map
        });
      }
      function toggleHeatmap() {
        heatmap.setMap(heatmap.getMap() ? null : map);
      }
      function changeGradient() {
        var gradient = [
          'rgba(0, 255, 255, 0)',
          'rgba(0, 255, 255, 1)',
          'rgba(0, 191, 255, 1)',
          'rgba(0, 127, 255, 1)',
          'rgba(0, 63, 255, 1)',
          'rgba(0, 0, 255, 1)',
          'rgba(0, 0, 223, 1)',
          'rgba(0, 0, 191, 1)',
          'rgba(0, 0, 159, 1)',
          'rgba(0, 0, 127, 1)',
          'rgba(63, 0, 91, 1)',
          'rgba(127, 0, 63, 1)',
          'rgba(191, 0, 31, 1)',
          'rgba(255, 0, 0, 1)'
        ]
        heatmap.set('gradient', heatmap.get('gradient') ? null : gradient);
      }
      function changeRadius() {
        heatmap.set('radius', heatmap.get('radius') ? null : 20);
      }
      function changeOpacity() {
        heatmap.set('opacity', heatmap.get('opacity') ? null : 0.2);
      }

// This is the LatLng I meant, any possible way to load these from a database? 
      function getPoints() {
        return [
          new google.maps.LatLng(40.761916,-73.9228569),
          new google.maps.LatLng(42.35047,-71.07613),
          new google.maps.LatLng(42.3456382751,-71.0715713501),
          new google.maps.LatLng(42.3429222107,-71.078666687),
          new google.maps.LatLng(42.3450012207,-71.0808029175),
          new google.maps.LatLng(42.3429222107,-71.078666687),
          new google.maps.LatLng(42.35521698,-71.0732955933),
          new google.maps.LatLng(42.35521698,-71.0732955933),
          new google.maps.LatLng(42.3903999329,-71.1128997803),
          new google.maps.LatLng(42.3641662598,-71.0299758911),
          new google.maps.LatLng(43.6952018738,-79.285987854),
          new google.maps.LatLng(40.7427902222,-73.9834136963),
          new google.maps.LatLng(43.6991958618,-79.2756347656),
          new google.maps.LatLng(41.9590682983,-87.7312164307)
        ]
      }
    </script>

【问题讨论】:

标签: javascript mysql google-maps google-maps-api-3 coordinates


【解决方案1】:

是的,您需要执行以下操作:

  1. 创建一个连接到数据库并在您的 lat lng 表上运行查询的服务器端脚本。
  2. 创建 php 代码以将结果格式化为 json 数组并将其作为 json 输出到浏览器
  3. 在客户端进行 ajax 调用以从 url 获取此 json
  4. 一旦 ajax 返回并返回有效结果,只需对其进行迭代,如果需要,将其格式化为您在 getPoints 函数中需要的类似对象,而不是返回长数组,只需返回该对象。
  5. 触发地图刷新功能(您将需要编写此功能)可能基于包含您在 4 中创建的对象的参数,然后执行所有地图功能。

【讨论】:

  • 那么sql数据库必须通过php代码转成Json格式?
  • 它可以使用节点或任何服务器端技术进行转换。您将需要在服务器上执行 SQL 语句然后格式化输出。
猜你喜欢
  • 2012-07-24
  • 1970-01-01
  • 2011-07-31
  • 1970-01-01
  • 1970-01-01
  • 2020-12-24
  • 2022-06-15
  • 2012-12-06
  • 2013-09-07
相关资源
最近更新 更多