mahesh
mahesh

Reputation: 3217

How can I perform mathematical operation like cos and sin in Javascript or jQuery?

I have a searchlocationNear function for which i pass geocoded data i need to calculate distance based on lattitude and longitude values but during the alert functions of distance it displays undefined is their any problem with my code.

function searchLocationsNear(center) {
     var radius = document.getElementById('radiusSelect').value;
     lat1=center.lat();
     alert(lat1);
     lng1=center.lng();
     alert(lng1);
     var distance1=( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) ;
     alert(distance1);
     var searchUrl = 'example.xml';
     GDownloadUrl(searchUrl, function(data) {
       var xml = GXml.parse(data);
       var markers = xml.documentElement.getElementsByTagName('marker');
       map.clearOverlays();

       var sidebar = document.getElementById('sidebar');
       sidebar.innerHTML = '';
       if (markers.length == 0) {
         sidebar.innerHTML = 'No results found.';
         map.setCenter(new GLatLng(40, -100), 4);
         return;
       }

Upvotes: 1

Views: 579

Answers (3)

Robusto
Robusto

Reputation: 31883

Use the Math object: Math.sin() and Math.cos()

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1038820

You could take a look at the Math object which contains among others trigonometric functions.

Upvotes: 1

Matti Virkkunen
Matti Virkkunen

Reputation: 65126

I don't think that code should display anything, since you're using variables that aren't defined. In JavaScript all the math functions are under the Math object. So use Math.sin et cetera.

Might also be worth checking if Google's Map library already contains something for calculating the distance between two points. It sounds like something they would definitely include.

Upvotes: 3

Related Questions