javascript - 将 GeoJson LineString 格式化为虚线

标签 javascript google-maps-api-3

这是我的 GeoJson:

{
    "type" : "FeatureCollection",
    "created" : "2014/07/08 03:00:55 GMT",
    "announced_date" : "2017/07/10 03:00:55 GMT",
    "features" : [{
            "type" : "Feature",
            "properties" : {
                "name" : "n18",
                "analized_date" : "2013/07/08 10:00:00 GMT"
            },
            "geometry" : {
                "type" : "GeometryCollection",
                "geometries" : [{
                        "type" : "Point",
                        "coordinates" : [134.7, 37.3]
                    }, {
                        "type" : "LineString",
                        "coordinates" : [[134.7, 37.3], [134.6, 37.1]]
                    }
                ]
            }
        }]
}

我可以用普通线显示,但我想用虚线显示。 我谷歌了一下,有一种方法:使用 Polyline 但我不知道如何将其转换为 Polyline。

请帮忙。谢谢 :)。

最佳答案

要使折线变为虚线,您必须创建原生的 google.maps.Polyline目的。一种方法是使用数据层加载 GeoJSON,然后使用其方法从 GeoJSON 创建折线:

代码片段:

function initialize() {
  // Create a simple map.
  map = new google.maps.Map(document.getElementById('map-canvas'), {
    zoom: 8,
    center: {
      lat: 37,
      lng: 134
    }
  });
  google.maps.event.addListener(map, 'click', function() {
    infowindow.close();
  });
  // process the loaded GeoJSON data.
  google.maps.event.addListener(map.data, 'addfeature', function(e) {
    if (e.feature.getGeometry().getType() === 'GeometryCollection') {
      var geometry = e.feature.getGeometry().getArray();
      for (var i = 0; i < geometry.length; i++) {
        if (geometry[i].getType() === 'Point') {
          map.setCenter(geometry[i].get());
          new google.maps.Marker({
            map: map,
            position: geometry[i].get()
          });
        } else if (geometry[i].getType() === 'LineString') {
          new google.maps.Polyline({
            map: map,
            path: geometry[i].getArray(),
            // make the polyline dashed. From the example in the documentation:
            // https://developers.google.com/maps/documentation/javascript/examples/overlay-symbol-dashed
            strokeOpacity: 0,
            icons: [{
              icon: {
                path: 'M 0,-1 0,1',
                strokeOpacity: 1,
                scale: 4
              },
              offset: '0',
              repeat: '20px'
            }]
          })
        }
      }
    }
    map.data.setMap(null);
  });
  map.data.addGeoJson(data);
}
google.maps.event.addDomListener(window, 'load', initialize);
var data = {
"type" : "FeatureCollection",
"created" : "2014/07/08 03:00:55 GMT",
"announced_date" : "2017/07/10 03:00:55 GMT",
"features" : [{
        "type" : "Feature",
        "properties" : {
            "name" : "n18",
            "analized_date" : "2013/07/08 10:00:00 GMT"
        },
        "geometry" : {
            "type" : "GeometryCollection",
            "geometries" : [{
                    "type" : "Point",
                    "coordinates" : [134.7, 37.3]
                }, {
                    "type" : "LineString",
                    "coordinates" : [[134.7, 37.3], [134.6, 37.1]]
                }
            ]
        }
    }]
};
html,
body {
  height: 100%;
  margin: 0px;
  padding: 0px;
  width: 100%;
}
#map-canvas {
  height: 100%;
  width: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map-canvas"></div>

关于javascript - 将 GeoJson LineString 格式化为虚线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28802664/

相关文章:

javascript - 这是来自 chart.js 的自定义问题

javascript - 如何根据谷歌地图上的视口(viewport)调整边界?

Javascript在按钮单击时清除表单字段

google-maps - 如何打印谷歌地图标记

javascript - 获取函数而不是运行函数时引发错误的任何方法

javascript - AngularJS 1.5 当模板组件内部发生变化时如何获取事件

javascript - Chrome、Firefox 和 Opera 中的 CSS 转换问题,但 IE 中没有

javascript - 如何使用javascript在谷歌地图上输出geojon点?

javascript - 带有多个标记的谷歌地图 javascript => 标签文本在循环中无处不在

javascript - 如何将多个项目添加到列表中