Javascript 跟踪从我所在位置到google maps v3所在位置的路线

Javascript 跟踪从我所在位置到google maps v3所在位置的路线,javascript,html,google-maps,google-maps-api-3,Javascript,Html,Google Maps,Google Maps Api 3,我在绘制从我的位置到某个位置的路线时遇到问题,我需要获取原点变量的纬度和经度,并简单地附加代码,以查看是否有人能够解决此问题 试着留下这些值​​在其他变量中,将它们留在标记中,等等 地理定位 html,正文,#地图画布{ 身高:100%; 边际:0px; 填充:0px } //注意:此示例要求您在以下情况下同意位置共享: //由浏览器提示。如果您看到的是空白区域而不是地图,则 //可能是因为您拒绝了位置共享的权限。 var映射; 函数初始化(){ var-pos; 纬度变异; 长变种; 方向

我在绘制从我的位置到某个位置的路线时遇到问题,我需要获取原点变量的纬度和经度,并简单地附加代码,以查看是否有人能够解决此问题 试着留下这些值​​在其他变量中,将它们留在标记中,等等


地理定位
html,正文,#地图画布{
身高:100%;
边际:0px;
填充:0px
}
//注意:此示例要求您在以下情况下同意位置共享:
//由浏览器提示。如果您看到的是空白区域而不是地图,则
//可能是因为您拒绝了位置共享的权限。
var映射;
函数初始化(){
var-pos;
纬度变异;
长变种;
方向变量;
var directionsService=new google.maps.directionsService();
var directionsDisplay=new google.maps.DirectionsRenderer();
变量映射选项={
缩放:6,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
map=new google.maps.map(document.getElementById('map-canvas'),
地图选项);
//试试HTML5地理定位
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(函数(位置){
pos=新的google.maps.LatLng(position.coords.latitude,
位置坐标经度);
//var-hola=pos;
纬度=位置坐标纬度;
longitud=位置.坐标.经度;
/*var marker=new google.maps.marker({
职位:pos,,
地图:地图,
标题:“你好,世界!”
});
*/
var infowindow=new google.maps.infowindow({
地图:地图,
职位:pos,,
//内容:“使用HTML5找到的位置。”
});            
地图设置中心(pos);
},函数(){
手持导航(真);
});
}否则{
//浏览器不支持地理位置
手动定位(假);
}
var请求={
产地:'圣地亚哥',
目的地:“兰卡瓜”,
travelMode:google.maps.Directions travelMode.DRIVING
};   
方向显示.setMap(地图);
路由(请求、功能(响应、状态){
if(status==google.maps.directionstatus.OK){
/*//显示距离:
document.getElementById('distance')。innerHTML+=
response.routes[0]。legs[0]。distance.value+“米”;
//显示持续时间:
document.getElementById('duration')。innerHTML+=
response.routes[0]。legs[0]。duration.value+“秒”;
*/
方向显示。设置方向(响应);
}
});
}
函数GetAddress(latlang){
var latlng=新的google.maps.latlng(latitud,longitud);
var geocoder=geocoder=new google.maps.geocoder();
geocoder.geocode({'latLng':latLng},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
如果(结果[1]){
dirinical=results[1]。格式化的\u地址;
//警报(“位置:”+结果[1]。格式化的地址);
}
}
});
}
函数handleNogeLocation(errorFlag){
如果(错误标志){
var content='错误:地理位置服务失败';
}否则{
var content='错误:您的浏览器不支持地理位置';
}
变量选项={
地图:地图,
职位:新google.maps.LatLng(60105),
内容:内容
};
var infowindow=new google.maps.infowindow(选项);
地图设置中心(选项位置);
}
google.maps.event.addDomListener(窗口“加载”,初始化);

我在这里看到几个错误。首先,错误管理在与用户最终位置无关的随机位置{lat:60,lng:105}显示一个信息窗口

其次,您的路由请求具有固定的起点和终点(圣地亚哥和兰卡瓜),根本不考虑用户的当前位置。您应该等待HTML5地理定位API返回用作源的内容

我简化了你的代码,让它正常工作了

var map;

function initialize() {
    var pos;
    var latitud;
    var longitud;
    var dirinicial;

    var directionsService = new google.maps.DirectionsService();
    var directionsDisplay = new google.maps.DirectionsRenderer();
    var mapOptions = {
      zoom: 6,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById('map-canvas'),  mapOptions);

    var generateRoute=function(origin,destination) {
        var request = {
           origin:origin, 
           destination: destination,
           travelMode: google.maps.DirectionsTravelMode.DRIVING
        };   

        directionsDisplay.setMap(map);
        directionsService.route(request, function(response, status) {
            if (status === google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);
            }
        });
    };

  // Try HTML5 geolocation
  if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
        pos = new google.maps.LatLng(position.coords.latitude,  position.coords.longitude);

        var infowindow = new google.maps.InfoWindow({
            map: map,
            position: pos,
            content: 'Location found using HTML5.'
        }); 

        var yourpositionmarker = new google.maps.Marker({position:pos, map:map});

        infowindow.open(map,yourpositionmarker);
        map.setCenter(pos);
        generateRoute(pos, 'Rancagua');
    });

  } else {
    // Browser doesn't support Geolocation
    console.log('No geolocation API');
  }

}

google.maps.event.addDomListener(window, 'load', initialize);