javascript - 如何在 Google map API 中设置数据层特征标签的样式?

标签 javascript google-maps-api-3 geojson

我有这个有效的 js 代码:

var p;
p = new google.maps.Data();
p.loadGeoJson('http://mysource.example.com');
p.setStyle( function(feature){
    var icon = feature.getProperty('icon');
    var title = feature.getProperty('title');
    return  { 
        icon: icon,
        label: title
    }
});
p.setMap(map);

代码生成此输出:

image :

如何设置标签“L1PIAZZA”的样式?

最佳答案

使用记录的属性来设置 MarkerLabel 的样式:

Properties

color Type: string

The color of the label text. Default color is black.

fontFamily Type: string

The font family of the label text (equivalent to the CSS font-family property).

fontSize Type: string

The font size of the label text (equivalent to the CSS font-size property). Default size is 14px.

fontWeight Type: string

The font weight of the label text (equivalent to the CSS font-weight property).

text Type: string

The text to be displayed in the label.

p.setStyle(function(feature) {
  var icon = feature.getProperty('icon');
  var title = feature.getProperty('title');
  return {
    icon: {
      url: icon,
      labelOrigin: new google.maps.Point(15, -10)
    },
    label: {
      color: "blue",
      fontFamily: "Courier",
      fontSize: "24px",
      fontWeight: "bold",
      text: title
    }
  }
});

proof of concept fiddle

screenshot of resulting labelled marker on map

代码片段:

var geocoder;
var map;

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var p;
  p = new google.maps.Data();
  p.addGeoJson({
    "type": "FeatureCollection",
    "features": [{
      "type": "Feature",
      "properties": {
        "icon": "http://maps.google.com/mapfiles/ms/micons/blue.png",
        "title": "blue",
      },
      "geometry": {
        "type": "Point",
        "coordinates": [-122.1419, 37.4419]
      }
    }]
  });
  p.setStyle(function(feature) {
    var icon = feature.getProperty('icon');
    var title = feature.getProperty('title');
    return {
      icon: {
        url: icon,
        labelOrigin: new google.maps.Point(15, -10)
      },
      label: {
        color: "blue",
        fontFamily: "Courier",
        fontSize: "24px",
        fontWeight: "bold",
        text: title
      }
    }
  });
  p.setMap(map);
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>

关于javascript - 如何在 Google map API 中设置数据层特征标签的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40785053/

相关文章:

javascript - 如何将按钮组的 html 名称传递给 javascript 函数

javascript - 检测从 angularjs 中的 ngclick 调用了一个函数

google-maps - Google Maps v3 自定义控件 - 与 map 外的控件交互

android - 在谷歌地图中沿着道路画一条线

javascript - 如何使用 onEachFeature 和 geojson 将标记添加到传单中的不同图层

javascript - Ionic 3 减少启动时间

javascript - 未捕获( promise )TypeError : response. json 不是函数

javascript - Google Maps API 错误,页面代码未更改

javascript - 操作 GeoJSON 数据 - 在 OpenLayers 中重新加载矢量图层

javascript - Turf js绘制具有中心点和半径的完美正方形