2016-02-11 49 views
1

我有一個嵌入到我的網站的Google地圖,顯示人們創建的旅行傳單標記。在Google地圖infoWindowContent內部,我顯示旅行傳單標題和旅行傳單橫幅照片。 像這樣: Google Maps如果infoWindowContent中的某個字段爲空,Google地圖標記不會顯示

但是,當用戶決定不上傳旅遊傳單的橫幅照片,谷歌地圖在標記消失,因爲橫幅照片是空的。 下面是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 
]; 

如何讓這個即使沒有旅遊傳單的橫幅照片顯示,標記仍然顯示在地圖上?

*********** EDIT ******************* 我嘗試這樣做:

@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)) 
+0

想到的一個快速解決方案是某種錯誤處理。你應該首先檢查你想要注入到infoWindow中的數據是否存在,如果存在,那麼你顯示它就像你已經在做的那樣,如果沒有,那麼你應該爲infoWindow創建一個不同的內容。這只是一個可能的方法的想法,但記住數據未正確檢索並使您的應用程序正常工作的用例是一種很好的做法。 – G4bri3l

+0

嗯....讓我看看 – David

+0

檢查我的編輯頂部 – David

回答

1

我不知道laravel但你不能測試,如果圖像設置,然後準備一個正確的琴絃...... somthings這樣的(僞代碼)

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 

]。

+0

我會嘗試。 – David

+0

檢查我的編輯頂部。 – David

+0

我建議你,如果圖像不存在,爲這部分字符串設置no html代碼...例如:如果不存在var image =''; – scaisEdge

相關問題