Reputation: 3
having some issues with the HERE maps api outputting the data i'm requesting too many times. I need it to output only once every 10 seconds, however it is doing it far more times than that.
The code I have here is as follows:
function getMapCorners() {
var mapView = map.getViewModel();
var mapCorners = mapView.b.bounds.cb.X;
var corners = [mapCorners[9], mapCorners[10], mapCorners[3], mapCorners[4]];
console.log(corners);
}
map.addEventListener("mapviewchange", function () {
setTimeout(getMapCorners, 10000);
});
I need to grab these specific co-ordinates as this is feeding into another API that is creating markers on the map in specific areas. When zooming in, it does wait 10 seconds to run the function, but then it throws out the co-ordinate changes for every change in the map's view model during that 10 seconds.
The other API has a request limit of 100 every 15 minutes. So I need this to change so that it will run the function every 10 seconds, and it will only output the information for the final viewModel when the function runs.
Upvotes: 0
Views: 209
Reputation:
First, please don't use internal name of objects like .b.bounds.cb.X - b, cb, X are defined by compilation of js library and will be changed for new version of JS API. Please use for it a name of methods and objects described in documentation: https://developer.here.com/documentation/maps/3.1.29.0/api_reference/H.map.ViewModel.html#getLookAtData like:
var mapCorners = map.getViewModel().getLookAtData().bounds.getBoundingBox();
Second, the map event 'mapviewchange' runs multiply times during interacting with map therefore you get multiply times run of function getMapCorners. Suggestion to achieve what you want:
function getMapCorners() {
console.log("getMapCorners!!!", map.getViewModel().getLookAtData().bounds.getBoundingBox());
map.addEventListener("mapviewchange", onMapViewChange);
}
function onMapViewChange() {
map.removeEventListener("mapviewchange", onMapViewChange);
setTimeout(getMapCorners, 10000);
}
See please full code there on https://jsfiddle.net/1m8fvLjy/1/
Upvotes: 0
Reputation: 1078
If you want to run getMapCorners once every 10 seconds, then do this:
function doEveryTenSeconds(){
getMapCorners();
setTimeout(doEveryTenSeconds, 10000);
}
doEveryTenSeconds();
Upvotes: 0