简体   繁体   English

如何将侦听器中 function 的返回值分配给变量并将其拆分

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

In the code posted below I have the method MousePosition which has coordinateFormat as an attribute.在下面发布的代码中,我有方法 MousePosition,它具有坐标格式作为属性。

The createStringXY() returns a string encapsulates the longitude and latuitude, and they are comma separated. createStringXY() 返回一个封装了经度和纬度的字符串,它们是逗号分隔的。 The latter function keeps providing the long and lat values as the mouse moves.后者 function 在鼠标移动时不断提供 long 和 lat 值。

What I want to achieve is to assign the values generated from createStringXY() to a variable and then split the string.我想要实现的是将 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
  });
}

If I understand your question correctly, you can create a new variable.如果我正确理解您的问题,您可以创建一个新变量。 Use split() to split the string.使用split()分割字符串。

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

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

Hope it answers your question.希望它能回答你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM