Marker Using base64 encoded string

China☆狼群 提交于 2019-11-28 13:18:09

Try doing it as follows:

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

Edit: 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.

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);

Then (front-end):

var marker = new google.maps.Marker({
    //...
    icon: '<?=$encoded;?>'
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!