【问题标题】:styling geojson point in google maps api在谷歌地图api中设置geojson点的样式
【发布时间】:2018-08-19 08:30:41
【问题描述】:

我正在使用 google maps api 和 geojson 数据文件进行地图项目。我的问题是我不知道如何将自定义样式应用于我的观点。这就是我加载数据的方式:

      <script>
var map;
function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 11,
    center: {lat: 43.15, lng: -84.75}
  });

  map.data.loadGeoJson(
      'Maps/Newark.geojson');

      map.data.loadGeoJson('Maps/Newark.geojson');
     map.data.setStyle(//* I'm not sure what to put here*//)


}
</script>

这是我的 geojson 文件的示例:

    {
   "type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
   "type": "Point",
   "coordinates":  [ -84.69729,43.24808 ]
},
"properties": {}
},
 {
"type": "Feature",
"geometry": {
   "type": "Point",
   "coordinates":  [ -84.58872,43.23395 ]
},
"properties": {}
}
}

【问题讨论】:

    标签: html google-maps-api-3 geojson


    【解决方案1】:

    要将标记的样式设置为自定义图标,请使用Data.StyleOptions 对象(如果您为图标提供字符串,它会将其视为 URL):

    map.data.setStyle({
      icon: "http://maps.google.com/mapfiles/ms/micons/blue.png"
    });
    

    proof of concept fiddle

    代码 sn-p:

    var map;
    
    function initMap() {
      map = new google.maps.Map(document.getElementById('map'), {
        zoom: 9,
        center: {
          lat: 43.15,
          lng: -84.75
        }
      });
      map.data.addGeoJson(geoJson);
      map.data.setStyle({
        icon: "http://maps.google.com/mapfiles/ms/micons/blue.png"
      });
    
    
    }
    google.maps.event.addDomListener(window, "load", initMap);
    var geoJson = {
      "type": "FeatureCollection",
      "features": [{
          "type": "Feature",
          "geometry": {
            "type": "Point",
            "coordinates": [-84.69729, 43.24808]
          },
          "properties": {}
        },
        {
          "type": "Feature",
          "geometry": {
            "type": "Point",
            "coordinates": [-84.58872, 43.23395]
          },
          "properties": {}
        }
      ]
    };
    html,
    body,
    #map {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <div id="map"></div>

    【讨论】:

    • 谢谢!一个问题是我在哪里可以找到不同的风格。就像我想要一个银行符号、房子或普通圆圈一样。
    • 那是另一个问题。可能是Google Map API Marker Icon Url? 的副本
    猜你喜欢
    • 2020-03-28
    • 2014-10-05
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    相关资源
    最近更新 更多