2012-02-19 8 views
1

Google Maps API v.3には少し問題があります。 MySqlデータベースからロケーションアドレスを取得し、GMapsに動的に送信しようとします。 これは正常に動作しますが、私は "infowindow"を開くことができません。 コードにちょっとぶち当たったが、私が解決策に持っているのは、情報ウィンドウが開いていたが、マップが定義された位置に集中していて、実際のものではなかった。私はさまざまなサイトを検索しましたが、適切なアドバイスを見つけることができませんでした。たぶん私はv.2とv.3をミキシングしていますが、私が見る限り、それはそうではないようです。これはGoogleのAPIの最初の一見ですので、忍耐してください。 コードのすべては、例からです:事前にGoogle Maps API v.3 - インフォウィンドウを表示できません。または地図の中心がデザインの場所にある場合

var marker; 
    var geocoder; 
    var map; 
    function initialize() { 
    var mapDiv = document.getElementById('map-canvas'); 
    var latlng = new google.maps.LatLng(42.696492,23.326011); 
     geocoder = new google.maps.Geocoder(); 
     var address = "Milano 20099"; 
     geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      map.setCenter(results[0].geometry.location); 
      marker = new google.maps.Marker({ 
       map: map, 
       position: results[0].geometry.location 
      }); 
     } else { 
      // alert("Geocode was not successful for the following reason: " + status); 
     } 
     }); 
     var infowindow = new google.maps.InfoWindow({content: '<strong>I will be here!</strong><br/>Milano 20099'}); 
     var myOptions = { 
     zoom: 9, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.HYBRID 
     } 
     map = new google.maps.Map(mapDiv, myOptions); 
     infowindow.open(map, marker); 
    } 

ありがとう!

答えて

0

情報ウィンドウのオープンメソッドをジオコーダーの成功コールバック関数に移動する必要があります。ジオコード要求が応答するまでに時間がかかることがあり、ジオコーダがマーカー位置を返す前に情報ウィンドウを開こうとしています。したがって、コードは次のようになります:

var marker; 
      var geocoder; 
      var map; 
      function initialize() { 
       var latlng = new google.maps.LatLng(42.696492, 23.326011); 
       var myOptions = { 
        zoom : 9, 
        center : latlng, 
        mapTypeId : google.maps.MapTypeId.HYBRID 
       } 
       var mapDiv = document.getElementById('map-canvas'); 

       map = new google.maps.Map(mapDiv, myOptions); 
       geocoder = new google.maps.Geocoder(); 
       var address = "Milano 20099"; 
       geocoder.geocode({ 
        'address' : address 
       }, function(results, status) { 
        if(status == google.maps.GeocoderStatus.OK) { 
         map.setCenter(results[0].geometry.location); 
         marker = new google.maps.Marker({ 
          map : map, 
          position : results[0].geometry.location 
         }); 
         var infowindow = new google.maps.InfoWindow({ 
        content : '<strong>I will be here!</strong><br/>Milano 20099' 
       }); 


       infowindow.open(map, marker); 
        } else { 
         // alert("Geocode was not successful for the following reason: " + status); 
        } 
       }); 

      } 
+0

ありがとうございました! – nnikolov06

関連する問題