Leafletjs - 数组中存在传单点
Leafletjs - Presence of a leaflet point in a array
我有一个传单点(L.latLng),我想知道传单点数组中是否出现了它。
o.LatLng {lat: 48.8589507, lng: 2.2775168}
我尝试使用 JQuery ($.inArray) 或 underscorejs (_.intersection) 但我不知道如何同时比较纬度和经度。
谢谢!
您可能正在寻找 .equals()
method 个对象,共 L.LatLng
个对象:
Returns true
if the given LatLng point is at the same position (within a small margin of error).
使用下划线查找功能。
_.find(arrayOfPoints, function(point){
return point.lat === myLat && point.lon === myLon;
});
正如@ghybs 指出的那样,.equals
can compare two instances of L.LatLng
. In order to check if an equal instance exists in an array, you'll need to use Array.find
或遍历项目:
latLngToBeFound = L.latLng( 48, 2 );
latLngArray = [
L.latlng( 58, 9 ),
L.latlng( 32, 14 ),
L.latlng( 48, 2 ),
L.latlng( 0, 15 )
];
var exists = !!latLngArray.find( function(i){
return latLngToBeFound.equals(i);
} );
我有一个传单点(L.latLng),我想知道传单点数组中是否出现了它。
o.LatLng {lat: 48.8589507, lng: 2.2775168}
我尝试使用 JQuery ($.inArray) 或 underscorejs (_.intersection) 但我不知道如何同时比较纬度和经度。
谢谢!
您可能正在寻找 .equals()
method 个对象,共 L.LatLng
个对象:
Returns
true
if the given LatLng point is at the same position (within a small margin of error).
使用下划线查找功能。
_.find(arrayOfPoints, function(point){
return point.lat === myLat && point.lon === myLon;
});
正如@ghybs 指出的那样,.equals
can compare two instances of L.LatLng
. In order to check if an equal instance exists in an array, you'll need to use Array.find
或遍历项目:
latLngToBeFound = L.latLng( 48, 2 );
latLngArray = [
L.latlng( 58, 9 ),
L.latlng( 32, 14 ),
L.latlng( 48, 2 ),
L.latlng( 0, 15 )
];
var exists = !!latLngArray.find( function(i){
return latLngToBeFound.equals(i);
} );