关于Javascript& amp的问题Google Map API

时间:2011-01-08 11:28:45

标签: javascript api google-maps

我目前正在开展一个关于出租车的项目。我想在谷歌地图上显示一些出租车但遇到以下问题。当程序变成print_taxi()函数时,taxilatlng []中的所有数据都将消失!似乎所有数据都只能保存在directionsservice.route(){}中。需要帮助。

<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>
onerror=handleErr
var txt=""

function handleErr(msg,url,l)
{
    txt="There was an error on this page.\n\n"
    txt+="Error: " + msg + "\n"
    txt+="URL: " + url + "\n"
    txt+="Line: " + l + "\n\n"
    txt+="Click OK to continue.\n\n"
    alert(txt)
    return true
}

var centerlat=22.551622;
var centerlng=114.121178;
var taxi_num=10;
var taxiradius=0.005;

var taxilatlng = new Array();
var taximarker = new Array();
var taxilat = new Array();
var taxilng = new Array();
var map;

var RANDOM_DATA_INITIALIZED;

function get_random(lowerbound, upperbound){
    return lowerbound+Math.floor((upperbound-lowerbound)*Math.random());
}

function get_random_loc(now, radius){
    return now+radius-Math.random()*2*radius;
}

function get_random_latlng(taxiradius){
    var tmp=new google.maps.LatLng(get_random_loc(centerlat,taxiradius), get_random_loc(centerlng,taxiradius));
    return tmp;
}

function dump_obj(myObject) {  
  var s = "";  
  for (var property in myObject) {  
  s = s + "\n "+property +": " + myObject[property] ;  
  }
  alert(s);  
}  

function init_map()
{

    var latlng=new google.maps.LatLng(centerlat, centerlng);
    var options={
        zoom:16,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map=new google.maps.Map(document.getElementById("map_canvas"), options);

}

function print_taxi(){
    image="taxi.gif";
    for(var i=1;i<=taxi_num; ++i){
        taximarker[i]=new google.maps.Marker({
            position: taxilatlng[i],
            icon: image,
            map: map
        });
    }
}

function get_taxi_pos()
{
    var cnt_generatedcar=0;

    for(var i=1;i<=taxi_num; i++)
    {
        var from=get_random_latlng(taxiradius);
        var to=get_random_latlng(taxiradius);
        var directionsservice=new google.maps.DirectionsService();
        var taxirenderer=new google.maps.DirectionsRenderer();
        var DirReq={
                origin: from,
                destination: to,
                travelMode: google.maps.DirectionsTravelMode.DRIVING
        };

        directionsservice.route(DirReq, function(response, status){
            if(status==google.maps.DirectionsStatus.OK)
            {
                var all_routes = response.routes;
                for(curroute in all_routes)
                {
                    ++cnt_generatedcar;
                    var all_paths = all_routes[curroute].overview_path;
                    var cnt_paths = all_paths.length;
                    var id_path=get_random(0, cnt_paths-1);
                    taxilatlng[i] = all_paths[id_path];
                    if(cnt_generatedcar==taxi_num){
                        print_taxi();
                    }
                }
            }
        });
    }
}

function init(){

    init_map();

    get_taxi_pos();

}


</script>
</head>

<body onload="init();">
<div id="map_canvas" style="float:left; width:70%; height:100%"></div>
<div id="msg"></div>
</body>

</html>

1 个答案:

答案 0 :(得分:0)

directionsservice.route是一种回调方法,仅在i&gt; taxi_num之后调用。因此,taxilatlng中的值仅在index(taxi_num + 1)处设置一次。您可以将代码修改为:

 var ctr = -1; // added
 directionsservice.route(DirReq, function(response, status){if(status==google.maps.DirectionsStatus.OK)
        {
            var all_routes = response.routes;
    ctr++;
            for(curroute in all_routes)
            {
                ++cnt_generatedcar;
                var all_paths = all_routes[curroute].overview_path;
                var cnt_paths = all_paths.length;
                var id_path=get_random(0, cnt_paths-1);
                taxilatlng[ctr] = all_paths[id_path];

                if(cnt_generatedcar==taxi_num){
                    print_taxi();
                }
            }
        }
    });

我刚用一个新变量作为计数器而不是i。