簡體   English   中英

Console.log('variable')-如何顯示

[英]Console.log('variable') - how to show

這里我有一個函數route();

function route() {
      // Clear any previous route boxes from the map
      clearBoxes();

      // Convert the distance to box around the route from miles to km
      distance = parseFloat(document.getElementById("distance").value) * 1.609344;

      var request = {
        origin: document.getElementById("from").value,
        destination: document.getElementById("to").value,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
      }
      if (status == google.maps.DirectionsStatus.OK) {
  directionsRenderer.setDirections(result);
  var route = response.routes[0];
  startLocation = new Object();
  endLocation = new Object();

  var path = response.routes[0].overview_path;
  var legs = response.routes[0].legs;
  for (i=0;i<legs.length;i++) {
    if (i == 0) { 
      startLocation.latlng = legs[i].start_location;
      startLocation.address = legs[i].start_address;
    }
    endLocation.latlng = legs[i].end_location;
    endLocation.address = legs[i].end_address;
  }
      }
      // Make the directions request
      directionService.route(request, function(result, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          directionsRenderer.setDirections(result);

          // Box around the overview path of the first route
          var path = result.routes[0].overview_path;
          var boxes = routeBoxer.box(path, distance);
          drawBoxes(boxes);
        } else {
          alert("Directions query failed: " + status);
        }
      });
    }

此功能在兩個地方之間創建路線方向,並在地圖上放置2個標記(起點標記和終點標記)

現在,我想在其他功能中使用此標記位置,並在console.log中顯示以查看代碼發生了什么。

我嘗試:

console.log(startLocation.latlng);

但隨后進入控制台:

ReferenceError: startLocation is not defined
get stack: function () { [native code] }
message: "startLocation is not defined"
set stack: function () { [native code] }
__proto__: Error

如何在全局變量中顯示此起點和終點緯度,經度數據以用於其他功能?

更新:我真正需要的是使用功能ROUTE()獲取已經創建的標記的位置,該操作是:

function route() {
      // Clear any previous route boxes from the map
      clearBoxes();

      // Convert the distance to box around the route from miles to km
      distance = parseFloat(document.getElementById("distance").value) * 1.609344;

      var request = {
        origin: document.getElementById("from").value,
        destination: document.getElementById("to").value,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
      }

      // Make the directions request
      directionService.route(request, function(result, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          directionsRenderer.setDirections(result);

          // Box around the overview path of the first route
          var path = result.routes[0].overview_path;
          var boxes = routeBoxer.box(path, distance);
          drawBoxes(boxes);
        } else {
          alert("Directions query failed: " + status);
        }
      });
    }

在任何編程語言(包括javascript)中使用全局變量通常不是一個好主意。 理想情況下,您應該在函數內部以及外部作用域的其他函數中聲明要訪問的變量。

例如,假設您的代碼是這樣的。

var valueOne;
function setValue(){
    valueOne = 3;
}
function readValue(){
    console.log(valueOne);
}
setValue();
readValue();

僅在readValue之前調用setValue時,才會記錄“ 3”。 在這種情況下,您必須100%確保在包含兩個函數的作用域中聲明變量,並按該順序調用它們。

調整您自己的代碼的另一種方法是使用window.startLocation,window.endLocation等,這會將變量附加到window對象,從而使它們全局可用。 默認情況下,在javascript中使用不帶var startLocation varvar startLocation )的變量應該使它成為全局變量,但是如果不起作用,則可以嘗試將其直接附加到window對象。 無論如何,您必須確保設置要記錄在另一個函數中使用的值的函數在另一個函數之前被調用,如果不是,則顯然不會設置這些值。

startLocation是此函數的局部變量。 您需要從這里返回它才能從其他功能訪問。 或者,使用類范圍。 閱讀有關javascript的任何標准書籍。 您也可以將其設置為全局,但我不建議這樣做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM