0

In my flask GIS app, I am passing my web map a list of coordinates from a route in my Python backend, which works as I expect it to. When I print the list it looks like this.

[['53.792614', '-1.789202'], ['53.792561', '-1.789223'], ['53.770554', '-1.751224'], ['53.712614', '-1.746287']] ... 

I am then trying to add this list of coordinates to the map as markers, in my JavaScript. I am following HERE maps documentation - https://developer.here.com/documentation/examples/maps-js/markers/markers-on-the-map - and trying to loop through each coordinate in my list, creating a new marker for each.

// These variables are dependant on the flask route that is completed
    var lat = {{latitude}}     // 53.483837
    var long = {{longitude}}     // -2.225899
    var chargepoints = {{chargepoints}}
    var coords = {{radius_coordinates}}

// REST OF MAP FUNCTION OMITTED DUE TO LENGTH

    // chargepoints = 2 if the chargepoints route is completed
        if (chargepoints = 2) {
    
          // ADD THE CHARGEPOINTS TO THE MAP
          for (var i=0; i<coords.length; i++) {
    
            var chargemarker = new H.map.Marker({lat:coords[i][1], lng: coords[i][0]}); Cmap.addObject(chargemarker);
    
            Cmap.addObject(chargemarker);
        }
        }

However when I do this, the map does not load, and I get the error - Uncaught SyntaxError: expected expression, got '&' - in my console, and I don't know where the problem is. When I look in the Web Inspector, the coords variable looks like this.

var coords = [&#39;53.712614&#39;, &#39;-1.746287&#39;] 
8
  • I think this is going to be incredibly difficult for someone to offer help. Are you getting a line number from the error that could help you narrow down the problem? It could be happening all sorts of places, but a guess is the response you're parsing has something your code doesn't expect.
    – KHibma
    Commented Mar 9, 2021 at 13:44
  • You could start adding a bunch of print( ... ) in your Python code, especially in the loops. It might show the last line and what the values were. Its been awhile since I worked with Flask, I'm not sure if you can inject a console.log(...) and have it actually output.
    – KHibma
    Commented Mar 9, 2021 at 13:51
  • I've edited down my question, the Python route works as expected the problem is in passing the coordinates to my javascript and adding them to my map.
    – Stuey17
    Commented Mar 9, 2021 at 14:48
  • The console usually should show the location in code where the error ocurred. Can you track it down this way? Commented Mar 9, 2021 at 14:55
  • 1
    There ya go. &#39 sounds like its the string conversion issue (stackoverflow.com/questions/29314231/…) - those used to be quotes, but your app is translating them to the escaped values. Your "fix" might be as sample as building up your list with floats. IE. float(device['ChargeDeviceLocation']['Latitude']) when making radius_coordinates in the Python side of things.
    – KHibma
    Commented Mar 9, 2021 at 17:13

1 Answer 1

2

Based on the error and debug message, your Python code is passing a string into the JavaScript / web side of things. This literal string is being escaped, and the escaped characters are being passed into the JavaScript code to put items on the map.

So, in your Python code you have: ['53.792614', '-1.789202']

What JavaScript is passing to the mapping function: [&#39;53.712614&#39;, &#39;-1.746287&#39;]

There are probably a few different places to fix this, but I'd suggest fixing it within Python by converting the strings to doubles. From your Python code, you could do that when you build up list to plot: float(device['ChargeDeviceLocation']['Latitude'])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.