私はこの男を理解しようと思っているほど多くのStackOverflow/googleグループを使いました。Google Map API BackBoneJSヌルの 'offsetWidth'プロパティを読み取ることができません
私はBackboneJSを使用して、開始位置と終了位置を持つマップをレンダリングしています。私はjQueryの$(window).load(.....)関数を使用しているため、新しいページ/ページのリフレッシュでは、このエラーは発生せず、しかし、DOMがDIVをまだロードしていない(document.getElementByIdで失敗している)ため、ビューを動的にレンダリングすると、このエラーが発生すると思います。私は$(window).load()以外のさまざまなメソッドを試してみましたが、どちらのユースケース(新鮮なページの読み込み - BackboneJSビューの読み込み)でも動作するものは得られません。テンプレートの直後に関数を呼び出そうとしていても動作しません。
ご協力いただければ幸いです。
ロバート
ビュー:
App.Views.MapShow = Backbone.View.extend({
initialize: function() {
_.bindAll(this, 'render');
var self = this;
$(window).load(function() {
self.renderMap();
});
},
render: function() {
this.renderTemplate();
},
renderTemplate: function() {
this.$el.html(JST['path/to/show/file']());
},
renderMap: function() {
var from = this.model.get('location_from');
var to = this.model.get('location_to');
var geocoder = new google.maps.Geocoder();
var map = new google.maps.Map(document.getElementById('mapCanvas'), {
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
directionsDisplay.setMap(map);
var request = {
origin: from,
destination: to,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
});
HTML:
<div class="map" id="mapCanvas"></div>
'setTimeout(function(){...}、0)'の中にマップの内容を入れてみましたか? http://stackoverflow.com/a/9145790/479863 –
これを回答として入れても大丈夫な人はいらないでしょう。 – rpearce
アンダースコアの_にリファクタリングしました。友人の推薦に基づいて機能を延期する。 – rpearce