google map api查询线路(转帖)
2010-04-03 16:16
453 查看
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>
<%--
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>--%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Google maps API 查询路线,任何地点到任何地点</title>
<script src="http://ditu.google.com/maps?file=api&v=2&key=ABQIAAAAQ0pgyic4ROE9rr4PWF1qBRDvuDKJLLEMBt0175lffxKd2WRRBRIVZEREjaUWbrLdbvDieibc2PHlg&hl=zh-CN" type="text/javascript"></script>
</head>
<body onload="initialize();" onunload="GUnload();">
<div style="width:1000px;margin:0 auto;">
<div style="height:50px;">
<p><label for="from_tbx">从:</label><input type="text" id="from_tbx" value="" />
<label for="to_tbx">到:</label><input type="text" id="to_tbx" value="" />
<input type="button" id="luxian" onclick="javascript:luxian();" value="驾车路线查询" /></p>
</div>
<div id="map_info">
<div id="direction" style="width:350px;height:500px;float:left;overflow:auto;"></div>
<div id="googlemap" style="width:600px;height:500px;margin-right:8px;float:right;"></div>
</div>
</div>
<script type="text/javascript">
var map;
var gdir;
var geocoder = null;
var addressMarker;
var centerRoute = "31.238508612629097,121.46677494049072";
function initialize()
{
if (GBrowserIsCompatible())
{
map = new GMap2(document.getElementById("googlemap"));
var center = new GLatLng(31.238508612629097,121.46677494049072);
map.setCenter(center,13);
gdir = new GDirections(map,document.getElementById('direction'));
GEvent.addListener(gdir,"error",handleErrors);
geocoder = new GClientGeocoder();
//定义一些控件
var customUI = map.getDefaultUI();
customUI.maptypes.hybrid = false;
customUI.maptypes.physical = false;
map.setUI(customUI);
map.disableDoubleClickZoom();
//因为后面要重新定义双击事件,这里移除双击放大事件
var marker = new GMarker(center);
map.addOverlay(marker);
}
}
//乘车路线
function luxian()
{
var fromAddress = document.getElementById('from_tbx');
var toAddress = document.getElementById('to_tbx');
var address = fromAddress.value;
//getLocations方法解析地址,返回json
geocoder.getLocations(address,
function(json)
{
if (!json)
{
alert("解析/""+address+"/"错误");
}
else
{
//地址解析为坐标可能有多个结果,这里只取查询结果的第一个json.Placemark[0]
//有兴趣扩展可以通过遍历json输出所有查询结果,让使用者选择最符合的结果。
var addressRoute ="from:"+address+"@"+json.Placemark[0].Point.coordinates[1]
+","+json.Placemark[0].Point.coordinates[0]
+" to:"+toAddress.value+"@";
//设置路线起始点坐标
gdir.load(addressRoute,{travelMode:G_TRAVEL_MODE_DRIVING});
}
});
}
function handleErrors(){
if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect./nError code: " +gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known./n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input./n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_BAD_KEY)
alert("The given key is either invalid or does not match the domain for which it was given. /n Error code: "+ gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
alert("A directions request could not be successfully parsed./n Error code: " + gdir.getStatus().code);
else alert("An unknown error occurred.");
}
</script>
</body>
</html>
<%--
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>--%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Google maps API 查询路线,任何地点到任何地点</title>
<script src="http://ditu.google.com/maps?file=api&v=2&key=ABQIAAAAQ0pgyic4ROE9rr4PWF1qBRDvuDKJLLEMBt0175lffxKd2WRRBRIVZEREjaUWbrLdbvDieibc2PHlg&hl=zh-CN" type="text/javascript"></script>
</head>
<body onload="initialize();" onunload="GUnload();">
<div style="width:1000px;margin:0 auto;">
<div style="height:50px;">
<p><label for="from_tbx">从:</label><input type="text" id="from_tbx" value="" />
<label for="to_tbx">到:</label><input type="text" id="to_tbx" value="" />
<input type="button" id="luxian" onclick="javascript:luxian();" value="驾车路线查询" /></p>
</div>
<div id="map_info">
<div id="direction" style="width:350px;height:500px;float:left;overflow:auto;"></div>
<div id="googlemap" style="width:600px;height:500px;margin-right:8px;float:right;"></div>
</div>
</div>
<script type="text/javascript">
var map;
var gdir;
var geocoder = null;
var addressMarker;
var centerRoute = "31.238508612629097,121.46677494049072";
function initialize()
{
if (GBrowserIsCompatible())
{
map = new GMap2(document.getElementById("googlemap"));
var center = new GLatLng(31.238508612629097,121.46677494049072);
map.setCenter(center,13);
gdir = new GDirections(map,document.getElementById('direction'));
GEvent.addListener(gdir,"error",handleErrors);
geocoder = new GClientGeocoder();
//定义一些控件
var customUI = map.getDefaultUI();
customUI.maptypes.hybrid = false;
customUI.maptypes.physical = false;
map.setUI(customUI);
map.disableDoubleClickZoom();
//因为后面要重新定义双击事件,这里移除双击放大事件
var marker = new GMarker(center);
map.addOverlay(marker);
}
}
//乘车路线
function luxian()
{
var fromAddress = document.getElementById('from_tbx');
var toAddress = document.getElementById('to_tbx');
var address = fromAddress.value;
//getLocations方法解析地址,返回json
geocoder.getLocations(address,
function(json)
{
if (!json)
{
alert("解析/""+address+"/"错误");
}
else
{
//地址解析为坐标可能有多个结果,这里只取查询结果的第一个json.Placemark[0]
//有兴趣扩展可以通过遍历json输出所有查询结果,让使用者选择最符合的结果。
var addressRoute ="from:"+address+"@"+json.Placemark[0].Point.coordinates[1]
+","+json.Placemark[0].Point.coordinates[0]
+" to:"+toAddress.value+"@";
//设置路线起始点坐标
gdir.load(addressRoute,{travelMode:G_TRAVEL_MODE_DRIVING});
}
});
}
function handleErrors(){
if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect./nError code: " +gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known./n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input./n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_BAD_KEY)
alert("The given key is either invalid or does not match the domain for which it was given. /n Error code: "+ gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
alert("A directions request could not be successfully parsed./n Error code: " + gdir.getStatus().code);
else alert("An unknown error occurred.");
}
</script>
</body>
</html>
相关文章推荐
- web地图信息格网索引-----基于google map api和oracle查询的生活地图
- google map api 与jquery结合使用(1)--控件,监听器[转帖]
- google map api 与jquery结合使用(2) --标注,浮窗[转帖]
- google map api 与jquery结合使用(3) --图标样式,使用xml和异步请求【转帖】
- iOS Cllocationmanger定位, 使用Google Map API在mapview上画路径
- android google map api的具体使用
- 关于注册Google Map api key的方法和解决网页无法显示的问题
- google map api在ie6中:缺少对象的解决方案
- 关于google map api中的球平投影算法接口: GProjection和GMercatorProjection类 - tim-wu - BlogJava
- Google Map API的使用
- Google Map API
- google map api javascript xml 生成一个简单的地图
- Google的全新在线地图API演示网站 - More than a map
- 使用百度API实现实时公交线路查询及地图显示
- 获取google Map API Key方法
- Google Map Api 简单使用笔记
- PHP查询Google PR值接口API介绍
- Google Map API
- googleMap API及调用实例
- Android或者Windows Phone在HTTP调用Google Map Api时返回英文结果的解决方法