誰かがこのコードで私を助けてくれますか?
私の Rails プロジェクト (どのプロジェクトであるかは問題ではないと思います) には、'fixed_header' と呼ばれる位置が固定されたヘッダーがあります。このページにはリンクがあり、クリックすると下に新しいページが読み込まれます。
クリックすると読み込まれるページの 1 つに、「map_canvas」という div があります。「fixed_header」ページのコードで「map_canvas」という div の存在をチェックし、そこにある場合はマップをロードします。
以下のコードは、アプリケーション全体のスニペットから取得したものであり、機能しますが、この特定のものに対して機能させることはできません。どんな助けでも大歓迎です。
<script>
//Is 'ready' the code to use? This code is in 'fixed_header'
//My idea is that, once
//a link in 'fixed_header' is clicked, a new page loads
//underneath the header. The div 'map_canvas' is
//searched for and the function activates if it is found.
$(document).on("ready", function () {
if ($("#map_canvas").length > 0) {
initialize_google_maps();
}
});
function initialize_google_maps() {
var currentlatlng = new google.maps.LatLng(user_latitude, user_longitude);
var zoom = 10;
var myOptions = {
zoom: zoom,
center: currentlatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP, // ROADMAP, SATELLITE, HYBRID
streetViewControl: false
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var marker = new google.maps.Marker({
map: map,
position: currentlatlng,
icon: {
oppacity: 0
}
});
var circle = new google.maps.Circle({
map: map,
fillOpacity: 0,
strokeWeight: 2,
strokeOpacity: 0.7,
radius: 10000,
});
circle.bindTo('center', marker, 'position');
}
</script>