本文介绍了计算angularjs中两个坐标之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js"></script>
<script src="ng-haversine.min.js"></script>
<script src="package.json"></script>
</head>
<body ng-app="myNgApp">
<h1>AngularJS $scope Demo: </h1>
<div ng-controller="myController">
{{distance}}
</div>
<script>
var ngApp = angular.module("myNgApp",["dahr.ng-haversine"]);
ngApp.controller('myController', function ($scope) {
var coord1 = {
"latitude": 40.4169473,
"longitude": -3.7057172
};
var coord2 = {
"latitude": 40.4236942,
"longitude": -3.7109793
};
var distance = $haversine.distance(coord1, coord2);
$scope.distance=distance ;
});
</script>
</body>
我的尝试:
我试过使用ng-haversine模块。下面是ng-haversine的脚本。
What I have tried:
I tried by using ng-haversine module .below is the script of ng-haversine.
// Code goes here
/**
* @name ng-haversine
* @version 0.0.1
* @license MIT
* @author Diego Herrera
* @description ng-haversine is an AngularJS module that applies the Haversine formula to a pair of coordinates to calculate the distance between them.
*/
!function(t){"use strict";t&&t.angular?angular.module("dahr.ng-haversine",[]).service("$haversine",function(){var t=6378137,a=function(t){return t*Math.PI/180};this.distance=function(n,e){var r=a(e.latitude-n.latitude),i=a(e.longitude-n.longitude),o=Math.pow(Math.sin(r/2),2)+Math.cos(a(n.latitude))*Math.cos(a(e.latitude))*Math.pow(Math.sin(i/2),2),s=2*Math.atan2(Math.sqrt(o),Math.sqrt(1-o));return t*s}}):t.console.error("ng-haversine: AngularJS must be loaded first. More info at https://angularjs.org/.")}(window);
推荐答案
这篇关于计算angularjs中两个坐标之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!