我在使用Google Maps API时陷入困境 - 我正在使用GDirections
对象来获取两点之间旅行所需的时间。
我在Firebug中测试了以下代码,因此我的其余代码影响这一次调用的可能性很低(我无法链接到实际的地图;它位于本地计算机上)。
var start = "NY, USA";
var end = "CA, USA";
var searchString = "from: " + start + " to: " + end;
C.console(searchString);
var myDir = new GDirections();
GEvent.addListener(myDir,"load",C.console(myDir,getDuration()));
myDir.load(searchString);
C.console
只是我写的一个函数,用于将其参数打印到Firebug调试日志中。当我运行该代码时,它输出searchString from: NY, USA to: CA, USA
并调用回调函数。但是,它输出null
而不是GDirections
对象的持续时间。
接下来,我运行
C.console(myDir.getDuration())
并且输出
Object seconds=157994 html=1 day 20 hours
根据需要。有谁知道为什么需要两次调用才能工作?我认为这是时间依赖的,因为在代码中如果我只是调用它两次它会连续两次给我null,这并不奇怪。不过,我已经使用事件监听器等待它完成加载。我也尝试使用addoverlay
事件代替load
事件,但这也无效。
有没有人见过这个或者有关于如何修复它的想法?任何帮助将不胜感激!
答案 0 :(得分:2)
你的问题是这一行
GEvent.addListener(myDir,"load",C.console(myDir,getDuration()));
导致C.console立即执行,无论它返回什么都将被用作回调函数。那不是你想要的。你想要的是在回调发生时调用C.console。一种方法是
GEvent.addListener(myDir,"load",function() {
C.console(myDir,getDuration())
});
答案 1 :(得分:0)
我记得我遇到了类似的问题,但它的性质不同,但问题结构是一样的。
我必须点击两次才能正常工作。我终于明白了,这是代码,谷歌API上的我的原始帖子(http://groups.google.com/group/Google-AJAX-Search-API/browse_thread/thread/c6c46cc8a0435eb0/f697b028bbce18db#f697b028bbce18db)
<script type='text/javascript'>
google.load('search', '1');
google.load('maps', '2');
//Define our Globals
var map; //Map API
var gdir; //Direction API
var gFirstSearch; //The From Local Search
var gSecondSearch; //The To Local Search
var fromAddress; //From Address The user inputs
var toAddress; //To Address the user inputs
var first; //First Set of Results for From Search
var second; //Second Set of Results for To Search
//On Load, Load all the Details Needed
function OnLoad(){
//Set up the Map and the Globals
//If the Browser Supports its
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(52.037366,-0.703726), 7);
map.removeMapType(G_HYBRID_MAP);
var mapControl = new GMapTypeControl();
map.addControl(mapControl);
map.addControl(new GLargeMapControl());
gdir = new GDirections(map);
GEvent.addListener(gdir, "load", onGDirectionsLoad);
GEvent.addListener(gdir, "error", handleErrors);
gFirstSearch = new google.search.LocalSearch();
gFirstSearch.setCenterPoint(map);
gFirstSearch.setSearchCompleteCallback(null, FirstSearch);
gSecondSearch = new google.search.LocalSearch();
gSecondSearch.setCenterPoint(map);
gSecondSearch.setSearchCompleteCallback(null, SecondSearch);
}
}
//Run the From Search
//Runs after the gFirstSearch.execute has finished
//Reference: setSearchCompleteCallback
function FirstSearch(){
if (!gFirstSearch.results.length){ alert("Could Not Find: " +
fromAddress + "\n Please Try Refining your 'From' Address Field");
return; }
//Return the First Result into a Variable
first = gFirstSearch.results[0];
//Execute the Second
gSecondSearch.execute(toAddress);
}
//Run the To Search
//Runs after the gSecondSearch.execute has finished
//Reference: setSearchCompleteCallback
function SecondSearch(){
if (!gSecondSearch.results.length){ alert("Could Not Find: " +
toAddress + "\n Please Try Refining your 'To' Address Field");
return; }
//Returns the Second results into a Variable
second = gSecondSearch.results[0];
//Plot our Graph
gdir.load("from: " + (first.lat + ", " + first.lng) + " to: " +
(second.lat + ", " + second.lng));
}
//Use to Execite our Form Commands
function setDirections(ifromAddress, itoAddress) {
//Initiate the inputs into our Global Variables
fromAddress = ifromAddress;
toAddress = itoAddress;
//Execute our Search
gFirstSearch.execute(fromAddress);
//Return False so our broweser dosent Refresh
return false;
}
//Set the Values in our HTML after Direction has loaded
function onGDirectionsLoad(){
var miles = gdir.getDistance().meters * 0.000621371192; //Convert to
Miles
document.getElementById("distance").innerHTML = "Distance: " + miles
+ " ml";
// and yada yada yada...
}
</script>
通过观察这个我记得第一个获取方向是作为线程运行然后第二个也作为线程运行,那么为什么需要2次点击是因为当你执行gdirection时第二个没有加载hense值仍为null。因此,你需要一些如何使gdirection等到第一个和第二个返回其值
注意:上面代码中的两个callBack
gFirstSearch.setSearchCompleteCallback(null, FirstSearch);
gSecondSearch.setSearchCompleteCallback(null, SecondSearch);
注意:函数gSecondSearch.execute(toAddress)正在函数firstSearch()中运行
//Run the From Search
//Runs after the gFirstSearch.execute has finished
//Reference: setSearchCompleteCallback
function FirstSearch(){
if (!gFirstSearch.results.length){
alert("Could Not Find: " + fromAddress);
return;
}
//Return the First Result into a Variable
first = gFirstSearch.results[0];
//Execute the Second
gSecondSearch.execute(toAddress);
}
我解决它的方法是将其置于回调函数中。进程完成时调用回调函数
以下是步骤
如果你仍然迷失在这个概念上,请告诉我.. il编辑它并将它分开一点,这样你就可以理解,但它应该可以解决你的问题
答案 2 :(得分:0)
这让我很难过。我玩了代码,这有效:
var start = "NY, USA";
var end = "CA, USA";
var searchString = "from: " + start + " to: " + end;
C.console(searchString);
var directions = new GDirections();
GEvent.addListener(directions,"load",function() {
C.console(directions.getDuration());
});
directions.load(searchString);
除了变量变化(myDir
现在是directions
)之外,我还使用了一个匿名函数作为回调函数。这似乎是一个范围问题,也许第二次通过变量已经定义,所以它不再是null
。无论如何,这似乎现在正常 - 我希望这也有助于其他人。
答案 3 :(得分:0)
如何使用GDirections?
var dir=new GDirections(map);
var queryString="from: "+ map.getCenter()+"to: "+marker.getLatLng();
dir.load(queryString);
alert(dir.getDistance().meters);
错误:未定义dir.getDistance()....
答案 4 :(得分:0)
<强>&GT;如何使用GDirections
回复上述查询,可以通过以下链接查看显示的用法 重新定义事物的方向
a) Distance between the two locations b)两个论坛之间的持续时间。