關於GDIRECTION的CLEAR方法

14 views
Skip to first unread message

wei

unread,
Oct 27, 2009, 2:02:48 PM10/27/09
to Google API 技術論壇


GEvent.addListener(map, "click", clickDirection);

function clickDirection(overlay, latlng) {
directions = new GDirections(map, directionsPanel);
directions.clear();
var lat = latlng.lat();
var lng = latlng.lng();
var latlngs = latlng.lat() + "," + latlng.lng();
arrDir.push(latlngs);
directions.loadFromWaypoints(arrDir, { "avoidHighways":
true });

}

arrDir陣列宣告在FUNCTION INITIALIZE外

想請問一下為什麼這裡的CLEAR方法都沒有發揮作用
因為想要每點一次都會把上次留存的DIRECTION陸線刪掉
但是沒有作用
請問問題出在哪?

Kevin Liu

unread,
Oct 27, 2009, 6:31:10 PM10/27/09
to wei, Google API 技術論壇
you are creating a new GDirections every time in the function;
you should create it once and then clear it every time, something like:

var directions = new GDirections(map, directionsPanel);
function clickDirection(overlay, latlng) {
           directions.clear();
           ...
}


2009/10/28 wei <wei...@gmail.com>



--
Kevin Tsung-Yuan Liu
Google Taipei Software Engineer

wei

unread,
Oct 28, 2009, 12:07:07 AM10/28/09
to Google API 技術論壇
另外想請問這樣的寫法
為什麼會造成每次SHOW出來的路徑距離都少一次
就是我click第二次秀出路徑後並沒有秀出距離
而是click第三次之後才秀出地一次到第二次的距離
請問問題是出在哪裡?

GEvent.addListener(map, "click", clickDirection);

function clickDirection(overlay, latlng) {

var lat = latlng.lat();
var lng = latlng.lng();
var latlngs = latlng.lat() + "," + latlng.lng();
arrDir.push(latlngs);

directions.loadFromWaypoints(arrDir, { "avoidHighways":
true });
var dist = directions.getDistance();
document.getElementById("Label13").innerHTML = dist.html;
}



// var dist = directions.getDistance();
// document.getElementById("Distance").innerHTML =
dist.meters;
// document.getElementById("Distance").value =
dist.meters;

}

On 10月28日, 上午6時31分, Kevin Liu <kty...@google.com> wrote:
> you are creating a new GDirections every time in the function;
> you should create it once and then clear it every time, something like:
>
> var directions = new GDirections(map, directionsPanel);
> function clickDirection(overlay, latlng) {
> directions.clear();
> ...
>
> }
>
> 2009/10/28 wei <weic...@gmail.com>

Kevin Liu

unread,
Oct 28, 2009, 7:03:54 AM10/28/09
to Google API 技術論壇
getting directions is not instantaneous,
you need to do something like:
GEvent.addListener(directions, "load", onGDirectionsLoad);
function onGDirectionsLoad() {
  var dist = directions.getDistance();
  document.getElementById("Label13").innerHTML = dist.html;
}



2009/10/28 wei <wei...@gmail.com>

wei

unread,
Oct 28, 2009, 4:37:06 PM10/28/09
to Google API 技術論壇
感謝你
問題解決了

On 10月28日, 下午7時03分, Kevin Liu <kty...@google.com> wrote:
> getting directions is not instantaneous,
> you need to do something like:
> GEvent.addListener(directions, "load", onGDirectionsLoad);
> function onGDirectionsLoad() {
> var dist = directions.getDistance();
> document.getElementById("Label13").innerHTML = dist.html;
>
> }
>
> see a complete example at:http://code.google.com/intl/zh-TW/apis/maps/documentation/examples/di...
>
> 2009/10/28 wei <weic...@gmail.com>
Reply all
Reply to author
Forward
0 new messages