2016-12-08 25 views
0

私はHTMLページを持っており、割り当ての一環として、HTMLページをリッピングしてJavascriptで再構築する必要があります。私はjavascriptで私のサイトの支出を作成し、今いくつかの機能を追加したい。Javascript経由でGoogle APIを挿入

次のページでは、GoogleマップAPIを追加して 'MainWindow'に表示したいと考えています。この関数はサイドメニューで呼び出され、テストされて動作しています。

function MainBody() { 
var x = document.getElementById("mainWindow"); 

x.style.width = "1200px"; 
x.style.height = "750px"; 
x.style.position = "absolute"; 
// started using 'zIndex' to 1. orginising on the site and 2. visually show where everything is meant to be in the code 
x.style.zIndex = "0"; 
x.style.margin = "0px 0px 0px 300px"; 
x.style.background = "#c01e0c"; 
x.style.border = "thick solid #901709"; 
x.style.borderWidth = "10px"; 
} 





function setUpListener2() { 
document.getElementById("listItem3").addEventListener("click", Option3, false); 
} 




function Option3() { 
    var subX1 = document.getElementById("mainWindow"); 

    var map = document.createElement('DIV'); 
    function initMap() { 
     map = new google.maps.Map(document.getElementById('map'), { 
     center: {lat: -34.397, lng: 150.644}, 
     zoom: 8 
     }); 
    } 

    subX1.appendChild(map); 
    map.style.zIndex = "1"; 


} 
+1

質問は何ですか? – Archer

+0

「mainWindow」に表示されない関数にgoogle APIを追加したい –

+0

htmlを設定するのと同じ方法でスクリプトを追加しますか? –

答えて

0
var my_google_script = document.createElement('script'); 

my_google_script.setAttribute('src','https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY'); 

document.head.appendChild(my_google_script); 
+0

私はあなたの答えを取って編集しました。私はあなたが見るように頭にこれを追加しません: var parent = document.getElementById masterDiv "); var subX1 = document.getElementById( "mainWindow"); parent.removeChild(subX1); var my_google_script = document.createElement( 'script'); my_google_script.style.position = "absolute"; my_google_script.setAttribute( 'src'、 'https://maps.googleapis.com/maps/api/js?key=AIzaSyCzjbgHg4JMAhEcgxQePAzo2ez2vGIk4ic'); parent.appendChild(my_google_script); –

+0

APIはまだ画面に表示されません.... –

関連する問題