如何在Google maps api中设置数据层功能标签的样式?

时间:2016-11-24 11:19:01

标签: 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"?

1 个答案:

答案 0 :(得分:4)

使用记录的属性设置MarkerLabel

的样式
  

<强>属性

     

颜色类型:字符串

     

标签文字的颜色。默认颜色为黑色。

     

fontFamily 类型:字符串

     

标签文本的字体系列(相当于CSS font-family属性)。

     

fontSize 类型:字符串

     

标签文本的字体大小(相当于CSS font-size属性)。默认大小为14px。

     

fontWeight 类型:字符串

     

标签文本的字体粗细(相当于CSS font-weight属性)。

     

文字类型:字符串

     

标签中显示的文字。

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

代码段

&#13;
&#13;
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);
&#13;
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>
&#13;
&#13;
&#13;