Javascript 谷歌地图地理定位距离计算器
我有一个小的网络应用程序,可以计算谷歌地图上两点之间的距离 我希望它在加载应用程序时将用户放置在当前位置。我尝试过不同的地理定位方法,但没有成功 最好的办法是让它计算与用户位置的距离。然而,对我来说,仅仅让用户在web应用程序中的位置就足够了Javascript 谷歌地图地理定位距离计算器,javascript,geolocation,maps,Javascript,Geolocation,Maps,我有一个小的网络应用程序,可以计算谷歌地图上两点之间的距离 我希望它在加载应用程序时将用户放置在当前位置。我尝试过不同的地理定位方法,但没有成功 最好的办法是让它计算与用户位置的距离。然而,对我来说,仅仅让用户在web应用程序中的位置就足够了 <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> <script typ
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var directionDisplay;
var map;
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var copenhagen = new google.maps.LatLng(55.6771, 12.5704);
var myOptions = {
zoom:12,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: copenhagen
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
directionsDisplay.setMap(map);
}
var directionsService = new google.maps.DirectionsService();
function calcRoute() {
var start = document.getElementById("start").value;
var end = document.getElementById("end").value;
var distanceInput = document.getElementById("distance");
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);
distanceInput.value = response.routes[0].legs[0].distance.value / 1000;
}
});
}
</script>
<title>Distance Calculator</title>
<style type="text/css">
body {
font-family:Helvetica, Arial;
}
#map_canvas {
height: 50%;
}
</style>
</head>
<body>
<body onload="initialize()">
<p>Enter your current location and desired destination to get the distance</p>
<div>
<p>
<label for="start">Start: </label>
<input type="text" name="start" id="start" />
<label for="end">End: </label>
<input type="text" name="end" id="end" />
<input type="submit" value="Calculate Route" onclick="calcRoute()" />
</p>
<p>
<label for="distance">Distance (km): </label>
<input type="text" name="distance" id="distance" readonly="true" />
</p>
</div>
<div id="map_canvas"></div>
</body>
</html>
方向显示;
var映射;
函数初始化(){
directionsDisplay=new google.maps.DirectionsRenderer();
var哥本哈根=新的google.maps.LatLng(55.6771,12.5704);
变量myOptions={
缩放:12,
mapTypeId:google.maps.mapTypeId.ROADMAP,
中心:哥本哈根
}
map=new google.maps.map(document.getElementById(“map_canvas”),myOptions);
方向显示.setMap(地图);
}
var directionsService=new google.maps.directionsService();
函数calcRoute(){
var start=document.getElementById(“start”).value;
var end=document.getElementById(“end”).value;
var distanceInput=document.getElementById(“距离”);
var请求={
来源:start,
目的地:完,
travelMode:google.maps.Directions travelMode.DRIVING
};
路由(请求、功能(响应、状态){
if(status==google.maps.directionstatus.OK){
方向显示。设置方向(响应);
distanceInput.value=response.routes[0]。legs[0]。distance.value/1000;
}
});
}
距离计算器
身体{
字体系列:Helvetica,Arial;
}
#地图画布{
身高:50%;
}
输入当前位置和所需目的地以获取距离
开始:
完:
距离(公里):
您需要试试这个
要使用javascript获取用户的位置,请执行以下操作:
var showPosition = function (position) {
var userLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
// Do whatever you want with userLatLng.
}
navigator.geolocation.getCurrentPosition(showPosition); // Attach showPosition Callback which will be executed as soon as position is available. Remember it needs user confirmation.
例如,见
更新:
您需要更改CalCrout按钮的onclick回调。新的将请求用户的位置。一旦获得位置,就调用calcRoute
function findCurrentLocation() {
navigator.geolocation.getCurrentPosition(calcRoute);
}
在calcRoute中,position对象将作为参数使用,您需要将其转换为LatLng对象以处理google directions服务
function calcRoute(currentLocation) {
// create a LatLng object from the position object.
var start = new google.maps.LatLng(currentLocation.coords.latitude, currentLocation.coords.longitude);
// Continue with your original code.
var end = document.getElementById("end").value;
// etc.
请参见我尝试了您发布的最后一个链接(第一个链接不是关于地理位置的),但我在问题中发布的代码有效。我只需要添加地理位置。是的。。。无法使用我现有的代码:S如果有人可以尝试使用我自己的代码(张贴在问题中)解决这个问题,那就太好了!它要求我现在进入我的位置。我只需要知道在我的代码中放在哪里,让它出现在地图上。很抱歉JS新手提出的问题..@user1749428-输入字段中的预期值是多少?弦,纬度和经度还是两者都有?我想两者都有。用户可以输入城市名称、地址等,而不是坐标。@user1749428-您读过更新的答案了吗?这就是你想要的吗?是的,我想是的!我有一些问题,把代码放在一起,虽然。。。你能帮我从我的问题中提取我的原始代码并用你的建议编辑它吗?谢谢:)
function calcRoute(currentLocation) {
// create a LatLng object from the position object.
var start = new google.maps.LatLng(currentLocation.coords.latitude, currentLocation.coords.longitude);
// Continue with your original code.
var end = document.getElementById("end").value;
// etc.