google地图的路线实现代码
人气:0复制代码 代码如下:
<script language="javascript" type="text/javascript">
//add by ahuinan 2009-8-17
//initialize(23.165613663312594,113.35968017578125)
var map;
var gdir;
var marker2;
var hasMarker = false;
var hotelLatLng = new GLatLng(<%=initxy %>);//初始化酒店的坐标
var polyline;
var points = [];
points[1] = hotelLatLng;
function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("divMap"), { googleBarOptions: { showOnLoad: true} });
gdir = new GDirections(map);
map.setCenter(hotelLatLng, 9);
map.addControl(new GOverviewMapControl());
map.addControl(new GLargeMapControl());
//加入搜索框
map.enableGoogleBar();
map.enableScrollWheelZoom();
//创建酒店的标注,不可拖动
var hotelMarker = new GMarker(new GLatLng(<%=initxy %>));
map.addOverlay(hotelMarker);
//鼠标左键可以建立一个标记,
GEvent.addListener(map,"click",function(overlay,latlng,overlaylatlng){
if(latlng == null || hasMarker == true){
return;
}
var greenIcon = new GIcon(G_DEFAULT_ICON);
greenIcon.image = "../googlemap/green-dot.png";
greenIcon.iconSize = new GSize(32,32);
greenIcon.iconAnchor = new GPoint(16,32);
greenIcon.shadowSize=new GSize(56,33);
marker2 = new GMarker(latlng,{draggable: true,icon:greenIcon});
map.addOverlay(marker2);
GEvent.addListener(marker2,"dragend",function(){
points[0] = marker2.getLatLng();
gdir.loadFromWaypoints(points,{"preserveViewport":true});
})
GEvent.addListener(gdir, "addoverlay", function(){
for (var i=0; i<=gdir.getNumRoutes(); i++){
var originalMarker = gdir.getMarker(i);
map.removeOverlay(originalMarker);
}
polyline = gdir.getPolyline();
polyline.setStrokeStyle({color:"red",weight:2,opacity:1});
// map.addOverlay(polyline);
});
hasMarker = true;
points[0] = latlng;
gdir.loadFromWaypoints(points,{"preserveViewport":true});
})
}
}
//按下添加按钮
function add(){
if(points.length == 1){
alert("没有线路,终止操作");
}else{
window.opener.document.getElementById("txtHI_MapMarkDesc").value = window.opener.document.getElementById("txtHI_MapMarkDesc").value+"\n"+document.getElementById("txtName").value+"||||"+document.getElementById("txtType").value+"||||"+points[0]+"\n";
window.close();
}
}
window.onload = function() {
initialize()
}
</script>
加载全部内容