如何解决如何使用要基于其创建的自定义库中的传单中提供的distanceTo方法?
我在项目中使用传单。现在有一种 distanceTo 方法,该方法基本上可以计算两个坐标之间的距离。现在,我想创建一个单独的JS文件,该文件将具有一个名为getDistance()的函数,并且我想保留分别计算坐标之间距离的逻辑。这是代码
FILE a.js
function getDistance() {
var getDistanceCal = (L.Layer ? L.Layer : L.Class).extend({
calculateDistance: function (latA,latB) {
if (latA !== undefined && latB !== undefined) {
//How can I make it run distanceTo method here where leaflet Js is being called in another file
let dis = latA.distanceTo(latB);
let distanceConversion = ((dis) / 1000).toFixed(0);
let distanceKm = distanceConversion;
return distanceKm || 0;
}
else {
return 0;
}
}
});
L.markerDistance = function () {
return new getDistanceCal();
};
return L;}
在另一个名为leaflet.js的文件中,我称它为:
FILE b.js
CALLING THE FUNCTION
markerDistanceFunction().markerDistance().calculateDistance(1.3521,103.81) // should give distance but throws an error that distanceTo is not defined
我正在尝试扩展传单,但是出了点问题。有人可以看一下,让我知道如何使它工作。
一些链接:
任何帮助将不胜感激。谢谢大家!
解决方法
我不明白你为什么要这么复杂。
我建议创建一个普通函数而不是扩展一个类。
function calculateDistance(latA,latB) {
if (latA !== undefined && latB !== undefined) {
//How can I make it run distanceTo method here where leaflet Js is being called in another file
let dis = latA.distanceTo(latB);
let distanceConversion = ((dis) / 1000).toFixed(0);
let distanceKm = distanceConversion;
return distanceKm || 0;
}
else {
return 0;
}
}
distance = calculateDistance(latlng,latlng);
但是要回答您的问题,问题是distanceTo
仅适用于L.LatLng
类,不适用于普通数。
markerDistanceFunction().markerDistance().calculateDistance(L.latLng(1.3521,103.81),latlngFromMarker)
当您想覆盖默认的distanceTo
函数src时:
L.LatLng.prototype.distanceTo = function (other) {
return (L.CRS.Earth.distance(this,L.latLng(other)) / 1000).toFixed(0);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。