如何将当前GPS导航器位置设置为destinationA的变量,以查找距离和时间
How do you set the current GPS navigator location as a variable for destinationA to find distance and time
我在这里使用两个脚本。脚本所做的是找到您的当前位置,用标记更新地图,并将当前地址显示为文本。
脚本的第二部分获取您键入的地址,并计算与第二个变量DestinationA的距离和时间。
目前目的地A设置为"美国纽约",但我希望能够将DestinationA变量设置为从GPS导航器中找到的latLng位置。我该如何实现这一目标?
这是我的脚本:
function $(id) {
return document.getElementById(id);
}
var trackerId = 0;
var geocoder;
var theUser = {};
var map = {};
function initialize() {
geocoder = new google.maps.Geocoder();
if (navigator.geolocation) {
var gps = navigator.geolocation;
gps.getCurrentPosition(function (pos) {
var latLng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
var opts = {
zoom: 12,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map($("map_canvas"), opts);
theUser = new google.maps.Marker({
position: latLng,
map: map,
title: "You!"
});
showLocation(pos);
});
var trackerId = gps.watchPosition(function (pos) {
var latLng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
map.setCenter(latLng);
theUser.setPosition(latLng);
showLocation(pos);
});
}
}
function showLocation(pos) {
var latLng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
if (geocoder) {
geocoder.geocode({
'latLng': latLng
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
$("location").innerHTML = results[0].formatted_address;
var addressinput = results[0].formatted_address;
document.getElementById('addressinput').value = addressinput;
}
}
});
}
}
//Calc Distance/Time - to fix..
var bounds = new google.maps.LatLngBounds();
var markersArray = [];
var destinationA = "New York, NY, USA"; //How do I make this my current position?
var destinationIcon = "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=D|FF0000|000000";
var originIcon = "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=O|FFFF00|000000";
function calculateDistances() {
if (navigator.geolocation) {
var gps = navigator.geolocation;
gps.getCurrentPosition(function (pos) {
var latLng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
var opts = {
zoom: 12,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map($("map_canvas"), opts);
theUser = new google.maps.Marker({
position: latLng,
map: map,
title: "You!"
});
showLocation(pos);
});
var trackerId = gps.watchPosition(function (pos) {
var latLng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
map.setCenter(latLng);
theUser.setPosition(latLng);
showLocation(pos);
});
}
var origin1 = document.getElementById('originDestination').value;
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix({
origins: [origin1],
destinations: [destinationA],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, callback);
}
function callback(response, status) {
if (status != google.maps.DistanceMatrixStatus.OK) {
alert('Error was: ' + status);
} else {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
deleteOverlays();
for (var i = 0; i < origins.length; i++) {
var results = response.rows[i].elements;
addMarker(origins[i], false);
for (var j = 0; j < results.length; j++) {
addMarker(destinations[j], true);
document.getElementById('duration').value = results[j].duration.text;
document.getElementById('distance').value = results[j].distance.text;
}
}
}
}
function addMarker(location, isDestination) {
var icon;
if (isDestination) {
icon = destinationIcon;
} else {
icon = originIcon;
}
geocoder.geocode({
'address': location
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
bounds.extend(results[0].geometry.location);
map.fitBounds(bounds);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
icon: icon
});
markersArray.push(marker);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
function deleteOverlays() {
if (markersArray) {
for (i in markersArray) {
markersArray[i].setMap(null);
}
markersArray.length = 0;
}
}
感谢您的帮助
在gps.getCurrentPosition()和gps.watchPosition()的回调中(取决于您想要做什么),设置destinationA:destinationA=新的google.maps.LatLng(位置坐标纬度,位置坐标经度);
但我真的不明白你想做什么……如果目的地现在是用户位置,那么起点是什么?
相关文章:
- 将函数的上下文应用于javascript变量
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- 将PHP变量传递给jQuery时遇到问题
- 如何通过ajax刷新JSF填充的javascript变量
- 参数变量出现ngTable指令问题
- 通过javascript重定向html传递php变量
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- 全局变量和全局对象的属性之间有什么区别吗
- 如何在Bootstrap Modal中为动态点击生成的变量设置jade属性
- 值对象在某个变量发生更改后发生更改
- Javascript变量赋值|
- AngularJS-在JSON选择器中使用变量名
- Javascript计数器变量未显示正确的值
- delete关键字在全局变量上的不同行为
- 如何在php变量中嵌入JQuery代码
- ReactJS映射:如何仅在url变量不为空时呈现html链接
- 在javascript函数中设置全局变量
- 将变量传递给外部Javascript
- 如何将当前GPS导航器位置设置为destinationA的变量,以查找距离和时间