2016-04-17 10 views
2

Google Maps APIを使用するアプリケーションをテストするために、Seleniumを使用してアクセプタンステストを作成しています。マップマーカーの配置をテストするために、私はusing un-optimized markersであり、マーカーエレメント(SeleniumからXPathクエリー)をターゲットにするための一意のタイトル属性です。Google Maps Unimimimized Marker/fitBounds

google.maps.Map.fitBoundsを(呼び出しここで私が問題に遭遇してきた

)はマーカーが最適化されていないマーカーが使用されているDOMから削除されます。これを行うには、fitBounds()を繰り返し呼び出す必要があります。 これは最適化されていないマーカーでのみ発生し、アプリケーション自体は期待通りに機能します。

私は、ブラウザで複製する短いテストスクリプトを書いている:以下を実行してください。

私もcreate a fiddleをしました。

私はAPIドキュメントには何も関係がないことを見てきました。

助けがあれば助かります。

<html> 
    <head> 
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js"></script> 
    <style type="text/css"> 
    #container { 
     width: 800px; 
     height:500px; 
    } 
    </style> 
    </head> 
    <body> 
    <button id="show">Show</button> 
    <button id="hide">Hide</button> 
    <div id="container"></div> 
    <script type="text/javascript"> 

     // Repeatedly click on 'Hide' then 'Show' (8+ times sometimes). At some point the markers disappear and not come back - even if the code for showing/hiding markers is entirely commented out. 
     // The issue seems to be with the map.fitBounds() call when un-optimized markers are being used. 
     // Note - un-optimized markers are needed for targeting marker HTML elements as part of an acceptance test. 
     // Replicated on: 45.0.2 Firefox; 49.0.2623.112 m Chrome. 

     function changeBounds(boolToggle) { 
     //return; // TODO - if you add this line in the problem is fixed. 
     var bounds = new google.maps.LatLngBounds(); 
     var latLng = boolToggle ? new google.maps.LatLng(70, 70) : new google.maps.LatLng(30, 30); 
     bounds.extend(marker1.getPosition()); 
     bounds.extend(marker2.getPosition()); 
     bounds.extend(latLng); 
     map.fitBounds(bounds); 
     } 

     var mapOptions = { 
     center : { lat : 50, lng : 50 }, 
     zoom: 5, 
     }; 

     var map = new google.maps.Map(document.getElementById("container"), mapOptions); 

     var marker1 = new google.maps.Marker({ 
     position : { 
      lat : 51, 
      lng : 51, 
     }, 
     title : "MARK1", 
     optimized : false 
     }); 

     var marker2 = new google.maps.Marker({ 
     position : { 
      lat : 52, 
      lng : 52, 
     }, 
     title : "MARK2", 
     optimized : false 
     }); 

     marker1.setMap(map); 
     marker2.setMap(map); 

     document.getElementById('show').onclick = function() { 
     //marker1.setVisible(true); // TODO - The original code was showing/hiding the markers but the issue is replicated without even doing this. 
     //marker2.setVisible(true); 

     changeBounds(true); 
     } 

     document.getElementById('hide').onclick = function() { 
     //marker1.setVisible(false); // TODO - The original code was showing/hiding the markers but the issue is replicated without even doing this. 
     //marker2.setVisible(false); 

     changeBounds(false); 
     } 
    </script> 
    </body> 
    </html> 

編集:私はGoogleとのバグとしてこれを報告しました。 https://code.google.com/p/gmaps-api-issues/issues/detail?id=9912

+0

私は縮小さGmaps APIで捕まってしまったが、私はこれを言うことができます。マーカーは、物理的に(自分のDOM要素を)消え、私はマーカのマップをリセットしても戻ってこない(最適化をtrueに変更してから、それを表示するマーカー上で.setMap(map)を実行するが、falseに設定して再びsetMapを実行すると消えてしまう!)、マーカーを再び表示させる唯一の方法は、マップを少しドラッグすることです。 –

+0

フィドルをマップにレンダリングし、Chromeでその問題を確認しました – geocodezip

答えて

4

Google Maps APIで問題があるはずですが、私は解決策を見つけました:pan *メソッドを使用してください。私にとって最高のものは、panByを使用していました。panByは、xとyピクセルでパンするために地図に表示します。 0ピクセルを動かすとうまくいきました。そのため、fitBoundsの後にmap.panBy(0,0)を追加するだけで消滅マーカーが修正されました。

しかし、それはマップの素敵な漸進的な移動を失うことを意味し、その代わりに、私はアイドルイベントでpanByを使用しました。最終溶液は、マップの作成後、以下を追加しました:

google.maps.event.addListener(map, 'idle', function() { 
    map.panBy(0,0); 
}); 

私は申し訳ありませんが、私は実際には問題が何であるかを見るためには、Google APIを取捨選択するのに十分な時間がありません。

何を動作しませんでした:

  • marker.setMap(またはその他のプロパティ)
  • マーカー
  • ドラッグ可能かどうかgoogle.maps.event.trigger(マップ、 'some_event')作ります;

関連:菱鉄鉱へ

+1

ありがとうございました。回避策が助けになりました。私もGoogleにバグレポートを提出しました:https://code.google.com/p/gmaps-api-issues/issues/detail?id=9912 – Pete171

+0

Googleは上記のバグスレッドを https:// codeとマージしました.google.com/p/gmaps-api-issues/issues/detail?id = 5716 彼らはバグを受け入れたように見え、それを再現することができます。うまくいけば、今後のリリースで修正が見られます。 – CHawk

0

同様に、私が直接バグを修正する方法は考えています。しかし、私が見つけたこの問題を回避するには、マップの「サイズを変更」イベントをトリガすることです:

google.maps.event.trigger(google_map, "resize"); 
+0

本当ですか?私が覚えているのは、それが私が試した最初のものの1つであり、失敗したことです。それはうまくいかないものの中にさえある。 –

+0

ええ - それは正常に私のためにマップ上にマーカーを置きます。しかし、状況によっては、resizeを呼び出すことで地図上の境界が乱れてしまい、誰にとっても適切な解決策ではないかもしれません。私は他のGoogleマップイベントをトリガーしてテストしませんでした。 – CHawk