javascript 离子:网络工作者
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 离子:网络工作者相关的知识,希望对你有一定的参考价值。
readonly myWorker: Worker = new Worker('./assets/workers/shops.js');
this.allService.myWorker.onmessage = (event) => {
this.ngZone.run(() => {
this.allService.subShopItems = event.data.filteredList;
console.timeEnd('shopsWorker 1: ');
console.log('shopsWorker 2: ', this.allService.subShopItems);
})
};
this.allService.myWorker.postMessage({
command : 'doFuzzySearch',
sub_categories: this.rootCategories2[5].sub_categories,
radius : this.radius,
lat2 : this.lat2,
long2 : this.long2,
});
///////
///////
'use strict';
//import * as nativegeocoder from "../../../plugins/cordova-plugin-nativegeocoder/www/NativeGeocoder";
//self.importScripts("../../../node_modules/@ionic-native/native-geocoder/index.js");
var sub_categories,
radius,
lat2,
long2;
self.onmessage = function (event){
// Receive a message and answer accordingly
sub_categories = event.data.sub_categories;
radius = event.data.radius;
lat2 = event.data.lat2;
long2 = event.data.long2;
self.doFuzzySearch();
};
self.doFuzzySearch = function (){
var filteredList = [];
console.time('shopsWorker: internalFastFilter');
for (var n = 0; n < sub_categories.length; n++) {
//console.log('shopsWorker: subShopItems 0: ', n, sub_categories.length, sub_categories, this.radius);
filteredList[n] = sub_categories[n];
if (sub_categories[n] && sub_categories[n].sub_categories && sub_categories[n].sub_categories.length > 0) {
for (var i = 0; i < sub_categories[n].sub_categories.length; i++) {
//console.log('shopsWorker: subShopItems 1: ', i, self.radius, sub_categories[n].sub_categories[i].latitude,
// sub_categories[n].sub_categories[i].longitude, self.lat2, self.long2);
if (sub_categories[n].sub_categories[i].latitude && sub_categories[n].sub_categories[i].latitude.length > 0 && sub_categories[n].sub_categories[i].longitude && sub_categories[n].sub_categories[i].longitude.length > 0) {
let km = self.getDistanceFromLatLonInKm(sub_categories[n].sub_categories[i].latitude, sub_categories[n].sub_categories[i].longitude, self.lat2, self.long2);
//console.log('shopsWorker: km: ', i, km);
if (km < self.radius) {
console.log('shopsWorker: TRUE: ', n, i, km, self.radius, sub_categories[n].sub_categories[i].latitude, sub_categories[n].sub_categories[i].longitude, self.lat2, self.long2, filteredList.length, filteredList);
filteredList[n].sub_categories[i].shops = true;
} else {
filteredList[n].sub_categories[i].shops = false;
}
console.log('shopsWorker: subShopItems 3: ', n, i, km, self.radius, sub_categories[n].sub_categories[i], filteredList.length, filteredList);
//console.log('calltocal 2: result: ', i, result);
/*var xhr = new XMLHttpRequest();
xhr.open("GET", "https://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false&key=AIzaSyCXy_YDP0b8hbEbPHAwfAF_8wu7Ri_75Lo®ion=Malaysia");
xhr.onload = function (){
if (this.status == 200 && xhr.responseText && JSON.parse(xhr.responseText) && JSON.parse(xhr.responseText).results.length > 0) {
var coordinates = JSON.parse(xhr.responseText).results[0].geometry.location;
console.log('shopsWorker xhr: ', i, coordinates);
sub_categories[n].sub_categories[i].lat = coordinates.lat;
sub_categories[n].sub_categories[i].long = coordinates.lng;
} else {
//send some general error
console.error('shopsWorker xhr: ', i);
}
}
xhr.send();*/
/*this.nativeGeocoder.forwardGeocode(sub_categories[n].sub_categories[i].address).then((coordinates : NativeGeocoderForwardResult[]) => {
sub_categories[n].sub_categories[i].lat = coordinates[0].latitude;
sub_categories[n].sub_categories[i].long = coordinates[0].longitude;
console.log('subShopItems 2: ', i, sub_categories[n].sub_categories[i].lat, sub_categories[n].sub_categories[i].long, this.lat2, this.long2);
var km = this.getDistanceFromLatLonInKm(sub_categories[n].sub_categories[i].lat, sub_categories[n].sub_categories[i].long, this.lat2, this.long2);
//console.log('calltocal 4: km: ', i, km);
if (km < this.radius) {
filteredList[n].sub_categories[i].shops = true;
console.log('subShopItems 3: ', n, i, sub_categories[n].sub_categories[i], filteredList.length, filteredList, km);
}
})*/
} else {
filteredList[n].sub_categories[i].shops = false;
}
}
}
}
console.timeEnd('shopsWorker: internalFastFilter');
// Send the results back
postMessage({filteredList : filteredList});
};
self.getDistanceFromLatLonInKm = function (lat1, lon1, lat2, lon2){
var R = 6371; // Radius of the earth in km
var dLat = self.deg2rad(lat2 - lat1); // deg2rad below
var dLon = self.deg2rad(lon2 - lon1);
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(self.deg2rad(lat1)) * Math.cos(self.deg2rad(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c; // Distance in km
return d;
}
self.deg2rad = function (deg){
return deg * (Math.PI / 180)
}
以上是关于javascript 离子:网络工作者的主要内容,如果未能解决你的问题,请参考以下文章