利用google map api计算两地距离

因为工作关系正好要在web上实现计算距离的功能,自然就要利用google map的api来实现,经过一段时间的研究终于小有收获.

完整测试代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
 <meta name="robots" content="noindex,follow" />
 <title>Calculate driving distance with Google Maps API</title>
 <script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAA7j_Q-rshuWkc8HyFI4V2HxQYPm-xtd00hTQOC0OXpAMO40FHAxT29dNBGfxqMPq5zwdeiDSHEPL89A" type="text/javascript"></script>
 <!-- According to the Google Maps API Terms of Service you are required display a Google map when using the Google Maps API. see: http://code.google.com/apis/maps/terms.html -->
 <script type="text/javascript">
 
 var geocoder, location1, location2, gDir;
 
 function initialize() {
  geocoder = new GClientGeocoder();
  gDir = new GDirections();
  GEvent.addListener(gDir, "load", function() {
   var drivingDistanceMiles = gDir.getDistance().meters / 1609.344;
   var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
   document.getElementById('results').innerHTML = '<strong>Address 1: </strong>' + location1.address + ' (' + location1.lat + ':' + location1.lon + ')<br /><strong>Address 2: </strong>' + location2.address + ' (' + location2.lat + ':' + location2.lon + ')<br /><strong>Driving Distance: </strong>' + drivingDistanceMiles + ' miles (or ' + drivingDistanceKilometers + ' kilometers)';
  });
 }
 
 function showLocation() {
  geocoder.getLocations(document.forms[0].address1.value, function (response) {
   if (!response || response.Status.code != 200)
   {
    alert("Sorry, we were unable to geocode the first address");
   }
   else
   {
    location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
    geocoder.getLocations(document.forms[0].address2.value, function (response) {
     if (!response || response.Status.code != 200)
     {
      alert("Sorry, we were unable to geocode the second address");
     }
     else
     {
      location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
      gDir.load('from: ' + location1.address + ' to: ' + location2.address);
     }
    });
   }
  });
 }
 
 </script>
</head>
 
<body οnlοad="initialize()">
 
 <form action="#" οnsubmit="showLocation(); return false;">
  <p>
   <input type="text" name="address1" value="Address 1" />
   <input type="text" name="address2" value="Address 2" />
   <input type="submit" value="Search" />
  </p>
 </form>
 <p id="results"></p>
 
</body>
</html>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Google Map API V3是一种用于在Web应用程序中嵌入和操作Google地图的编程接口。它提供了一组丰富的功能和工具,可以帮助开发者在自己的网站或应用中实现地图和位置相关功能。 使用Google Map API V3,开发者可以轻松地在网页上显示一个互动的地图,包括地图图块、标记、信息窗口、图形和覆盖物等元素。开发者还可以通过调用API提供的方法,获取地图的坐标和视图信息,以及监听用户交互事件。 除了基本的地图显示功能外,Google Map API V3还提供了许多高级功能。例如,开发者可以使用地图的几何库进行位置计算和空间分析,可以实现地图上的地理编码和逆地理编码等操作。API还支持在地图上绘制路径和多边形,并可以使用地图上的各种工具控制地图的交互和视图。 Google Map API V3还与其他Google服务集成得很好。例如,开发者可以通过API调用Google Places服务,实现根据关键字搜索地点和显示地点详细信息的功能。还可以使用API调用Google Street View服务,在地图上显示街景图像。 总而言之,Google Map API V3为开发者提供了一个强大而灵活的工具,可以方便地在自己的网站或应用中集成和操作Google地图,实现丰富的地图和位置相关功能。无论是创建一个简单的地图显示页面,还是实现复杂的地理应用,Google Map API V3都是一个很好的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值