当前位置: 首页 > 图文教程 > 网络编程 > Javascript > google地图的路线实现代码

Javascript
javascript实例教程(19) 使用HoTMetal(5)
javascript实例教程(19) 使用HoTMetal(6)
javascript实例教程(19) 使用HoTMetal(7)
javascript实例教程(20) OLE Automation(1)
javascript实例教程(20) OLE Automation(2)
javascript实例教程(20) OLE Automation(3)
javascript实例教程(20) OLE Automation(4)
javascript实例教程(20) OLE Automation(5)
javascript实例教程(20) OLE Automation(6)
javascript实例教程(20) OLE Automation(7)
用javascript实现利用FLASH嵌入声音
javascript版的日期输入控件(6)
javascript设计网页中的下拉菜单
javascript设计漫天雪花
javascript制作浮动的工具条
javascript制作闪烁的边框
javascript模拟游戏中的弹出菜单效果
在IE中使用javascript
利用javascript制作简单动画
首页地址添加到收藏夹(javascript)

Javascript 中的 google地图的路线实现代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-12   浏览: 250 ::
收藏到网摘: n/a

google地图的路线实现代码,大家可以参考下,实现自己想要的功能。
复制代码 代码如下:

<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>