Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
mapbox passing coordinates as variable does not work

It tried to implement the solution to draw a polygon from external data as shown in https://jsfiddle.net/zxaktouy/1/ but I get the error: Input data given to 'frag15' is not a valid GeoJSON object.

My JS-method:

    drawFragment : function(pFRAGMENT) {
        const wPolygon =  pFRAGMENT.coordinates;
        console.log("drawFragment: Coords="+wPolygon);
        wSourceId = "frag"+pFRAGMENT.id;
        wFillId = "fragfill"+pFRAGMENT.id;
        wOutlineId = "fragoutline"+pFRAGMENT.id;
        map.addSource(wSourceId,{
          'type': 'geojson',
          'data': {
            'type': 'Feature',
            'geometry': {
            'type': 'Polygon',
            'coordinates': [ 
wPolygon
 ]
               }
             }
        });
        // Add a new layer to visualize the polygon.
        map.addLayer({
          'id': wFillId,
          'type': 'fill',
          'source': wSourceId, // reference the data source
          'layout': {},
          'paint': {
            'fill-color': '#00ff80', // green color fill
            'fill-opacity': 0.5
          }
        });
        // Add a black outline around the polygon.
        map.addLayer({
          'id': wOutlineId,
          'type': 'line',
          'source': wSourceId,
          'layout': {},
          'paint': {
          'line-color': '#0d0',
          'line-width': 2
          }
        });
    },

And the data passed (perfectly shown via console.log) look like this:

[[8.543590974130666,47.377830192117756],
[8.543641551219707,47.37784384335191],
[8.543634914341965,47.37789513281288],
[8.543582309906242,47.37791046432616],
[8.543590974130666,47.377830192117756]]

when I replace the "wPolygon" just by copy-pasting the data into the code everything works fine.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

After replacing:

const wPolygon =  pFRAGMENT.coordinates;

with

var wPolygon = JSON.parse(pFRAGMENT.coordinates);

it works.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!