0

Google Maps APIをjQueryを使用してサイトに統合するためのSitePointチュートリアルに従っていますが、1つの例外を除いて、すべてが非常にうまく機能しています。新しいマーカーごとに、前のマーカーを閉じることなく、個別の情報ウィンドウが開きます。一度に1つのウィンドウだけを開く方法を見つけようとしています。

問題のチュートリアルは次のとおりです:http ://www.sitepoint.com/google-maps-api-jquery/

ここでこの質問を確認しました。GoogleMapsAPIv3で開いているInfoWindowは1つだけですが、そこでの回答に従って問題を解決できませんでした(簡単に誤解された可能性があります)。

私のコードは次のようになります:

$(document).ready(function(){  
  var MYMAP = {
  map: null,
  bounds: null
}

MYMAP.init = function(selector, latLng, zoom) {
  var myOptions = {
    zoom:zoom,
    center: latLng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }

  this.map = new google.maps.Map($(selector)[0], myOptions);
  this.bounds = new google.maps.LatLngBounds();       
}

MYMAP.placeMarkers = function(filename) {
  $.get(filename, function(json){
    $.each(json, function(i,loc){
      var marker = new google.maps.Marker({
        position: new google.maps.LatLng(loc.location.merchant_location.latitude, loc.location.merchant_location.longitude),
        map: MYMAP.map,
        title: loc.deal.subject
      });

      var arrMarkers = [];
      arrMarkers[i] = marker;
      var infoWindow = new google.maps.InfoWindow({
        content: "<h3>" + loc.deal.subject + "</h3><p>" + loc.location.merchant_location.address + "</p>"
      });

      var arrInfoWindows = [];
      arrInfoWindows[i] = infoWindow;
      google.maps.event.addListener(marker, 'click', function(){
        infoWindow.open(MYMAP.map,marker);
      });
    });         
  }, "json");
}

$("#map").css({
  height: 500,
  width: 600
});

var myLatLng = new google.maps.LatLng(#{@deals.first.merchant_locations.first.latitude},#{@deals.first.merchant_locations.first.longitude});
MYMAP.init('#map', myLatLng, 11);
MYMAP.placeMarkers('/more_deals/get_locations/#{@market.id}');

});

どんな助けでも大歓迎です。ありがとう

4

2 に答える 2

3

.each()ループ内に情報ウィンドウを作成しています。代わりに、そのループの外に1つの情報ウィンドウを作成します。次に、イベントリスナーで、そのグローバル情報ウィンドウのコンテンツを毎回更新するだけです。

MYMAP.init = function(selector, latLng, zoom) {
  var myOptions = {
    zoom:zoom,
    center: latLng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }

  this.map = new google.maps.Map($(selector)[0], myOptions);
  this.bounds = new google.maps.LatLngBounds();      
}

MYMAP.placeMarkers = function(filename) {
  $.get(filename, function(json){
    var infoWindow = new google.maps.InfoWindow({
            content: ""
      }); 

    $.each(json, function(i,loc){
      var marker = new google.maps.Marker({
        position: new google.maps.LatLng(loc.location.merchant_location.latitude, loc.location.merchant_location.longitude),
        map: MYMAP.map,
        title: loc.deal.subject
      });

      bindInfoWindow(marker, MYMAP.map, infoWindow, "<h3>" + loc.deal.subject + "</h3><p>" + loc.location.merchant_location.address + "</p>");
    });         
  }, "json");
}

function bindInfoWindow(marker, map, infowindow, html) {
    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(html);
        infowindow.open(map, marker);
    });
} 

$("#map").css({
  height: 500,
  width: 600
});

var myLatLng = new google.maps.LatLng(#{@deals.first.merchant_locations.first.latitude},#{@deals.first.merchant_locations.first.longitude});
MYMAP.init('#map', myLatLng, 11);
MYMAP.placeMarkers('/more_deals/get_locations/#{@market.id}');
于 2011-10-17T16:06:10.017 に答える
0

インフォウィンドウオブジェクトを1つだけ作成します。変更したコード。

MYMAP.init = function(selector, latLng, zoom) {
  var myOptions = {
    zoom:zoom,
    center: latLng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }

  this.map = new google.maps.Map($(selector)[0], myOptions);
  this.bounds = new google.maps.LatLngBounds();       
}

MYMAP.placeMarkers = function(filename) {

 var infoWindow = new google.maps.InfoWindow();

  $.get(filename, function(json){
    $.each(json, function(i,loc){
      var marker = new google.maps.Marker({
        position: new google.maps.LatLng(loc.location.merchant_location.latitude, loc.location.merchant_location.longitude),
        map: MYMAP.map,
        title: loc.deal.subject
      });

      var arrMarkers = [];
      arrMarkers[i] = marker;


      google.maps.event.addListener(marker, 'click', function(){
        infoWindow.setContent (
        "<h3>" + loc.deal.subject + "</h3><p>" + loc.location.merchant_location.address + "</p>"
      );
        infoWindow.open(MYMAP.map,marker);
      });
    });         
  }, "json");
}

$("#map").css({
  height: 500,
  width: 600
});
于 2011-10-17T16:11:51.120 に答える