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.

5
  • How to insert javascript variables into a URL : stackoverflow.com/questions/17734343/… Commented Aug 15, 2017 at 8:58
  • can you print what's their in kml ? Commented Aug 15, 2017 at 8:58
  • Possible duplicate of How to insert javascript variables into a URL Commented Aug 15, 2017 at 8:59
  • I had checked that question before, but the solution doesnt work in my case, According to that my url should become "example.com/KML/?=" + kml; but this doesnt work as well . Commented Aug 15, 2017 at 9:02
  • The names stored in the kml files are like this "T12_Zentral_Schweiz.kml" . Commented Aug 15, 2017 at 9:10

1 Answer 1

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}`
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.