日期:2014-05-16  浏览次数:20360 次

关于html5 谷歌地图路径导航路线问题
今天在利用html5制作地图进行导航的过程中,出现地点可以定位,但是没有路线规划出来。上代码,哪位大神帮忙看看是出什么问题了~感激不尽~~~

var newMap = {a:{},b:{name:"目的地"}}; //全局变量
newMap.directionsDisplay = {};    
newMap.directionsService = new google.maps.DirectionsService(); //这两个是在导航的时候用到的


//初始化地图
function init_Map(mapCenter) {
    newMap.directionsDisplay = new google.maps.DirectionsRenderer();
    var myOptions = {
        zoom:16,
        mapTypeId: google.maps.MapTypeId.ROADMAP, //地图类型
        center: mapCenter   //LatLng 对象
    }
    newMap.map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    newMap.directionsDisplay.setMap(newMap.map); 
}



//导航方法
function My_calcRoute(start,end) {
    var request = {
        origin:start,  //开始的位置 (A)
        destination:end, //开始的位置 (B)
        travelMode: google.maps.TravelMode.DRIVING   //导航类型 驾驶
    };
    newMap.directionsService.route(request, function(result, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(result);
        }
    });
}


function getMyLatLng(){
    if( navigator.geolocation ) {
        function gpsSuccess(pos){

            if( pos.coords ){
                newMap.a.lat = pos.coords.latitude;
                newMap.a.lng = pos.coords.longitude;
            }
            else{
                newMap.a.lat = pos.latitude;
                newMap.a.lng = pos.longitude;
            }
            var userPositon = new google.maps.LatLng(newMap.a.lat,newMap.a.lng);
            var crsPositon = new google.maps.LatLng("22.81366","108.34083");
            init_Map(userPositon);
            My_calcRoute(userPositon,crsPositon);
            addMarker(crsPositon,newMap.map,newMap.b.name);
        &nb