如何在Leaflet ImageOverlay中使用base64字符串而不是URL
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Leaflet ImageOverlay中使用base64字符串而不是URL相关的知识,希望对你有一定的参考价值。
我正在使用ImageOverlay图层使用Leaflet在地图上打印叠加图像。如果我使用可在线访问的图像,即使用Leaflet example:
var imageUrl = 'http://www.lib.utexas.edu/maps/historical/newark_nj_1922.jpg',
imageBounds = [[40.712216, -74.22655], [40.773941, -74.12544]];
L.imageOverlay(imageUrl, imageBounds).addTo(map);
一切正常。但是,我想使用base64编码的字符串作为图像。此字符串在我的脚本中的其他位置预先计算。
我应该如何插入这样的字符串而不是URL路径?
PS:我正在使用angularjs。
答案
只需将base64编码的图像字符串作为L.imageOverlay
的第一个参数传递,即代替你的imageUrl
:
var map = L.map('map');
var imageUrl = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAlgCWAAD/2wBDAAMCAgMCAgMDAgMDAwMDBAcFBAQEBAkGBwUHCgkLCwoJCgoMDREODAwQDAoKDhQPEBESExMTCw4UFhQSFhESExL/2wBDAQMDAwQEBAgFBQgSDAoMEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhL/wAARCAB/AKIDASIAAhEBAxEB/8QAHQAAAgIDAQEBAAAAAAAAAAAABQYEBwACAwgBCf/EAEQQAAICAQMDAwIFAQUEBQ0AAAECAwQFBhESABMhByIxFEEIFSMyUUIkM1JhcRYlQ4EXcpGj0zVEYmRzgoSSk6Gx0dL/xAAWAQEBAQAAAAAAAAAAAAAAAAAAAQL/xAAcEQEBAQEAAwEBAAAAAAAAAAAAAREhEjFRAkH/2gAMAwEAAhEDEQA/AP0e1JfvRajnir37UMEVGFxBCY1CkvJyclh/6KjyQvjYlSd+kPX2qNaac/K81py+LtCxZ+isYa8RAsqE7CzHZWFnilMhEaxyKyNuP2nd+njVcM9jPTwUZf7Vbr1oq8RbjxPKYtMHHuQovnceDuFIPIdR8Fmxi9QT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sVQi3BYSuocxtEVI4NIQCw3O7n3AdU1iMjcz/AKNal0l6W4d8pPpM5jBJkVljoY+VY5SIHVt2eY8DWG6RkkH+9G+/UD/+GWbG2/RXMNhhXtpk81II68cSkiBFGylNzsO3Gyfx4/z26M17mNwGVabIS4/Ez17cBVJLy81hCyRyfprISDt7yFU+Nj1WXo9oTUGrfQLREtjUGChwupppvqDicA4nh2lsyKomltbFSw2IMXnYAr48kMP6UaagqZCKdM3fsUsZPPJHPbatEzxy7LII6pi5KfIBbkAQw+QerYSHtPUXFaNxGn9R5awMriSkqY/tScy6L315FpigQcoYdi3wWA6cq/rnoPUVSu+PqXNQ3bFdZmp4nAT5J08DcM8cRjGxIG5YD4/npCp6P03gcvYfB6bxWNsvcjUX6+J70r80UuCWDOT2pZ23Lee348gdW7oHHVYrUEkiKZZ8XVuVmDMFCyQrHKFXfYeYlJ/6/UFR538QtCHM2sXpv0hy1/IU5hBZju1a8LRyMCVBjhE8uxA334BfI3I3HXO1qv1P以上是关于如何在Leaflet ImageOverlay中使用base64字符串而不是URL的主要内容,如果未能解决你的问题,请参考以下文章
如何在 iframe 中使用 Leaflet.js 中的地图
使用 Leaflet-react 时如何调用 fitBounds()?