Good afternoon, im trying to pass the coordinates for 2 separate polylines from an array, so far the array gets populated from the coordinates held in the JSON file, its just I cant seem to then use that array to actually populate the polyline coordinates, it simply doesnt appear on the map - no errors identified in console.
Im new to this so please be gentle, last night was a very late night trying to get the thing to work. Im using latest version of goMap jquery plugin, im starting to edge towards a limitation in the plugin, but knowing my lack of skills this is a stab in the dark?
Any guidance greatly appreciated. Thanks.
p.s I intend to use the other properties in the JSON file, color, id etc for each polyline, however first I need to get past this stage :)
Relevant JSON file contents as follows:
"lines" :
[
{"id":2011, "colour":"#00CC00", "weight":4, "opacity": 0.5, "coords":[ { "lat": 51.94036, "lng": 4.12734 },{ "lat": 54.05466, "lng": 13.78057 },{ "lat": 54.13938, "lng": 13.76870 },{ "lat": 52.40433, "lng": 13.18649 },{ "lat": 51.53493, "lng": 10.75077 },{"lat": 51.25631, "lng": 7.15687 },{ "lat": 50.45001, "lng": 5.95817 },{ "lat": 51.94036, "lng": 4.12734}]
},
{"id":2010, "colour":"#3399FF", "weight":4, "opacity": 0.5, "coords":[ { "lat": 51.33761, "lng": 3.18406 },{ "lat": 50.82675, "lng": 2.18220 },{ "lat": 50.70592, "lng": 2.24091 },{ "lat": 51.18277, "lng": 3.20565 },{ "lat": 50.64371, "lng": 5.55140 },{ "lat": 50.45001, "lng": 5.95817 },{ "lat": 50.33386, "lng": 6.94722 },{ "lat": 50.31374, "lng": 6.96073 },{ "lat": 45.76458, "lng": 9.05604 },{ "lat": 45.55665, "lng": 9.05278 },{ "lat": 47.75976, "lng": 7.32900 },{ "lat": 48.65055, "lng": 6.14543 },{ "lat": 51.33761, "lng": 3.18406 }]
}
]
Relevant aspects of script:
$.get('positions.json', function (data) {
for (var i = 0, l = data.lines.length; i < l; i++) {
var mypath = new Array();
for (var j = 0, k = data.lines[i].coords.length; j < k; j++) {
var coords = data.lines[i].coords[j];
mypath.push(coords.lat, coords.lng);
}
$.goMap.createPolyline({
color: "#00CC00", opacity: 0.5, weight: 4, coords: mypath
});
}
}, 'json');
Is that your whole json file? If so you probably need to start the file with a "{" and end it with a "}" to signfy that it's a javascript object.
for (var j = 0, k = data.lines[i].coords.length; j < k; j++) {
var coords = data.lines[i].coords[j];
mypath.push(coords.lat, coords.lng);
}
coords
in each iteration will be {latitude:xx, longitude:yy}, which is what createPolyline expects an array of. Changing the inner loop to the below should work, though I haven't tested this.
for (var j = 0, k = data.lines[i].coords.length; j < k; j++) {
mypath.push(data.lines[i].coords[j]);
}