2

Hi I have been playing around with this for a while but no solution yet.

There are three parts to my code, the aim is to create a custom word to insert into a call for a url, so a specific kml file is loaded onto my webmap.

  1. A function to generate a word by clicking a button on my website.(there are 6 other buttons that once clicked create a word)

    var pub = [];
    function myFunction7() { 
    pub.push("pub");
    
  2. Once the words have been created, this step adds the variable to an array. The array will = the words needed to match the url.

    var url = [pub]
    

    So var url = bar if pub button is clicked.

  3. I would like a new variable to = the words in the array that is generated after the user clicks the buttons.

    var name = url
    var src = "https://www.dropbox.com/"+name+".kml?dl=1";
    

The problem comes at step 3. Instead of loading the kml the map disappears and url to the kml file is not read so nothing is loaded. Buttons all work, I have tested up to step 2 by printing the result on the web page.

Thanks for any help!

12
  • it is a map layer but it would be treated the same as any other file you would like to load onto a website Commented Apr 16, 2016 at 13:22
  • 1
    Well if you cast array to string (which you are doing) you will get comma-separated values. Maby try to var name = url.join(''); If you get the desired src the problem must be somewhere else. Commented Apr 16, 2016 at 13:26
  • And the problem would come from the url that is malformed? Have you set name to "cafebar" before? Commented Apr 16, 2016 at 13:29
  • I have another piece of code to remove the commas. It seems that if I have var name = "bar" the layer loads Commented Apr 16, 2016 at 13:30
  • 1
    @Kar98k you asked this same question not even an hour ago, so I've flagged this as a duplicate. Commented Apr 16, 2016 at 15:31

1 Answer 1

1

Did you check your vars and their typeof? eg. using console.log

I think that, because pub is an array, then url is an array, then name is an array, then your src fails... expecting a string.

If you push only 1 string to pub then you could do:

var url = pub.toString();

or get the first element from pub:

var url = pub[0];

Of course you could do that later on, depending on the rest of your code, eg.

var name = url.toString();
var src = "https://www.dropbox.com/"+name.toString()+".kml?dl=1";
Sign up to request clarification or add additional context in comments.

14 Comments

Thank you! This works in creating the correct url (as shown by the console log) but the file is still not loaded automatically onto the map. What does the 0 do in the var url = pub[0]?
it gets the first element from pub array... which is the string "pub" you pushed into it
how did you use the src string?
Is there a way to make the map reload its self after the button has been clicked? I think after the url is updated the map is not initialised with the new url
show code that sets the source attribute to the element... or show a demo of your page
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.