Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

google maps - Marker Using base64 encoded string

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...