Basically I'm trying to load a csv file, parse it to an array of js objects and use those objects to make map markers with the Google Maps API.
The loading works, the parsing works, all the values are correct (to my knowledge), I've been console logging to death and I get the values that I want but... my map isn't loading.
I think it may be because of timing? Like the map isn't initializing or being loaded correctly.
I occasionally get errors about connections timing out and security errors from the Maps API but refreshing the page and reloading the csv seems to clear that up. The errors come and go.
Here's the JS:
window.onload = function() {
var fileInput = document.getElementById('fileInput');
var fileDisplayArea = document.getElementById('fileDisplayArea');
fileInput.addEventListener('change', function(e) {
var file = fileInput.files[0];
var textType = /csv.*/;
var companies;
// Check if csv file. If so, then run program. If not, show error.
if (file.type.match(textType)) {
var reader = new FileReader();
reader.onload = function(e) {
var text = reader.result;
// Log for debug. Success.
// console.log(text);
// Parse csv file and make objects to store in companies array.
function csvParse(csv) {
var lines = csv.split("\n");
// Log for debug. Success.
// console.log(lines);
var result = [];
var headers = lines[0].split(",");
for (var i = 1; i < lines.length; i++) {
var obj = {};
var currentline = lines[i].split(",");
for (var j = 0; j < headers.length; j++) {
obj[headers[j]] = currentline[j];
}
result.push(obj);
}
return result;
}
// Store objects in companies.
companies = csvParse(text);
// Log for debug. Success.
// console.log(companies);
var siteCircle;
var companyMarker;
// Log for debug. Success.
// console.log(companies[1].sites);
function initialize() {
// Create the map of north america.
var mapOptions = {
zoom: 5,
center: new google.maps.LatLng(37.09024, -95.712891),
mapTypeId: google.maps.MapTypeId.TERRAIN
}
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
// Construct a circle and marker for each value in companies.
for (var company in companies) {
var latitude = (parseFloat(companies[company].lat)).toFixed(6);
var longitude = (parseFloat(companies[company].long)).toFixed(6);
// Log for debug. Success.
// console.log(latitude);
// console.log(longitude);
// console.log(parseInt(companies[company].sites));
var circleStyle = {
// Set constant options.
strokeColor: '#000000',
fillColor: '#000000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillOpacity: 0.35,
map: map,
center: new google.maps.LatLng(latitude, longitude),
radius: parseInt(companies[company].sites) * 100
};
// Not yet. circles first.
/*
var markerOptions = {
// Place marker at same loacation and with a label.
position: new google.maps.LatLng(parseFloat(companies[company].lat), parseFloat(companies[company].long)),
map: map,
title: companies[company].name,
};
*/
// Log for debug. Success.
// console.log(circleStyle)
// Add circle and marker to map. Repeat.
siteCircle = new google.maps.Circle(circleStyle);
// Circles need to populate first.
// companyMarker = new google.maps.Marker(markerOptions);
}
}
// Run mapping.
initialize();
}
reader.readAsText(file);
} else {
fileDisplayArea.innerText = "File not supported!";
}
});
}
Also here is a gist with all the files : https://gist.github.com/mhelmetag/20eeae8cd4c901fb1094
Thanks so much in advance!
Your posted code does not contain all the issues.
I commented out the
if (file.type.match(textType)) {
test, that was giving me an error: "File not supported!" fiddlethe csvParse function doesn't correctly parse the last entry of the file, you are getting NaN for the longitude (so the google.maps.LatLngs are not valid)
removed the .toFixed(6) after the parseFloat call, changed:
To:
your map doesn't have a size, so once the issue with "longitude" is fixed, you can't see it.
as an aside you have an issue with your css, the line below causes artifacts with the map controls, I removed it:
img { max-width: 100%; }
working jsfiddle
Data file used:
working code snippet: