Nativescript-mapbox plugin setOnScrollListener


#1

I’m new to both javascript and nativescript. After going through tuts and examples for the last week I started a new project.

I want to display a map that dynamically gets locations from a external api and displays them on the users map, and updates each time the user scrolls the map to a new location.

I think with mapbox I want to use the setOnScrollListener to call my onScroll function where I would update the map with new markers.

function onScroll() {
    // eventually update map here with markers from api call
    console.log("onScroll yeah....");
}

function onLoad(args) {
    var page = args.object;
    var mapboxView = new mapboxModule.MapboxView();

    mapboxView.accessToken = "my key";
    mapboxView.mapStyle = "outdoors";
    mapboxView.latitude = 46.51;
    mapboxView.longitude = -114.09;
    mapboxView.zoomLevel = 8;
    mapboxView.showUserLocation = false;

    mapboxView.setOnScrollListener(onScroll, this);


    var stackLayout = page.getViewById("mainMapView");  // Get ref to StackLayout
    stackLayout.addChild(mapboxView);    // Add map to StackLayout
}

exports.onLoad = onLoad;

This is just my attempt at getting a working sample going. I expect that when the map is scrolled I would get “onScroll yeah…” printed to the console each time.

Right now I just get an error:
System.err: TypeError: Cannot read property ‘setOnScrollListener’ of undefined

If I comment out mapboxView.setOnScrollListener(onScroll, this); it will run fine, but of course I don’t get my onScroll function triggered when scrolling.

mapbox.android.js has this line:
Mapbox.prototype.setOnScrollListener = function (listener, nativeMap)…

So I think listener should be my function onScroll and nativeMap should be either this or mapboxView, but I cant get them to work.

How am I supposed to use mapboxView.setOnScrollListener()? I’ve tried passing in many different combinations, but I always get similar errors. I’m stuck trying to figure out what I need to pass in.


#2

I figured out that on the onLoad() the mapboxView.setOnScrollListener() needs to be wrapped in a setTimeout() function to add in a delay to allow for the mapbox to initialize.

It’s working now.