2017-11-03 9 views
0

I'm pretty new to Javascript and I am not able to add a marker to these google maps API's. I was able to add one map with a marker using W3schools tutorial, but when I try to add multiple maps with one marker on each one, this is where I am running into problems. I have searched for an answer on here already, and I was not able to find a working solution. In the picture I have posted, I have not yet added the "var marker" constructor because I am not sure where to put it? Can someone kindly point me in the right direction, and tell me where I am going wrong?各緯度/経度のポイントにマーカー付き2つのGoogleマップを追加するにはどうすればよいですか?問題の

Javascriptを:

function myMap() { 
    var mapOptions1 = { 
    center: new google.maps.LatLng(42.9814362, -81.2267205), 
    zoom:15, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 

}; 
    var mapOptions2 = { 
    center: new google.maps.LatLng(42.9421287, -81.2284421), 
    zoom:15, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var map1 = new google.maps.Map(document.getElementById("googleMap1"),mapOptions1); 
    var map2 = new google.maps.Map(document.getElementById("googleMap2"),mapOptions2); 
} 

は私が追加する必要がと思いますが、わからない、あるいはどのようなもの:

var marker = new google.maps.Marker({position: myCenter}); 

marker.setMap(map); 
+0

あなたのページへのファイルアクセスを使用している可能性があります。時にはGoogle APIによってそのような使用が制限されることがあります。あなたはXAMPPにそれをさせることができます。またはノード: – kangaro0

+0

私はw3schoolsのツタンカーメンを使用して地図上で働くマーカーを得ることができましたが、別のマップを追加しようとしたときにうまくいきませんでした。私はそれを2つではなく画面上の1つのマップで動作させることができます。どうすれば、そのjavascriptを再配置して、そのvarマーカー機能を追加できますか? w3schoolsはマーカを追加する方法についてはすばらしいことがありますが、例では1つのマップしかなく、別のマップを追加するとjavascriptで変更されます。 –

+0

あなたの写真に表示されている問題を編集しました:https://www.pic-upload.de/view-34226230/Inked4vEGn_LI.jpg.html – kangaro0

答えて

1

あなたはそれぞれの有効なセンターを割り当てる必要があります各地図のマーカー

function myMap() { 
    var mapOptions1 = { 
     center: new google.maps.LatLng(42.9814362, -81.2267205), 
     zoom:15, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 

    }; 
    var mapOptions2 = { 
     center: new google.maps.LatLng(42.9421287, -81.2284421), 
     zoom:15, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var map1 = new google.maps.Map(document.getElementById("googleMap1"),mapOptions1); 
    var map2 = new google.maps.Map(document.getElementById("googleMap2"),mapOptions2); 

    var marker1 = new google.maps.Marker({ 
       position: new google.maps.LatLng(42.9814362, -81.2267205) 
    }); 

    marker1.setMap(map1); 

    var marker2 = new google.maps.Marker({ 
       position: new google.maps.LatLng(42.9421287, -81.2284421) 
    }); 

    marker2.setMap(map2); 

    } 
+0

ああありがとう!私の主な問題は、スクリプトをどこに追加するのかわからなかったことですが、今は意味をなさないようになっています。すべての入力をありがとう。 –

+0

答えが完全なコードで更新されました..もし私の答えが正しいか、適切な解決策に導かれればそれは受け入れられたとマークしてください...どうぞこちらをご覧ください http://meta.stackexchange.com/questions/5234/how-回答を受け取ってくれます – scaisEdge

+0

あなたの答えを読む前に私は結論に至りましたが、あなたのことが正しいので間違いなく私を助けてくれたでしょう。再度、感謝します。 –

0

fut私はそれを理解しました。

function myMap() { 
    var mapOptions1 = { 
    center: new google.maps.LatLng(42.9814362, -81.2267205), 
    zoom:15, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 

}; 
    var mapOptions2 = { 
    center: new google.maps.LatLng(42.9421287, -81.2284421), 
    zoom:15, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var map1 = new google.maps.Map(document.getElementById("googleMap1"),mapOptions1); 
    var latlong = {lat: , lng: }; // Insert your map marker lat/long here 
    var marker = new google.maps.Marker({ 
    position: latlong, 
    map: map1, 
    title: 'Hello World' 
    }); 
    var map2 = new google.maps.Map(document.getElementById("googleMap2"),mapOptions2); 
    var latlong = {lat: , lng: }; // Insert your map marker lat/long here 
    var marker = new google.maps.Marker({ 
    position: latlong, 
    map: map2, 
    title: 'Hello World' 
    }); 
} 
関連する問題