Home > WeChat Applet > Mini Program Development > Mini program example: How to calculate the distance between two points based on longitude and latitude (code)

Mini program example: How to calculate the distance between two points based on longitude and latitude (code)

不言
Release: 2018-08-16 17:12:39
Original
7085 people have browsed it

This article brings you an example of a small program: how to calculate the distance between two points based on longitude and latitude (code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. helped.

//计算两点位置距离
  getDistance: function (lat1, lng1, lat2, lng2) {
    lat1 = lat1 || 0;
    lng1 = lng1 || 0;
    lat2 = lat2 || 0;
    lng2 = lng2 || 0;    
    var rad1 = lat1 * Math.PI / 180.0;    
    var rad2 = lat2 * Math.PI / 180.0;    
    var a = rad1 - rad2;    
    var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;    
    var r = 6378137;  //地球半径
    var distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)));    
    /*
    if (distance > 1000){
      distance = Math.round(distance / 1000);
    }*/

    return distance;
  }
Copy after login

Related recommendations:

PHP calculates the distance between two GPS points

php calculates two longitude and latitude locations the distance between

The above is the detailed content of Mini program example: How to calculate the distance between two points based on longitude and latitude (code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template