1

I'm new to JavaScript and am making an online map visualization. I need to read in a local csv file of geographical coordinates as an array.

For example, I have:

var data = [
  new google.maps.LatLng(40.3456455, -74.6558775),
  new google.maps.LatLng(40.3456456, -74.6558772),
  ];

And I want to be able to populate this with the coordinates I have in my CSV file.

I looked at jquery-csv but it requires the csv to be passed in as a string, and that passing a filepath and having Javascript read the file is not possible. Should I have the string hard-coded, or try and read it in in Python and somehow pass the string to Javascript? What is the best way to manage this?

Thanks!

3
  • If you want the user to be able to supply the csv (rather than the server) then you need to look into the file API (and the drag and drop API if you want a nice UX) Commented Dec 12, 2014 at 21:39
  • No, I would want the server to supply it. How would I start implementing that? Commented Dec 12, 2014 at 21:47
  • In that case you should probably just grab it with ajax Commented Dec 12, 2014 at 22:09

2 Answers 2

1

You can read the file and its contents with a Jquery get function example:

//the path to your csv file
var filePath ="pathToYourCSVFile";

$.get(filePath,function(data){


//do something with the data
//whatever you do with the data needs to been done inside this function


//example of what you could do

//array to hold the lines of your data file
//data string is split up by each new line
var lines = data.split("\n");

//you could then loop through each line and populate the values you need for each line



});
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to read a local file, you need to use the HTML5 File API. After you have the CSV Data, I would parse it and convert to JSON. Here is a nice example including a link to a demo.

If you do not want to use the HTML5 File API, you can upload the CSV file to the server and convert the CSV to JSON there. Please find solution in PHP here.

I suggest you create a JSON like this, since this is easy to process with JavaScript on client side:

[
  {
    "latitude":40.3456455,
    "longitude":-74.6558775
  },
  {
    "latitude":40.3456456,
    "longitude":-74.6558772
  }
] 

3 Comments

The HTML5 File API requires the user to upload the file, though. I don't want that-- I have the file and I want the program to automatically load it when the browser opens.
If it is OK to load the CSV from the server, then you can load it with JavaScript/jQuery and process it like in the example in my second link.
Actually both david and Larry Lane suggest something similar. Did you make some progress?

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.