admin管理员组

文章数量:1404353

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 = '/?= + 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.

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./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.

Share Improve this question edited Aug 15, 2017 at 10:39 geocodezip 161k14 gold badges226 silver badges254 bronze badges asked Aug 15, 2017 at 8:56 Farhan HabibFarhan Habib 1534 silver badges13 bronze badges 5
  • How to insert javascript variables into a URL : stackoverflow./questions/17734343/… – Jixone Commented Aug 15, 2017 at 8:58
  • can you print what's their in kml ? – marvel308 Commented Aug 15, 2017 at 8:58
  • Possible duplicate of How to insert javascript variables into a URL – DarthJDG 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 bee "example./KML/?=" + kml; but this doesnt work as well . – Farhan Habib Commented Aug 15, 2017 at 9:02
  • The names stored in the kml files are like this "T12_Zentral_Schweiz.kml" . – Farhan Habib Commented Aug 15, 2017 at 9:10
Add a ment  | 

1 Answer 1

Reset to default 5

You missed it.

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

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

var src = `http://www.example./KML/?=${kml}`

本文标签: webHow to make a URL with javascript variableStack Overflow