Search code examples
javascriptwebgoogle-maps-api-3kml

How to make a URL with javascript variable?


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.


Solution

  • 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}`