初学google map(二)

行车路线
  可以用GDirections对象来计算行车路线。详细信息查看API文档。
在调试过程中可以查看directions对象的请求状态。计算路径可以用load和loadFromWaypoints方法

来查询,如果参数为多个坐标,则用数组来存放,另外可以根据GDirectionsOptions的参数来来设置

标记是否拖动等其他属性。
<html>
  <head>
    <title>Google Maps JavaScript API Example: Simple Directions</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <script src="http://maps.google.com/maps?file=api&v=2.x&key=ABQIAAAA9rPHPzW1TH1vr2ejmjcezxT2yXp_ZAY8_ufC3CFXhHIE1NvwkxS10E4LTpyI96dJmlDoOiCIW9u_kA"
      type="text/javascript"></script>
    <script type="text/javascript">
 // Create a directions object and register a map and DIV to hold the
    // resulting computed directions

    var map;
    var directionsPanel;
    var directions;

    function initialize() {
     if (GBrowserIsCompatible()) {     
      map = new GMap2(document.getElementById("maps"));
//杭州中心经纬坐标。14为缩放级别
      map.setCenter(new GLatLng(30.320, 120.154), 14);
//设置鼠标滑轮缩放开启
      map.enableScrollWheelZoom();
      directionsPanel = document.getElementById("route");
     directions = new GDirections(map, directionsPanel);

//多个坐标用数组存放,另外坐标前需加上new GLatLng
     directions.loadFromWaypoints([new GLatLng(30.323915428873455,

120.14554023742676),new GLatLng(30.313246211525392, 120.13223648071289),new GLatLng

(30.268111470509464, 120.1658821105957)]);
//检查请求状态来判断查询结果。
     //alert("请求状态:"+directions.getStatus());
//获得路线的距离。
      // document.getElementById("distance").innerHTML=directions.getDistance().meters;
      // var polylines=directions.getPolyline();
  }
 }
    </script>
  </head>
  <body οnlοad="initialize()"  οnunlοad="GUnload()">
    <div id="maps" style="width: 70%; height: 480px; float:left; border: 1px solid

black;"></div>
    <div id="route" style="width: 25%; height:480px; float:right; border; 1px solid

black;"></div>
    <br/>
    <div id="distance" />
    <br/>
  </body>
</html>
另外其他方法详见API文档。http://code.google.com/intl/zh-CN/apis/maps/documentation/reference.html#GDirections

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值