Extracting Json Data through an Ajax request:
{
"VehicleID": 1,
"Tracks": [{
"TrackID": 1,
"trackPoints": [{
"lat": 36.25514030456543,
"lng": 33.501180542295344
}, {
"lat": 36.255140304562,
"lng": 33.5011805422952
}, {
"lat": 36.255140304561,
"lng": 33.5011805422951
}]
}, {
"TrackID": 1,
"trackPoints": [{
"lat": 31.25514030456543,
"lng": 31.501180542295344
}, {
"lat": 31.255140304562,
"lng": 31.5011805422952
}, {
"lat": 31.255140304561,
"lng": 31.5011805422951
}]
}]
}
Implementation using Google Maps with two nested loops to iterate over tracks and track points, additionally logging output to the console:
for (var i = 0; i < numtracks; i++) {
var numPoints = result.Tracks[i].trackPoints.length;
arr[i] = new Array(numPoints); //creating 2d array for points
console.log("*********Array number " + i + " Got created" + "and its length is " + arr[i].length + " *******************");
console.log("Length of track #" + i + " Points => " + numPoints);
console.log("array length for Points track #" + i + " => " + arr[i].length);
Secondary loop for accessing individual points:
for (var j = 0; j < result.Tracks[i].trackPoints.length; j++) {
var x = result.Tracks[i].trackPoints[j].lat;
var y = result.Tracks[i].trackPoints[j].lng;
console.log("x= " + x + " for #" + j + " Point");
console.log("y= " + y + " for #" + j + " Point");
Data transmission to Google Maps API:
var p = new google.maps.LatLng(x, y);
console.log("p before pushing latitude: " + p.lat());
console.log("p before pushing longitude: " + p.lng());
Upon storing data in arr[i]:
arr[i].push(p);
//when accessing arr[i][j].lat();is not recognizing it
console.log("track #" + i + " , point #" + j + " pushed into array x= " + arr[i][j].lat() + " ,y=" + arr[i][j]);
The following logs appear in the console:
array length for tracks => 2 trackss:96
Tracks length : 2 trackss:97
*********Array number 0 Got created and its length is 3 ******************* trackss:110
Length of track #0 Points => 3 trackss:111
array length for Points track #0 => 3 trackss:112
x= 36.25514030456543 for #0 Point trackss:116
y= 33.501180542295344 for #0 Point trackss:117
p before pushing latitude for the 36.25514030456543 trackss:119
p before pushing longitude for the 33.50118054229529 trackss:120
Uncaught TypeError: Cannot read property 'lat' of undefined