[英]Google maps: place number in marker?
How can I display a number in the marker on a google map? 如何在Google地图上的标记中显示数字? I want to do server side clustering and I need to display how many points the cluster represents.
我想做服务器端集群,我需要显示集群代表的点数。
icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=1|FE6256|000000'
Looks fine with 1-digit and 2-digit numbers 1位和2位数字看起来很好
(from Mauro's link) (来自Mauro的链接)
Here are custom icons with the updated "visual refresh" style that you can generate quickly via a simple .vbs script. 以下是具有更新的“可视刷新”样式的自定义图标,您可以通过简单的.vbs脚本快速生成。 I also included a large pre-generated set that you can use immediately with multiple color options: https://github.com/Concept211/Google-Maps-Markers
我还包括一个大型的预生成集,您可以立即使用多种颜色选项: https : //github.com/Concept211/Google-Maps-Markers
Use the following format when linking to the GitHub-hosted image files: 链接到GitHub托管的图像文件时,请使用以下格式:
https://raw.githubusercontent.com/Concept211/Google-Maps-Markers/master/images/marker_[color][character].png
color 颜色
red, black, blue, green, grey, orange, purple, white, yellow
character 字符
A-Z, 1-100, !, @, $, +, -, =, (%23 = #), (%25 = %), (%26 = &), (blank = •)
Examples: 例子:
https://raw.githubusercontent.com/Concept211/Google-Maps-Markers/master/images/marker_red1.png
https://raw.githubusercontent.com/Concept211/Google-Maps-Markers/master/images/marker_red1.png
https://raw.githubusercontent.com/Concept211/Google-Maps-Markers/master/images/marker_blue2.png
https://raw.githubusercontent.com/Concept211/Google-Maps-Markers/master/images/marker_blue2.png
https://raw.githubusercontent.com/Concept211/Google-Maps-Markers/master/images/marker_green3.png
https://raw.githubusercontent.com/Concept211/Google-Maps-Markers/master/images/marker_green3.png
Instead of use the default solution ( http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=7|FF0000|000000 ), you can create these images as you wish, using JavaScript without any server-side code. 您可以使用不带任何服务器端代码的JavaScript,根据需要创建这些图像,而不是使用默认解决方案( http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=7|FF0000|000000 )。
Google google.maps.Marker accepts Base64 for its icon property. Google google.maps.Marker接受Base64作为其图标属性。 With this we can create a valid Base64 from a SVG.
有了这个,我们可以从SVG创建一个有效的Base64。
You can see the code to produce the same as this image in this Plunker: http://plnkr.co/edit/jep5mVN3DsVRgtlz1GGQ?p=preview 您可以在此Plunker中看到与此图像相同的代码: http ://plnkr.co/edit/jep5mVN3DsVRgtlz1GGQ?p = preview
var markers = [ [1002, -14.2350040, -51.9252800], [2000, -34.028249, 151.157507], [123, 39.0119020, -98.4842460], [50, 48.8566140, 2.3522220], [22, 38.7755940, -9.1353670], [12, 12.0733335, 52.8234367], ]; function initializeMaps() { var myLatLng = { lat: -25.363, lng: 131.044 }; var map = new google.maps.Map(document.getElementById('map_canvas'), { zoom: 4, center: myLatLng }); var bounds = new google.maps.LatLngBounds(); markers.forEach(function(point) { generateIcon(point[0], function(src) { var pos = new google.maps.LatLng(point[1], point[2]); bounds.extend(pos); new google.maps.Marker({ position: pos, map: map, icon: src }); }); }); map.fitBounds(bounds); } var generateIconCache = {}; function generateIcon(number, callback) { if (generateIconCache[number] !== undefined) { callback(generateIconCache[number]); } var fontSize = 16, imageWidth = imageHeight = 35; if (number >= 1000) { fontSize = 10; imageWidth = imageHeight = 55; } else if (number < 1000 && number > 100) { fontSize = 14; imageWidth = imageHeight = 45; } var svg = d3.select(document.createElement('div')).append('svg') .attr('viewBox', '0 0 54.4 54.4') .append('g') var circles = svg.append('circle') .attr('cx', '27.2') .attr('cy', '27.2') .attr('r', '21.2') .style('fill', '#2063C6'); var path = svg.append('path') .attr('d', 'M27.2,0C12.2,0,0,12.2,0,27.2s12.2,27.2,27.2,27.2s27.2-12.2,27.2-27.2S42.2,0,27.2,0z M6,27.2 C6,15.5,15.5,6,27.2,6s21.2,9.5,21.2,21.2c0,11.7-9.5,21.2-21.2,21.2S6,38.9,6,27.2z') .attr('fill', '#FFFFFF'); var text = svg.append('text') .attr('dx', 27) .attr('dy', 32) .attr('text-anchor', 'middle') .attr('style', 'font-size:' + fontSize + 'px; fill: #FFFFFF; font-family: Arial, Verdana; font-weight: bold') .text(number); var svgNode = svg.node().parentNode.cloneNode(true), image = new Image(); d3.select(svgNode).select('clippath').remove(); var xmlSource = (new XMLSerializer()).serializeToString(svgNode); image.onload = (function(imageWidth, imageHeight) { var canvas = document.createElement('canvas'), context = canvas.getContext('2d'), dataURL; d3.select(canvas) .attr('width', imageWidth) .attr('height', imageHeight); context.drawImage(image, 0, 0, imageWidth, imageHeight); dataURL = canvas.toDataURL(); generateIconCache[number] = dataURL; callback(dataURL); }).bind(this, imageWidth, imageHeight); image.src = 'data:image/svg+xml;base64,' + btoa(encodeURIComponent(xmlSource).replace(/%([0-9A-F]{2})/g, function(match, p1) { return String.fromCharCode('0x' + p1); })); } initializeMaps();
#map_canvas { width: 100%; height: 300px; }
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="style.css"> <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> </head> <body> <div id="map_canvas"></div> </body> <script src="script.js"></script> </html>
In this demo I create the SVG using D3.js, then transformed SVG to Canvas, so I can resize the image as I want and after that I get Base64 using canvas' toDataURL method. 在这个演示中,我使用D3.js创建SVG,然后将SVG转换为Canvas,这样我就可以根据需要调整图像大小,之后我使用canvas'toDataURL方法获得Base64。
All this demo was based on my fellow @thiago-mata 's code. 所有这些演示都基于我的同事@thiago-mata的代码。 Kudos for him.
感谢他。
Latest google js API has google.maps.MarkerLabel object. 最新的google js API有google.maps.MarkerLabel对象。
So you can easily set text/styles for labels 因此,您可以轻松设置标签的文本/样式
var mIcon = {
path: google.maps.SymbolPath.CIRCLE,
fillOpacity: 1,
fillColor: '#fff',
strokeOpacity: 1,
strokeWeight: 1,
strokeColor: '#333',
scale: 12
};
var gMarker = new google.maps.Marker({
map: gmap,
position: latLng,
title: 'Number 123',
icon: mIcon,
label: {color: '#000', fontSize: '12px', fontWeight: '600',
text: '123'}
});
Just found this tutorial: http://biostall.com/adding-number-or-letters-to-google-maps-api-markers 刚刚找到这个教程: http : //biostall.com/adding-number-or-letters-to-google-maps-api-markers
It doesn't look like the best solution, but it does work. 它看起来不是最好的解决方案,但确实有效。
You can use labels over markers, here is a tutorial about GIcon.label . 您可以在标记上使用标签, 这是一个关于GIcon.label的教程 。
You can also use GMarker.openInfoWindow . 您也可以使用GMarker.openInfoWindow 。
Tip: This is the best tutorial I found about google maps api (of course after Official documentation ) 提示: 这是我发现的关于google maps api 的最佳教程 (当然在官方文档之后 )
Simplest solution: 最简单的解决方案
marker = new google.maps.Marker({
position: my_position,
map: map,
label: num_events+'' //Needs to be a string. No integers allowed
});
See https://developers.google.com/maps/documentation/javascript/examples/marker-labels 请参阅https://developers.google.com/maps/documentation/javascript/examples/marker-labels
For more control over the layout of the marker see Valery Viktorovsky's answer 为了更好地控制标记的布局,请参阅Valery Viktorovsky的回答
<hr/>
1. add Google maps script To _Layout page.<br/>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"> </script >
<hr/>
2. Add script to your view page.
<script type="text/javascript" ><br/>
var mapLocation = [['Lucknow', 26.74561, 80.859375],<br/>
['New Delhi', 28.613459, 77.695313],<br/>
['Jaipur', 26.980829, 75.849609],<br/>
['Ahmedabad', 22.674847, 72.333984],<br/>
['Mumbai', 18.760713, 73.015135]];<br/>
$(document).ready(function () { initialize(); });
//At view initialize load default map //在视图初始化加载默认地图
function initialize() {<br/>
var latLng = new google.maps.LatLng(22.917923, 76.992188);<br/>
var myOptions = { center: latLng, zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
};<br/>
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
setMarker(map, mapLocation);
}
function setMarker(map, mapLoc) {
for (i = 0; i < mapLoc.length; i++) {
var loca = mapLoc[i];
var myLanLat = new google.maps.LatLng(loca[1], loca[2]);
var marker = new google.maps.Marker({
position: myLanLat,
icon:'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld='+ ( i + 1) +'|FF776B|000000',
shadow:'https://chart.googleapis.com/chart?chst=d_map_pin_shadow',
map: map,
tittle: loca[0],
zIndex: loca[3]
});
}
}
while creating marker use the
<script>
var marker = new google.maps.Marker({
position: myLanLat,
icon:'icon: 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld='+ (position) +'|FF776B|000000',
map: map,
});
<script>
The link above (' http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=1|FE6256|000000 ') can't be used via SSL. 上述链接(' http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=1|FE6256|000000 ')无法通过SSL使用。 To generate and store the number images locally:
要在本地生成和存储数字图像:
for i in {1..99}; do curl -o ./${i}_map_icon.png "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=${i}|FE6256|000000"; echo $i; done
Best solution would be to pass a remote or local image and text to a server side script through a url. 最佳解决方案是通过URL将远程或本地映像和文本传递到服务器端脚本。 When plotting the markers, you would use this url as the icon's value, and the server side script return a copy of your supplied image (never saved on the server mind you) with the text baked into image.
在绘制标记时,您将使用此URL作为图标的值,服务器端脚本将返回所提供图像的副本(从未保存在服务器上),并将文本烘焙到图像中。 Thus you could render numbers or text on custom marker images real-time as you drop them on the map.
因此,当您将自己的标记图像放在地图上时,可以实时渲染自定义标记图像上的数字或文本。
Here is the tutorial on my blog on how to do this. 以下是我的博客上有关如何执行此操作的教程。 - http://www.devinrolsen.com/google-maps-marker-icons-with-numbers-using-php-gd/
- http://www.devinrolsen.com/google-maps-marker-icons-with-numbers-using-php-gd/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.