我的网站中嵌入了谷歌地图,其中显示人们创建的旅行传单标记。在谷歌地图 infoWindowContent 中,我显示旅行传单标题和旅行传单横幅照片。 像这样:
但是,当用户决定不上传旅行传单横幅照片时,谷歌地图上的标记就会消失,因为横幅照片是空的。 这是 infoWindowContent 的简短代码:
var infoWindowContent = [
@foreach($flyer as $flyers)
[
'<div class="info_content">' +
'<a href="{{ $flyers->title }}">' +
'<img class="ui top aligned small image" src="/travel/{{ $flyers->thumbnail_path }}" alt="Test">' +
'<h5>{{ $flyers->title }}</h5>' +
'</a>' +
'</div>'
],
@endforeach
];
如何做到即使没有旅行传单横幅照片可显示,标记仍显示在 map 上?
************ 编辑******************** 我试过这个:
@foreach($flyer as $flyers)
@if(empty($flyers->thumbnail_path))
var image = '<img class="ui top aligned small image" src="/travel/src/public/css/01.jpg" alt="Test">';
@else
var image = '<img class="ui top aligned small image" src="/travel/{{ $flyers->thumbnail_path }}" alt="Test">';
@endif
@endforeach
var infoWindowContent = [
@foreach($flyer as $flyers)
[
'<div class="info_content">' +
'<a href="{{ $flyers->title }}">' +
image +
'<h5>{{ $flyers->title }}</h5>' +
'</a>' +
'</div>'
],
@endforeach
];
但它仍然只返回存在的照片,如果没有横幅图像,则不会显示标记。我也尝试过:但没有运气。
@if(file_exists($flyers->thumbnail_path))
@if(file_exists(public_path($flyers->thumbnail_path)))
@if($flyers->thumbnail_path === '')
@if(isset($flyers->thumbnail_path))
最佳答案
我不知道 Laravel,但你无法测试图像是否已设置,然后准备一个正确的字符串......像这样的东西(伪代码)
var infoWindowContent = [
@foreach($flyer as $flyers)
// test if image is set and prepare a proper string
if(isset($flyers->thumbnail_path )){
echo $my_image = '<img class="ui top aligned small image" src="/travel/{{ $flyers->thumbnail_path }}" alt="Test">';
} else {
$my_image = '';
}
[
'<div class="info_content">' +
'<a href="{{ $flyers->title }}">' +
{{$my_image}} + // add the per image code (empty html string for no image)
'<h5>{{ $flyers->title }}</h5>' +
'</a>' +
'</div>'
],
@endforeach
];
关于javascript - 如果 infoWindowContent 中的字段为空,则 Google map 标记不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35349025/