0

I am trying to pass a Javascript variable into a URL, but there is some sort of syntax error within it.

function initMap() {
  var jsonData = { $tourArray }
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 4,
    center: {
      lat: jsonData.CenterLat,
      lng: jsonData.CenterLon,
    },
  })

  var kml = jsonData.KmlFile
  var src = 'http://www.example.com/KML/?= + kml'

  var kmlLayer = new google.maps.KmlLayer(src, {
    map: map,
  })
}

The variable var kml is basically the kml file name which is stored on the server within the folder KML and I am trying to access that file in order to add a KML layer on my google map.

The URL syntax seems to be incorrect.

geocodezip
  • 158,664
  • 13
  • 220
  • 245
Farhan Habib
  • 153
  • 3
  • 13
  • How to insert javascript variables into a URL : https://stackoverflow.com/questions/17734343/how-to-insert-javascript-variables-into-a-url – Jixone Aug 15 '17 at 08:58
  • can you print what's their in kml ? – marvel308 Aug 15 '17 at 08:58
  • Possible duplicate of [How to insert javascript variables into a URL](https://stackoverflow.com/questions/17734343/how-to-insert-javascript-variables-into-a-url) – DarthJDG Aug 15 '17 at 08:59
  • I had checked that question before, but the solution doesnt work in my case, According to that my url should become "http://www.example.com/KML/?=" + kml; but this doesnt work as well . – Farhan Habib Aug 15 '17 at 09:02
  • The names stored in the kml files are like this "T12_Zentral_Schweiz.kml" . – Farhan Habib Aug 15 '17 at 09:10

1 Answers1

5

You missed it.

var src = "http://www.example.com/KML/?=" + kml;

If you're using ES6, then template literals makes it even cleaner.

var src = `http://www.example.com/KML/?=${kml}`
Rowland
  • 1,728
  • 1
  • 12
  • 19