2016-10-26 17 views
0

モーダルポップアップでGoogleマップを表示するには、以下のコードを使用しています。しかし、Google Mapマーカーはモーダルの中央に表示されません。それは常に左隅に隠されています。Googleマップのマーカーがモーダルポップアップの中央に表示されない

Map image

<script> 
    var geocoder; 
    var map; 
    function initialize() { 
    geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(0.0, 0.0); 
    var mapOptions = { 
     zoom: 14, 
     center: new google.maps.LatLng(0.0, 0.0), 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
    } 


    function codeAddress(i) {  

    var address = document.getElementById('address'+i).value; 
    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     map.setCenter(results[0].geometry.location); 
     var marker = new google.maps.Marker({ 
      map: map, 
      zoom:10, 
      position: results[0].geometry.location 
     }); 

     google.maps.event.addListenerOnce(map, 'idle', function() { 
     google.maps.event.trigger(map, 'resize'); 
     }); 

     } else { 
     alert('Geocode was not successful for the following reason: ' + status); 
     } 
    }); 

    } 

</script> 

またsee the code here

+0

サンプルページで簡単にできます:)まず、LatLng varを使用しません。それを削除する(またはそれを使用する:)私が理解するところでは、マップがアイドルであるときにマップの再描画が呼び出されます。アイドルイベントが起きたことは確かですか? – Nico

+0

変数から動的にLatLngを取得しています。その良い仕事。私はまた、マーカーを参照してください。それは左隅を示していました。私はどのように地図の中心に移動しますか?助けてください –

+0

マニュアルmap.setCenter(latLng)が問題を解決していますか?はいの場合、マップセンターがgmapでうまく使用されていないことを意味します。この場合、再描画後にマップセンターを設定するだけです。 – Nico

答えて

0

これもGoogle map modal issueに関連している可能性が。

@SSRで与えsolutionとしてモーダルイベントハンドラを使用してみてください:

ブートストラップモーダル地図問題解決方法:この関連SO post

$(document).ready(function(){ 
    $('#locationMap').on('shown.bs.modal', function(){ 
    google.maps.event.trigger(map, 'resize'); 
    map.setCenter(new google.maps.LatLng(-33.8688, 151.2195)); 
    }); 
}); 

ソリューションも役立つかもしれません。

関連する問題