将GeoJson LineString格式化为Dashed

时间:2015-03-02 04:20:59

标签: 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]]
                    }
                ]
            }
        }]
}

我可以在法线显示它,但我希望显示为虚线。 我谷歌有一种方法:使用折线,但我不知道如何将其转换为折线。

请帮忙。谢谢:)。

1 个答案:

答案 0 :(得分:2)

要使折线虚线,您必须创建一个原生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>