任何人都可以帮我用这个javascript在googlemap中绘制折线....我在谷歌地图中用标记绘制了两个折线,但问题是当我点击标记时细节只打印在一个标记上......还没有在信息窗口内完整打印细节
我的代码如下所示
$(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);
}
});
});});
答案 0 :(得分:0)
您有两个不同的信息窗口,但标记点击监听器仅适用于标记变量。制作两个唯一标记,并为每个打开相应InfoWindow的点击监听器应用。
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)}