1

ポリラインに複数の情報ウィンドウを追加しようとすると、情報ウィンドウが表示されません。

すでに同じ質問でこのスレッドを試しました: Google マップ V3 を使用した複数のポリラインと情報ウィンドウ

ご覧のとおり、指定された回答で提案された関数を使用しようとしましたが、それでも機能しません。

これが私のコードです:

var center = new google.maps.LatLng(51.97559, 4.12565);

    var map = new google.maps.Map(document.getElementById('map'), {
        center: center,
        zoom: 6,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });


var bounds = new google.maps.LatLngBounds();

// start coordinates
var start = ['51.97559, 4.12565', 
             '55.46242, 8.43872', 
             '49.49259, 0.1065',
             '50.36862, -4.13412']

// end coordinates
var end = ['51.94784, 1.2539', 
           '51.94784, 1.2539', 
           '50.79726, -1.11048',
           '43.45846, -3.80685']

function initialize() {


    // Make clickable tooltip
   /*

    */ 

    for (var i=0; i < end.length; i++){
      calcRoute(start[i], end [i]);
    }


}

function calcRoute(source,destination){

      var poly = new google.maps.Polyline({
        path: [],
        strokeColor: '#FF0000',
        strokeWeight: 5,
        strokeOpacity: 0.5
    });


   var directionsService = new google.maps.DirectionsService(); 
    var request = { 
        origin:source, 
        destination: destination, 
        travelMode: google.maps.DirectionsTravelMode.WALKING 
    };

    directionsService.route(request, function(result, status) { 
        if (status == google.maps.DirectionsStatus.OK) {
            path = result.routes[0].overview_path;

            $(path).each(function(index, item) {
                poly.getPath().push(item);
                bounds.extend(item);
            })

                // Custom infoWindow
    var toolTip = '<div id="map-box">'+
        '<div id="siteNotice">'+
        '</div>'+
        '<h1 id="firstHeading" class="firstHeading">Route</h1>'+
        '<div id="bodyContent">'+
        '<p>Lorem ipsum dolor sit amet</p>'+
        '</div>'+
        '</div>';


       poly.setMap(map);
       createInfoWindow(poly,toolTip); 


            map.fitBounds(bounds);
        }
    });  


}


function createInfoWindow(poly,content) {

    google.maps.event.addListener(poly, 'click', function(event) {
        infowindow.content = content;
        //infowindow.position = event.latLng;
        infowindow.setPosition(event.latLng);
        infowindow.open(map);
    });
}

google.maps.event.addDomListener(window, 'load', initialize);

ここにjsfiddleリンクがあります: http://jsfiddle.net/cnwMG/7/

ポリライン上に情報ウィンドウを表示するための助けをいただければ幸いです。

4

1 に答える 1

3

あなたは最も基本的なものを欠いていました:infowindowのインスタンス化:)

次に、情報ウィンドウのプロパティを設定する正しい方法は次のようになります

infowindow.setContent(toolTip);
infowindow.setPosition(event.latLng);

いいえ

infowindow.content = toolTip;

また、情報ウィンドウを呼び出すロジックにも問題がありました。
これは、クリーンアップされたフォークされたフィドルですhttp://jsfiddle.net/b7FHV/

ここに画像の説明を入力

于 2013-09-28T15:02:00.190 に答える