Javascript 路线服务不';当我更改google.maps.LatLng时,我无法在谷歌地图中工作
我正试图开发一个应用程序使用谷歌地图 申请说明: 用户可以在屏幕上看到谷歌地图,并且应该能够单击地图并看到被单击的点(首次单击),当用户再次单击时,应该绘制两个位置之间的路线 我发现:Javascript 路线服务不';当我更改google.maps.LatLng时,我无法在谷歌地图中工作,javascript,jquery,asp.net-mvc,google-maps,Javascript,Jquery,Asp.net Mvc,Google Maps,我正试图开发一个应用程序使用谷歌地图 申请说明: 用户可以在屏幕上看到谷歌地图,并且应该能够单击地图并看到被单击的点(首次单击),当用户再次单击时,应该绘制两个位置之间的路线 我发现: 在asp.net网站中显示带纬度和经度的Google地图 var映射; var directionsDisplay=new google.maps.DirectionsRenderer(); var directionsService=new google.maps.directionsService();
在asp.net网站中显示带纬度和经度的Google地图
var映射;
var directionsDisplay=new google.maps.DirectionsRenderer();
var directionsService=new google.maps.directionsService();
函数计算(开始、结束){
方向显示.setMap(地图);
var请求={
来源:start,
目的地:完,
travelMode:google.maps.Directions travelMode.DRIVING
};
路由(请求、功能(响应、状态){
if(status==google.maps.directionstatus.OK){
方向显示。设置方向(响应);
}
});
}
函数初始化(){
var mylatng=new google.maps.LatLng(35.18061975930,51.36565089010);
变量myOptions={
缩放:7,
中心:myLatlng,
mapTypeId:google.maps.mapTypeId.ROADMAP
}
map=新的google.maps.map(document.getElementById(“gmap”),myOptions);
//标记是指一个全局变量
marker=新的google.maps.marker({
职位:myLatlng,
地图:地图
});
google.maps.event.addListener(映射,“单击”,函数(事件){
//获取单击的纬度/经度
var clickLat=event.latLng.lat();
var clickLon=event.latLng.lng();
//在输入框中显示
document.getElementById(“lat”).value=单击lat.toFixed(5);
document.getElementById(“lon”).value=clickLon.toFixed(5);
var marker=new google.maps.marker({
位置:新建google.maps.LatLng(点击Lat,点击Lon),
地图:地图
});
var newLatLon=newgoogle.maps.LatLng(点击lat,点击lon);
卡尔克劳特(迈拉特朗,纽拉特伦);
});
}
window.onload=函数(){initialize()};
分区gmap{
宽度:80%;
高度:500px;
边界:双;
}
拉丁美洲:
朗:
拉丁美洲:
朗:
在示例代码中,我只是更改了这个var mylatng=new google.maps.LatLng(35.18061975930,51.36565089010)代码>,当我更改它时,路由服务不工作。为什么?
我在map.google.com上试过了,效果很好:
如果您查看以下链接:
“出行方式”一节
“如果您请求该方向类型不可用的区域的方向,响应将返回DirectionStatus=“ZERO\u RESULTS”。”
在您的情况下,它返回“零结果”:
看起来这个位置离任何道路都太远了,所以方向服务不知道从哪里开始路线。如果将位置移近道路,例如:var mylatng=new google.maps.LatLng(35.4900000000,51.36565089010)代码>然后你会看到它是有效的。当我使用map.google.com时,我可以得到我所在地区伊朗两个点之间的方向,为什么我在这里不能?我在map.google.com上试过,从那一点35.18061975930,51.36565089010,它无法画出路线。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Show Google Map with Latitude and Longitude in asp.net website</title>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC6v5-2uaq_wusHDktM9ILcqIrlPtnZgEk&sensor=false">
</script>
<script type="text/javascript">
var map;
var directionsDisplay = new google.maps.DirectionsRenderer();
var directionsService = new google.maps.DirectionsService();
function calcRoute(start, end) {
directionsDisplay.setMap(map);
var request = {
origin: start,
destination: end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
function initialize() {
var myLatlng = new google.maps.LatLng(35.18061975930, 51.36565089010);
var myOptions = {
zoom: 7,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("gmap"), myOptions);
// marker refers to a global variable
marker = new google.maps.Marker({
position: myLatlng,
map: map
});
google.maps.event.addListener(map, "click", function (event) {
// get lat/lon of click
var clickLat = event.latLng.lat();
var clickLon = event.latLng.lng();
// show in input box
document.getElementById("lat").value = clickLat.toFixed(5);
document.getElementById("lon").value = clickLon.toFixed(5);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(clickLat, clickLon),
map: map
});
var newLatLon = new google.maps.LatLng(clickLat, clickLon);
calcRoute(myLatlng, newLatLon);
});
}
window.onload = function () { initialize() };
</script>
<style>
div#gmap {
width: 80%;
height: 500px;
border:double;
}
</style>
</head>
<body>
<form id="form1" runat="server">
Lat: <input type="text" id='lat'>
Lon: <input type="text" id='lon'>
<center>
<!-- MAP HOLDER -->
<div id="gmap"></div>
<!-- REFERENCES -->
lat:
lon:
</center>
</form>
</body>
</html>
directionsService.route(request, function(response, status) {
console.log(response);
alert(status);
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});