简体   繁体   English

如何在 Google 地图 api 中设置数据层特征标签的样式?

[英]How to set style of a data layer feature label in Google maps api?

I've got this working js code:我有这个有效的 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);

The code generate this output:代码生成此输出:

形象 :

How can I set the style of the label "L1PIAZZA"?如何设置标签“L1PIAZZA”的样式?

Use the documented properties to style the MarkerLabel :使用记录的属性来设置MarkerLabel 的样式:

Properties属性

color Type: string颜色类型:字符串

The color of the label text.标签文本的颜色。 Default color is black.默认颜色为黑色。

fontFamily Type: string fontFamily类型:字符串

The font family of the label text (equivalent to the CSS font-family property).标签文本的字体系列(相当于 CSS font-family 属性)。

fontSize Type: string字体大小类型:字符串

The font size of the label text (equivalent to the CSS font-size property).标签文本的字体大小(相当于 CSS font-size 属性)。 Default size is 14px.默认大小为 14px。

fontWeight Type: string fontWeight类型:字符串

The font weight of the label text (equivalent to the CSS font-weight property).标签文本的字体粗细(相当于 CSS font-weight 属性)。

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概念证明小提琴

在地图上生成的标记标记的屏幕截图

code snippet:代码片段:

 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM