- 序章
こんにちは、私のコースでは、フェスティバルのすべてのイベントを 1 つのページに配置する必要がある個別のプロジェクトがあります。これは JavaScript コースであるため、ほとんどのタスクは JavaScript で処理する必要があります。
問題は、良いウェブサイトを提供するために、AJAX からのデータが取得されて処理されるまでユーザーが待たなければならないというメッセージを含む読み込み中の gif ファイルを使用していることです。
これは私の HTML スニペットです
<!-- showing loading screen -->
<div id="startup">
<h3>Please wait until the data is done with loading</h3>
<img src="images/ajax_load.gif" alt="loading icon" id="load_icon" />
</div>
<!-- actual content (will be displayed after loading phase) -->
<div id="siteContent">
<div id="top">
<label><input type="checkbox" name="cbDisabilities" id="cbDisabilities">Accessible for disabilities</label>
<label><input type="checkbox" name="cbFree" id="cbFree">for free</label>
<select id="selectCat">
<option selected="selected"> </option>
</select>
</div>
<div id="mapBox"></div>
<div id="dateBox" class="layout"></div>
<div id="eventBox" class="layout borders"></div>
</div>
<footer>
<p>Gentse Feesten Infos – © name here TODO</p>
</footer>
上記で、両方のdivに次のCSSもあり、
div#siteContent {
display: none;
}
/* style google map box */
div#mapBox {
display: block;
height : 500px;
width : 500px;
margin-top : 5px;
}
ご覧のとおり、実際のコンテンツは非表示になっているため、h3 テキストを含むロード イメージのみが表示されます。ここで、AJAX 呼び出しが完了したら、イベントの場所のマーカーをマップに追加する必要があります。その間、取得した JSON データも処理します。これが完了したら、loading.gif アニメーションで div を削除し、実際のコンテンツを表示したいと考えています。
これは、データがどのように処理されているかの画像でもあります (マップの初期化 = GPS 位置の読み取り + 現在位置マーカーの配置 + マップのロード)。
データを処理しているときにマップに複数のマーカーを追加する必要があるため、AJAX 呼び出しが完了する前にマップを初期化する必要があります。地図がないと、Google マップ マーカーを追加するときにエラーが発生します。
これは、ロード時の JavaScript スニペットです。AJAX を呼び出すloadData()とGoogle マップを初期化するplaceMap(currentLocation)の 2 つのメソッドがあります。
window.addEventListener('load', function() {
// get json data - going to call AJAX
loadData();
// getting current location by geocoder
var getGeoLocation = new google.maps.Geocoder();
var currentPosition;
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
currentPosition = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
placeMap(currentPosition);
}, function() {
handleError(100);
});
}
else {
handleError(200);
}
// other tasks omitted
});
これは、マップがどのように初期化およびレンダリングされるかです (currentMap は、Google マップ オブジェクトへの参照を保持するグローバル変数です)。
var placeMap = function(location) {
var mapOptions = {
zoom : 18,
center : location,
disableDefaultUI : true, // remove UI
scaleControl : true,
zoomControl : true,
panControl : true,
mapTypeId : google.maps.MapTypeId.ROADMAP
};
currentMap = new google.maps.Map($("mapBox"), mapOptions);
// current position
var mapMarker = new google.maps.Marker({
position : location,
map : currentMap,
zIndex : 255
});
google.maps.event.addListenerOnce(currentMap, 'idle', setActive);
}
- 私の問題
しかし、私の問題は、ロード時に display:none を使用すると、マップ ウィンドウがうまくレンダリングされないことです。インライン (デフォルトのブラウザー表示スタイル) に切り替えると、ボックスに灰色の領域があり、マップが部分的にレンダリングされます。
- すでに試した解決策
このサイトにある解決策は既に試しました。
- ここ- 結果: この質問者と同じ表示結果、その灰色の領域が得られます。しかし、それは私の問題を解決しませんでした。
- ここ- 結果: レンダリングなし (ボックスが折りたたまれています)。
- ここ- 結果 : (2) と同じ。
- これまでのところ最善の解決策ですが、
これまでのところ、次のコマンドで最高の結果が得られました
// display content
$("startup").style.display = "none";
$("siteContent").style.display = "inline";
google.maps.event.trigger(currentMap, 'resize');
(startup = 読み込み中の gif 画像を含む div と siteContent = 実際のコンテンツ) .
これにより、表示された後、マップが正しくレンダリングされます。しかし、ここでは、残念ながら地図は GPS 位置を中心としていません。GPS マーカーは左上隅にあります。
div#siteContent でdisplay="none" を使用せずにサイトを表示している場合、すべてが意図したとおりに機能します (Google マップは GPS 位置がマップの中心に正しく表示されます)。
誰かがこの小さなレンダリングの問題を解決する方法を知っていますか? できればjQueryを使用しないでください。