0

シンプルな Google マップ v3 コードがあります。マップを作成し、markerCluster にマーカーを追加します。infoWindow にコンテンツを設定して開くことを除いて、すべて正常に動作しています。この関数getSupplierDetails()は、短い文字列 (つまり、"Red Supply") を返すだけです。

問題は次のとおりです。「Red Supply」というテキストを setContent 行にハードコーディングするとinfoWindow.setContent("Red Supply");、情報ウィンドウがコンテンツで正常に開きます。

getSupplierDetails()しかし、以下のコードのようにそのままにしておくと、関数は「Red Supply」を返しますが、情報ウィンドウはまったく開きません。

getSupplierDetails()関数は次の JSON 文字列を返します: {"popupContent": "Red Suppplier"}Firebug から。

解決策なしでそれに長い間費やしました。どんな助けでも感謝します。

ありがとう

var map;
var markerCluster;
var infoWindow;

$(document).ready(function() {

  if (mapElem != null) {
        var latlng = new google.maps.LatLng(54.664936, -2.706299);
        var myOptions = {
            zoom: 5,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
        };

        map = new google.maps.Map(mapElem, myOptions);
        markerCluster = new MarkerClusterer(map);
        infoWindow = new google.maps.InfoWindow();

    AddMarkers();
  }
});

function AddMarkers(){
    markerCluster.clearMarkers();
    var marker = new google.maps.marker({position:latLng, map:map, title:"title"});

    google.maps.event.addListener(marker, 'click', function() {
      var res = getSupplierDetails();
      infoWindow.setContent(res);
      infoWindow.open(map, this);
    });

    markers.push(marker);
    markerCluster.addMarkers(markers);
}


function getSupplierDetails() { //returns {"popupContent": "Red Suppplier"}
    $.ajax({
        type: 'POST',
        url: "sitedetail.aspx",
        dataType: 'json',
        timeout: 30000,
        success: function(data) {
            return data.popupContent;
        },
        error: function(xhr, textStatus, thrownError) {
            var resp = JSON.parse(xhr.responseText);
            alert(resp.message);

        }
    });
}
4

2 に答える 2

0
function getSupplierDetails() { // returns "Red Suppplier" or ""
    var content = '';
    $.ajax({
        type: 'POST',
        url: "sitedetail.aspx",
        dataType: 'json',
        timeout: 30000,
        success: function(data) {
            content = data.popupContent;
        },
        error: function(xhr, textStatus, thrownError) {
            var resp = JSON.parse(xhr.responseText);
            alert(resp.message);

        }
    });
    return content;
}
于 2011-06-24T15:19:46.527 に答える
0

ajaxの成功呼び出しは非同期であるため、nullを取得しています。何をしたかは問題ありません(SetContentを成功部分に移動)が、これを行うこともできます

google.maps.event.addListener(marker, 'click', function() {
  getSupplierDetails(function(res){
     infoWindow.setContent(res)
  });
  infoWindow.setContent(res);
  infoWindow.open(map, this);
});

function getSupplierDetails(callback) {
    $.ajax({
        type: 'POST',
        url: "sitedetail.aspx",
        dataType: 'json',
        timeout: 30000,
        success: function(data) {
            callback(data.popupContent);
        },
        error: function(xhr, textStatus, thrownError) {
            var resp = JSON.parse(xhr.responseText);
            alert(resp.message);

        }
    });
}
于 2011-11-16T04:01:57.133 に答える