我得到了这个非常奇怪的错误
TypeError: undefined is not a function
at h.$scope.getCoordDistance (http://run.plnkr.co/NDFxGL6q55m1601d/script.js:32464:21)
at http://run.plnkr.co/NDFxGL6q55m1601d/script.js:32491:14
at Array.forEach (native)
at Object.q [as forEach] (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:7:280)
at new <anonymous> (http://run.plnkr.co/NDFxGL6q55m1601d/script.js:32480:11)
at d (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:34:479)
at Object.instantiate (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:35:103)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:66:467
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:53:250
at q (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:7:386)
在网上搜索大约一个半小时后,我完全不知道这意味着什么。有谁知道这意味着什么?这是我的代码:
var app = angular.module('app', []);
app.controller('firstCtrl', function($scope) {
var myLat,myLon,locLat,locLon;
$scope.ASiteLocs = [{
"name": "IL5077 BRUSSELS",
"styleUrl": "#waypoint",
"Point": {
"coordinates": "-90.58543899999999,38.955472,0"
}
}, {
"name": "IL5076 KAMPSVILLE",
"styleUrl": "#waypoint",
"Point": {
"coordinates": "-90.661923,39.29403,0"
}
}, {
"name": "IL5146 CARROLLTON",
"styleUrl": "#waypoint",
"Point": {
"coordinates": "-90.39965700000001,39.309142,0"
}
}];//there are a couple thousand of these
$scope.SSiteLocs = [/*More Locations*/];// but its too long
$scope.SiteLocs = $scope.SSiteLocs.concat($scope.ASiteLocs);
repoSortOrder = "site.name";
navigator.geolocation.getCurrentPosition(GetLocation);
function GetLocation(location, myLat, myLon) {
$scope.myLat = location.coords.latitude;
$scope.myLon = location.coords.longitude;
} $scope.getCoordDistance = function(myLat, myLon,locLat, locLon) {
var lat2 = $scope.myLat;
var lon2 = $scope.myLon;
var lat1 = $scope.locLat;
var lon1 = $scope.locLon;
var R = 3959; // Mean Earth radius in miles
var x1 = lat2 - lat1;
var dLat = x1.toRad();
var x2 = lon2 - lon1;
var dLon = x2.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));
$scope.d = R * c;
}
Number.prototype.toRad = function() {
return this * Math.PI / 180;
}
angular.forEach($scope.SSiteLocs, function(object) {
object.carrier = 'Sprint';
})
angular.forEach($scope.ASiteLocs, function(object) {
object.carrier = 'AT&T';
});
angular.forEach($scope.SiteLocs, function(location, locLat, locLon) {
var clength = location.Point.coordinates.length;
if (location.Point.coordinates.substring(clength - 2, clength) === ",0") {
location.Point.coordinates = location.Point.coordinates.substring(0, clength - 2).split(",");
Lat = location.Point.coordinates[0];
Lon = location.Point.coordinates[1];
Com = ",";
location.Point.coordinates = Lon.concat(Com, Lat);
}
$scope.locLat = location.Point.coordinates[0];
$scope.locLon = location.Point.coordinates[1];
$scope.getCoordDistance();
location.distance = d;
});
});
如果我尝试使用myLat, myLon, locLat, locLon
解析parseInt();
,我会收到此错误:
TypeError: Cannot read property 'toRad' of undefined
at h.$scope.getCoordDistance (http://run.plnkr.co/NDFxGL6q55m1601d/script.js:46:45)
at http://run.plnkr.co/NDFxGL6q55m1601d/script.js:73:14
at Array.forEach (native)
at Object.q [as forEach] (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:7:280)
at new <anonymous> (http://run.plnkr.co/NDFxGL6q55m1601d/script.js:62:11)
at d (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:34:479)
at Object.instantiate (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:35:103)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:66:467
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:53:250
at q (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:7:386)
任何帮助都非常感谢!!!
这是项目的一部分:http://plnkr.co/edit/nRQc7Ym0lsaK6jQwd626?p=preview
答案 0 :(得分:1)
在你从getCoordDistance()
获取坐标(btw是异步调用)之前,你正在运行navigator.geolocation.getCurrentPosition()
。
这意味着$scope.myLat
和$scope.myLon
都是 null / undefined ,这会导致错误&#34;无法读取属性&#39; toRad&#39;未定义&#34;。
选中此plunker