1

I got txt file with such coordinates:

21.178797 56.888384
21.373250 56.588044
24.136921 56.965521
26.231814 56.858971
24.123382 56.951146
25.399601 57.540989
24.542900 57.090442

I need to put them in this type of array :

var locations = [
 ['Title A', 3.180967,101.715546, 1],
 ['Title B', 3.200848,101.616669, 2],
 ['Title C', 3.147372,101.597443, 3],
 ['Title D', 3.19125,101.710052, 4]
 ];

So i can put multiple markers using loop like so :

for (i = 0; i < locations.length; i++) {  
markers = new google.maps.Marker({
     position: new google.maps.LatLng(locations[i][1], locations[i][2]),
     map: map
     });

     google.maps.event.addListener(marker, 'click', (function(markers, i) {
     return function() {
         infowindow.setContent(locations[i][0]);
         infowindow.open(map, marker);
     }
})(marker, i));

  }

}

Any ideas how i can make it work?

Thanks!:)

Thanks for your quick response guys.

I'll make it a little bit simplier for myself(it's my firs expirience using JS)

I have same file :

21.178797 56.888384
21.373250 56.588044
24.136921 56.965521
26.231814 56.858971
24.123382 56.951146
25.399601 57.540989
24.542900 57.090442

And i need array to look like that :

var locations = [
[3.180967,101.715546],
[3.200848,101.616669],
[3.147372,101.597443],
[3.19125,101.710052]
];

Thanks in advance)

2
  • Is the file on the server? Or a local file the "user" will load from their machine? Commented Dec 8, 2016 at 15:56
  • Right now it is in the same folder with .js file(on machine),i'm getting coordinates from Api using Python and i put them into txt file. Commented Dec 8, 2016 at 16:22

1 Answer 1

1

Parsing the data

Use String#split to split the text into an array of lines, and then to split each line to an array:

var str = `21.178797 56.888384
21.373250 56.588044
24.136921 56.965521
26.231814 56.858971
24.123382 56.951146
25.399601 57.540989
24.542900 57.090442`;

var result = str.split('\n').map(function(line, index) {
  return ['Title ' + String.fromCharCode(index + 65)].concat(line.split(' '));  
});

console.log(result);

Getting the data

Depending on the browsers you plan to support, you can XMLHttpRequest or the newer fetch. This simple method will use fetch if it can, and fallback to XMLHttpRequest:

function getData(path, cb) {
    var oReq;

    if (window.fetch) {
        window.fetch(path)
            .then(function(response) {
                return response.text();
            })
            .then(cb);
    } else {
        oReq = new XMLHttpRequest();
        oReq.addEventListener("load", function() {
            cb(this.responseText);
        });
        oReq.open("GET", path);
        oReq.send();
    }
}

function parseData(data) {
    var coo = str.split('\n').map(function(line, index) {
        return ['Title ' + String.fromCharCode(index + 65)].concat(line.split(' '));  
    });

    // your code that uses coo
}

getData('coo.txt', parseData);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.