來源:Multiline/Wrap Text Support - GitHub
方法1:
爲了得到多支持,補充一點:
MapLabel.prototype.wrapText = function(context, text, x, y, maxWidth, lineHeight) {
var words = text.split(' ');
var line = '';
for(var n = 0; n < words.length; n++) {
var testLine = line + words[n] + ' ';
var metrics = context.measureText(testLine);
var testWidth = metrics.width;
if (testWidth > maxWidth && n > 0) {
context.strokeText(line, x, y);
context.fillText(line, x, y);
line = words[n] + ' ';
y += lineHeight;
}
else {
line = testLine;
}
}
context.strokeText(line, x, y);
context.fillText(line, x, y);
};
在drawCanvas_,變更
if (strokeWeight) {
ctx.lineWidth = strokeWeight;
ctx.strokeText(text, strokeWeight, strokeWeight);
}
ctx.fillText(text, strokeWeight, strokeWeight);
到
if (strokeWeight) {
ctx.lineWidth = strokeWeight;
}
this.wrapText(ctx, text, strokeWeight, strokeWeight, *ADD MAX WIDTH*, *ADD LINEHEIGHT*);
//e.g. this.wrapText(ctx, text, strokeWeight, strokeWeight, 200, 14);
此代碼是的擴展: http://www.html5canvastutorials.com/tutorials/html5-canvas-wrap-text-tutorial/
方式2:
替換:
if (strokeWeight) {
ctx.lineWidth = strokeWeight;
ctx.strokeText(text, strokeWeight, strokeWeight);
}
ctx.fillText(text, strokeWeight, strokeWeight);
與
if (strokeWeight) {
ctx.lineWidth = strokeWeight;
// ctx.strokeText(text, strokeWeight, strokeWeight);
}
// ctx.fillText(text, strokeWeight, strokeWeight);
var lineheight = 15;
var lines = text.split('\n');
for (var i = 0; i<lines.length; i++) {
ctx.fillText(lines[i], strokeWeight, strokeWeight + (i * lineheight));
if (strokeWeight) {
ctx.lineWidth = strokeWeight;
ctx.strokeText(lines[i], strokeWeight, strokeWeight + (i * lineheight));
}
}
Infowindows和谷歌地圖的其他元素使用html標記。使用
– Rafe 2013-03-18 02:45:09
我試過「<%= zipCentroid [i]%>」+
+「<%= colorCount [i]%>」+「<%= layerType%>」,並且不起作用。如果我將
放在引號中,它只會顯示爲文本 – 2013-03-18 02:53:40