开发者

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;
       }


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜