簡體   English   中英

如何將偵聽器中 function 的返回值分配給變量並將其拆分

[英]how to assign a returned value from a function in a listener to variable and split it

在下面發布的代碼中,我有方法 MousePosition,它具有坐標格式作為屬性。

createStringXY() 返回一個封裝了經度和緯度的字符串,它們是逗號分隔的。 后者 function 在鼠標移動時不斷提供 long 和 lat 值。

我想要實現的是將 createStringXY() 生成的值分配給一個變量,然后拆分字符串。

ngOnInit() {
  var mousePositionControl = new MousePosition({
    className: 'custom-mouse-position',
    coordinateFormat: createStringXY(7),
    projection: 'EPSG:4326',
    // comment the following two lines to have the mouse position
    // be placed within the map.
    target: document.getElementById('mouse-position'),
    undefinedHTML: '', //for what to be rendered when the mouse leaves map scope: values https://openlayers.org/en/latest/apidoc/module-ol_control_MousePosition-MousePosition.html
  });
}

如果我正確理解您的問題,您可以創建一個新變量。 使用split()分割字符串。

var result = createStringXY(7)
var [long, lat] = result.split(",")    

// Or doing it in one line
// var [long, lat] = createStringXY(7).split(",")

希望它能回答你的問題。

暫無
暫無

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

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