我正在尝试使用一个base64编码字符串在Google Maps上使用自定义标记,但不知何故它无法工作。
我正在尝试使用一个base64编码字符串在Google Maps上使用自定义标记,但不知何故它无法工作。
尝试按照以下步骤进行:
var marker = new google.maps.Marker({
position: latLng,
map: map,
title: 'hello',
id: 'hehehe',
icon: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAA..."
});
编辑:补充一下,如果你使用的是服务器端语言来生成JS,你可以随时插入一些PHP/Python或其他代码来加载图像并将其转换为它的Base64表示形式。
例如(PHP后端):
$path = 'path/to/my/image.ext';
$info = getimagesize($info);
$ext = ($info[2]);
$data = file_get_contents($path);
$encoded = 'data:image/' . $ext . ';base64,' .base64_encode($data);
那么(前端):
var marker = new google.maps.Marker({
//...
icon: '<?=$encoded;?>'
});