How to run javascript function?

myndeswx

Client
Регистрация
15.05.2017
Сообщения
436
Благодарностей
104
Баллы
43
Hello, maybe a little stupid question, but how to run such javascript function in zenno? Never really needed to do so, but using external services for this job is not suitable for the job. Thank you!

JavaScript:
 function decompress (encoded, precision) {
   precision = Math.pow (10, -precision);
   var len = encoded.length, index = 0, lat = 0, lng = 0, array = [];
   while (index <len) {
      var b, shift = 0, result = 0;
      do {
         b = encoded.charCodeAt (index ++) - 63;
         result | = (b & 0x1f) << shift;
         shift + = 5;
      } while (b> = 0x20);
      var dlat = ((result & 1)? ~ (result >> 1): (result >> 1));
      lat + = dlat;
      shift = 0;
      result = 0;
      do {
         b = encoded.charCodeAt (index ++) - 63;
         result | = (b & 0x1f) << shift;
         shift + = 5;
      } while (b> = 0x20);
      var dlng = ((result & 1)? ~ (result >> 1): (result >> 1));
      lng + = dlng;
      array.push (lat * precision);
      array.push (lng * precision);
   }
   return array;
}
 

Astraport

Client
Регистрация
01.05.2015
Сообщения
4 983
Благодарностей
4 433
Баллы
113
Try insert your code above without 'function decompress (encoded, precision) {' and '}' in Zenno JS action (run on local window) and set up the variables before this code:
JavaScript:
var  encoded = {-Variable.encoded-};
var  precision = {-Variable.precision-};
 
  • Спасибо
Реакции: myndeswx

myndeswx

Client
Регистрация
15.05.2017
Сообщения
436
Благодарностей
104
Баллы
43
Try insert your code above without 'function decompress (encoded, precision) {' and '}' in Zenno JS action (run on local window) and set up the variables before this code:
[CODE = javascript] var encoded = {-Variable.encoded-};
var precision = {-Variable.precision -}; [/ CODE]
Thank you for answer, but for some reason it still doesn't work, here in this example I am using the snippet wihout the variables at all (with actuall values) and result is still empty :(
JavaScript:
var encoded = 'yhdoIo~xiCk@?gA?aA?c@@gAB';
var precision = 5;

   precision = Math.pow (10, -precision);
   var len = encoded.length, index = 0, lat = 0, lng = 0, array = [];
   while (index <len) {
      var b, shift = 0, result = 0;
      do {
         b = encoded.charCodeAt (index ++) - 63;
         result | = (b & 0x1f) << shift;
         shift + = 5;
      } while (b> = 0x20);
      var dlat = ((result & 1)? ~ (result >> 1): (result >> 1));
      lat + = dlat;
      shift = 0;
      result = 0;
      do {
         b = encoded.charCodeAt (index ++) - 63;
         result | = (b & 0x1f) << shift;
         shift + = 5;
      } while (b> = 0x20);
      var dlng = ((result & 1)? ~ (result >> 1): (result >> 1));
      lng + = dlng;
      array.push (lat * precision);
      array.push (lng * precision);
   }
   return array;
 

myndeswx

Client
Регистрация
15.05.2017
Сообщения
436
Благодарностей
104
Баллы
43
Code in template -
 

Вложения

BAZAg

Client
Регистрация
08.11.2015
Сообщения
1 788
Благодарностей
2 453
Баллы
113
Hello, maybe a little stupid question, but how to run such javascript function in zenno? Never really needed to do so, but using external services for this job is not suitable for the job. Thank you!

JavaScript:
 function decompress (encoded, precision) {
   precision = Math.pow (10, -precision);
   var len = encoded.length, index = 0, lat = 0, lng = 0, array = [];
   while (index <len) {
      var b, shift = 0, result = 0;
      do {
         b = encoded.charCodeAt (index ++) - 63;
         result | = (b & 0x1f) << shift;
         shift + = 5;
      } while (b> = 0x20);
      var dlat = ((result & 1)? ~ (result >> 1): (result >> 1));
      lat + = dlat;
      shift = 0;
      result = 0;
      do {
         b = encoded.charCodeAt (index ++) - 63;
         result | = (b & 0x1f) << shift;
         shift + = 5;
      } while (b> = 0x20);
      var dlng = ((result & 1)? ~ (result >> 1): (result >> 1));
      lng + = dlng;
      array.push (lat * precision);
      array.push (lng * precision);
   }
   return array;
}
No need to look for how to run the script.
You need to look for how to do in C # what is done in JavaScript. It is likely that compression and decompression is already built into the C # language and can be used in ZennoPoster without any problems.
But in order to find what you need, you need to say what data you submit to the input, and what data you want to receive at the output. And also what exactly is happening (suddenly there is some additional information that will help solve the problem easier than a way to rewrite the code in C #).

P.S. Google Translater...
 
  • Спасибо
Реакции: myndeswx

Astraport

Client
Регистрация
01.05.2015
Сообщения
4 983
Благодарностей
4 433
Баллы
113
Thank you for answer, but for some reason it still doesn't work, here in this example I am using the snippet wihout the variables at all (with actuall values) and result is still empty :(
JavaScript:
var encoded = 'yhdoIo~xiCk@?gA?aA?c@@gAB';
var precision = 5;

   precision = Math.pow (10, -precision);
   var len = encoded.length, index = 0, lat = 0, lng = 0, array = [];
   while (index <len) {
      var b, shift = 0, result = 0;
      do {
         b = encoded.charCodeAt (index ++) - 63;
         result | = (b & 0x1f) << shift;
         shift + = 5;
      } while (b> = 0x20);
      var dlat = ((result & 1)? ~ (result >> 1): (result >> 1));
      lat + = dlat;
      shift = 0;
      result = 0;
      do {
         b = encoded.charCodeAt (index ++) - 63;
         result | = (b & 0x1f) << shift;
         shift + = 5;
      } while (b> = 0x20);
      var dlng = ((result & 1)? ~ (result >> 1): (result >> 1));
      lng + = dlng;
      array.push (lat * precision);
      array.push (lng * precision);
   }
   return array;
Your code is not valid. Test it on this page https://esprima.org/demo/validate.html
 
  • Спасибо
Реакции: myndeswx

myndeswx

Client
Регистрация
15.05.2017
Сообщения
436
Благодарностей
104
Баллы
43
No need to look for how to run the script.
You need to look for how to do in C # what is done in JavaScript. It is likely that compression and decompression is already built into the C # language and can be used in ZennoPoster without any problems.
But in order to find what you need, you need to say what data you submit to the input, and what data you want to receive at the output. And also what exactly is happening (suddenly there is some additional information that will help solve the problem easier than a way to rewrite the code in C #).

PS Google Translater ...
Thank you both for the replies, I have used this snippet from here - https://developer.mapquest.com/documentation/common/encode-decode/
It's quite specific thing, so I don't think it's implemented in zp ..
This function is used to decode the compressed "polylines", that are returned from google maps api,
google returns such compressed values - _k~nE`s{pUk@zCWjAu@~DMn@G\K`@
and we can decompress them in this page ( https://developer.mapquest.com/documentation/common/encode-decode/ )
after decompression we get a list of coordinates. Will try to look for a C # snippet for that: |
 

myndeswx

Client
Регистрация
15.05.2017
Сообщения
436
Благодарностей
104
Баллы
43
We are moving somewhere...
In the original JS code
JavaScript:
return array;
is used, but zenno doesn't like it.
BUT, we can see that coordinates are being generated and stored in to the array, we can access them from array[0], array[1] etc.
now only need to find a way how to merge this array to the variable
Tried like this, but doesn't work.
JavaScript:
array.forEach((coord) => {
    return(coord);
});
87178
 

myndeswx

Client
Регистрация
15.05.2017
Сообщения
436
Благодарностей
104
Баллы
43
Moving forward, using Console.log we can send this array to our instances console, but how to save this to a variable instead :an:

87179
 

myndeswx

Client
Регистрация
15.05.2017
Сообщения
436
Благодарностей
104
Баллы
43
URA!
we don't need to use any loops .. Instead of return array;
we use -

return JSON.stringify(array);

:ar:
 

Кто просматривает тему: (Всего: 2, Пользователи: 0, Гости: 2)