javascript - Looping through coordinates in Mapbox -
i'm pretty new coding code may wrong. i'm using flyto feature in mapbox fly city city. want fly multiple cities clicking on button. i've created array coordinates want fly to, code doesn't seem working. can me went wrong? thanks! here's page on how use feature: https://www.mapbox.com/mapbox-gl-js/example/flyto/ .
var arrayofcoords = [[-73.554,45.5088], [-73.9808,40.7648], [-117.1628,32.7174], [7.2661,43.7031], [11.374478, 43.846144], [12.631267, 41.85256], [12.3309, 45.4389], [21.9885, 50.0054]]; document.getelementbyid('fly').addeventlistener('click', function () { if(i = 0; < arrayofcoords.length; i++) { map.flyto(arrayofcoords[i]); }); });
welcome stackoverflow!
here's how "fly" 1 coordinate next coordinate on each button click. note when reached last coordinate, next button click bring first one.
mapboxgl.accesstoken = '<your access token here>'; var map = new mapboxgl.map({ container: 'map', style: 'mapbox://styles/mapbox/streets-v9', center: [-74.50, 40], zoom: 9 }); // variable track current coordinate array's index. var idx = 0; var arrayofcoordinates = [ [-73.554,45.5088], [-73.9808,40.7648], [-117.1628,32.7174], [7.2661,43.7031], [11.374478, 43.846144], [12.631267, 41.85256], [12.3309, 45.4389], [21.9885, 50.0054] ]; document.getelementbyid('fly').addeventlistener('click', function () { // first coordinate. if (idx >= arrayofcoordinates.length) { idx = 0; } map.flyto({ center: arrayofcoordinates[idx] }); idx++; });
hope help!
Comments
Post a Comment