标记使用 base64 编码的字符串 [英] Marker Using base64 encoded string

查看:23
本文介绍了标记使用 base64 编码的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过使用 base64 编码字符串在 Google 地图上使用自定义标记.不知何故,它不起作用.

I am trying to use a custom marker on a Google Maps by using a base64 encoded string. Somehow it doesn't work.

推荐答案

尝试如下:

var marker = new google.maps.Marker({
    position: latLng,
    map: map,
    title: 'hello',
    id: 'hehehe',
    icon: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAA..."
});

只是补充:如果你使用服务器端语言来生成 js,你总是可以插入一些 PHP/Python/任何代码来加载图像并将其转换为它的 base64 表示.

Just complementing: if you're using a server side language to generate the js, you can always insert some PHP/Python/whatever code to load the image and convert it to its base64 representation.

类似(PHP 后端):

Something like (PHP back-end):

$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;?>'
});

这篇关于标记使用 base64 编码的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆