javascript - 如果 infoWindowContent 中的字段为空,则 Google map 标记不会显示

标签 javascript php google-maps laravel google-maps-api-3

我的网站中嵌入了谷歌地图,其中显示人们创建的旅行传单标记。在谷歌地图 infoWindowContent 中,我显示旅行传单标题和旅行传单横幅照片。 像这样: Google Maps

但是,当用户决定不上传旅行传单横幅照片时,谷歌地图上的标记就会消失,因为横幅照片是空的。 这是 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/

相关文章:

php - 触发按钮单击 Enter

php - 在 WP Loop 之外获取帖子作者 ID

android - 如何像uber android应用程序一样在 map 上获取居中位置图钉的位置

android - 如何在 android studio 中为 android v2 map 创建发布 key

javascript - 谷歌地图 API google.maps.event 未定义

javascript - 工厂中的 Angularjs $http.get 不返回有效的 JSON,而是返回不同的对象

javascript - 如何切换 z-index 值以使图像出现在前面或背景中?

javascript - 如何在不将其声明为全局的情况下使用多个类来修改单个数据源?

php - PHP "remote include"是如何工作的?

javascript - 如何在不同的屏幕尺寸上获得相同的scrollY高度数?