0

谁能帮我用这个javascript在googlemap中绘制折线......我已经在google-map中用标记绘制了两条折线,但问题是当我点击标记时,细节只打印在一个标记上...... ... 详细信息也没有完全打印在信息窗口内

http://jsfiddle.net/wLeBh/16/

我的代码如下

$(function(){
 var map    = new google.maps.Map(document.getElementById("map"), {
                  center: new google.maps.LatLng(11.275387916698238, 75.8015380957031),
                  zoom: 12
               }),
     routes = [{origin:'p t usha road, kozhikode', 
               destination:'cooperative hospital, eranjipalam, kozhikode'
               }, 
               {origin:'IIM, Kozhikode',
               destination:'VELLIMADUKUNNU, KOZHIKODE'
               }
              ],
     rendererOptions = {
                preserveViewport: true,
                map:map,
                polylineOptions:{strokeColor:'#FF3300',
                                 strokeWeight: 10},        
                suppressMarkers:true,
                routeIndex:0
              },
     directionsService = new google.maps.DirectionsService();

      $.each(routes,
        function(i,obj){//<--anonymous function

        var request = {
                origin: obj.origin,
                destination: obj.destination,
                travelMode: google.maps.TravelMode.DRIVING
              },

            directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
            directionsService.route(request, function(result, status) {

              if (status == google.maps.DirectionsStatus.OK) {

var lat = result.routes[0].legs[0].start_location.lat();
var lon = result.routes[0].legs[0].start_location.lng();

    var lat1 = result.routes[0].legs[0].end_location.lat();
var lon1 = result.routes[0].legs[0].end_location.lng();             



                  try{  

                  marker = new google.maps.Marker({
    position: new google.maps.LatLng(lat, lon),
        map: map
      });

                                                   var iws = new google.maps.InfoWindow({
       content: "Home For Sale"
     });
     google.maps.event.addListener(marker, "click", function (e) { iws.open(map, marker); });

                  }catch(e){alert(e)}


                       try{  

                  marker = new google.maps.Marker({
    position: new google.maps.LatLng(lat1, lon1),
        map: map
      });

                            var iw = new google.maps.InfoWindow({
       content: "Home For Sale"
     });
     google.maps.event.addListener(marker, "click", function (e) { iw.open(map, marker); });


                  }catch(e){alert(e)}

                  directionsDisplay.setDirections(result);
              }
            });  

        });});
4

1 回答 1

0

您有两个不同的信息窗口,但标记单击侦听器仅适用于标记变量。制作两个独特的标记并对每个标记应用一个单击侦听器,以打开相应的信息窗口。

try{  
  var marker1 = new google.maps.Marker({
    position: new google.maps.LatLng(lat, lon),
    map: map
  });

  var iws = new google.maps.InfoWindow({
    content: "Home For Sale 1"
   });
   google.maps.event.addListener(marker1, "click", function (e) { iws.open(map, marker1); });

}catch(e){alert(e)}


try{  

  var marker2 = new google.maps.Marker({
    position: new google.maps.LatLng(lat1, lon1),
    map: map
  });

  var iw = new google.maps.InfoWindow({
    content: "Home For Sale 2"
   });
   google.maps.event.addListener(marker2, "click", function (e) { iw.open(map, marker2);});


}catch(e){alert(e)}

更新的小提琴

于 2013-10-08T02:20:14.410 回答