javascript - 使用 Google map 为 geoJson 文件中的点自定义标记

标签 javascript json google-maps google-maps-api-3

我使用 GeoJSON 作为谷歌地图的数据源。我正在使用 API v3 创建数据层,如下所示:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
<script>
var map;
function initialize() {

  //Initialise the map
  var myLatLng = new google.maps.LatLng(53.231472,-0.539783);
  map = new google.maps.Map(document.getElementById('map-canvas'), {
    zoom: 17,
    center: myLatLng,
    scrollwheel: false,
    panControl: false,
    zoomControl: false,
    scaleControl: true,
    disableDefaultUI: true
  });

  //Initialise base folder for icons
  var iconBase = '/static/images/icons/';

  //Load the JSON to show places
  map.data.loadGeoJson('/api/geo');

}

google.maps.event.addDomListener(window, 'load', initialize);
</script>

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

我的 GeoJSON 数据来源如下:
{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "icon": "/static/images/icons/map-marker-do.png",
        "coordinates": [
          -0.53743,
          53.23437
        ]
      },
      "properties": {
        "name": "Cathedral",
        "description": "One of Europe’s finest Gothic buildings, once the tallest building in the world that dominates Lincoln's skyline.",
        "icon": "/static/images/icons/map-marker-do.png"
      }
    }
  ]
}

我想要做的是使 map 上的标记图标来自 JSON 中的“图标”属性,但无法弄清楚如何获取数据以更改默认标记。任何人都可以提供任何建议吗?谢谢。

最佳答案

使用样式功能(样式功能使您可以根据特定功能应用样式)

  map.data.setStyle(function(feature) {
    return {icon:feature.getProperty('icon')};
  });

关于javascript - 使用 Google map 为 geoJson 文件中的点自定义标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24433084/

相关文章:

javascript - 使用javascript更改页面内容后相当于readystatechange

javascript - React 组件提示 SVG 渐变样式?

javascript - 多个文件上传 - Blueimp jQuery uploader

google-maps - 如何使用谷歌地图顺利地为标记制作动画?

javascript - getPlace() 为动态添加的文本输入给出 Undefined

javascript - 计算谷歌地图javascript上绘制多边形的面积

javascript - 如何检查特定元素的 DOM 并创建新的 Vue 实例?

java - JSON 库是否就 .5000...1 的 double 值达成一致?

c++ - 如何使用 C++ Boost 解析 JSON 数组?

android - 如何使用json从android客户端获取字节[]到servlet