GeoLocation - Forum

Forum Navigation
You need to log in to create posts and topics.

GeoLocation

How can I convert the below script code to javascript subroutine?

https://tinyurl.com/y2lxz5r8

https://tinyurl.com/y4v96f97

 

Hi @asmat,
Please take a look here to understand how to create a subroutine or a plugin from JavaScript function:
https://visualneo.com/forum/topic/plugin-generator-and-subroutines-sample

Samuel Vanneste has reacted to this post.
Samuel Vanneste

I have made a simple plugin but it does not work look at following link:

https://tinyurl.com/yy9lfftn

Please mention my mistakes.

I'm answering from my phone so I can't test it, but the problem is you are returning the location value to nowhere.

Will get back to this as soon as I can but you are really close.

@asmat, take a look at the attached documents (plugin and sample App).
This is a special case because you have choosen a JavaScript function that calls another one and we need some way to preserve the parameters between functions. There is probably a better solution than mine, but it works.

Uploaded files:
  • You need to login to have access to uploads.
clueadventures has reacted to this post.
clueadventures

Great plugin - do you know if anyone has created a 'haversine' plug to calculated distance between two geo locations?

I have the javascript code, just not overly sure how I'd turn it into a plug in:

 

function distance(lon1, lat1, lon2, lat2) {
  var R = 6371; // Radius of the earth in km
  var dLat = (lat2-lat1).toRad();  // Javascript functions in radians
  var dLon = (lon2-lon1).toRad(); 
  var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
          Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
          Math.sin(dLon/2) * Math.sin(dLon/2); 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var d = R * c; // Distance in km
  return d;
}

/** Converts numeric degrees to radians */
if (typeof(Number.prototype.toRad) === "undefined") {
  Number.prototype.toRad = function() {
    return this * Math.PI / 180;
  }
}

@cluadventures:

I have the javascript code, just not overly sure how I'd turn it into a plug in:

Do you have a special requirement to invoke this functionality as a plugin ? ... note that VisualNEOWeb subroutines can be made with VisualNEOWeb commands or Javascript commands ... the code you posted can be setup as a Javascript subroutine (with 4 input and 1 output parameters) quite easily.

clueadventures has reacted to this post.
clueadventures

Thanks, yes I guess so. The help file seems to be somewhat lacking in how to set up subroutines, I'll see what I can find, thank you.

Has anyone had any luck combining this feature with a map?

Quote from luishp on April 20, 2019, 8:26 pm

@asmat, take a look at the attached documents (plugin and sample App).
This is a special case because you have choosen a JavaScript function that calls another one and we need some way to preserve the parameters between functions. There is probably a better solution than mine, but it works.

Thank you so much...

@clueadventures:

The help file seems to be somewhat lacking in how to set up subroutines

Project >>> Subroutines... (F12) >>> New Subroutine >>> New Javascript Subroutine >>> type name of subroutine (say myCalcDistance)

Since VisualNEOWeb subroutines can accept parameters, you use the section at the bottom right to define them ...

- Alias ... the variable you will use inside the subroutine to refer to the first parameter value
- Type ... defines the type of variable (string, integer etc.)
- Description ... optional but helpful when using the command wizard to compose the calling command

- Hint ... optional but helpful when using the command wizard to compose the calling command

Since you will be invoking a subroutine like ...

myCalcDistance "[requestedLatitude1]" "[requestedLongitude1]" "[requestedLatitude2]" "[requestedLongitude2]" "[resultingDistance]"

... you will need 5 parameters e.g. ...

thisLat1 ... type FLOAT
thisLon1 ... type FLOAT
thisLat2 ... type FLOAT
thisLon2 ... type FLOAT
thisDistance ... type VARNAME

Now, you add (javascript) commands just like with any other "event section code" (but without the wrapping BeginJS/EndJS) ... use javascript variables thisLat1, thisLat2 etc. inside the subroutine commands to reference the values passed through parameters.

Updated later ... the way to populate the result variable is ...

$App[thisDistance] = blah blah blah;

 

Hope this helps. If you need help with the Javascript commands, let us know.

clueadventures has reacted to this post.
clueadventures

Great Thank you, I'll have a go