hello can anyone help me debug little error that my eyes seem to be skipping. error is: unexpected ( error. Are my array syntex correct?
function SourceClusting()
{
// grabbing count
var table = document.getElementById('OSDataCount');
var counter= table.rows[1].children[0].innerHTML
// putting all variable into arrays
var latitude()
var longitude()
var i
var marker =[];
// placing values into arrays
for (i=1;i == counter;i++)
{
longitude[i]=table.rows[i].children[6].innerHTML;
latitude[i]=table.rows[i].children[5].innerHTML;
marker[i]=new GMarker(new GLatLng(longitude[i],latitude[i]));
}
var markerCluster = new MarkerClusterer(map, marker);
}
cheers
The problems are in these lines:
var latitude()
var longitude()
You mean
var latitude;
var longitude;
or possibly
var latitude = [];
var longitude = [];
since you seem to be treating them as arrays.
Quite a few things:
You have to parseInt() the string you get from var counter = ..., as a string can't be used in comparisons with integers the way you'd like.
var latitude = () should be var latitude = [], as it's an array, Don't forget those semicolons!
You usually use a lesser-than sign in a loop, not an equality sign ==.
You can condense the loop by initializing i within it.
Try this new, possibly working code:
function SourceClusting() {
// grabbing count
var table = document.getElementById('OSDataCount');
var counter= parseInt(table.rows[1].children[0].innerHTML, 10);
// putting all variable into arrays
var latitude = [];
var longitude = [];
var marker =[];
// placing values into arrays
for (var i = 0; i < counter; i++)
{
longitude[i]=table.rows[i].children[6].innerHTML;
latitude[i]=table.rows[i].children[5].innerHTML;
marker[i]=new GMarker(new GLatLng(longitude[i],latitude[i]));
}
var markerCluster = new MarkerClusterer(map, marker);
}
var latitude() is nonsense. I suspect you mean var latitude = [];
(With a similar correction for the following line)
If you're trying to instantiate an array, instead of:
var latitude();
it should be:
var latitude = [];
You declare var latitidue(), but that doesn't make anysense. Hence the unexpected '('. Also, missing semi-colon after statement.
Related
I am unable to print result in line by line,it's coming in one line ,how to solve this....
I tried this code-
<script>
function know(){
var num =Number(document.getElementById('number').value);
var range=Number(document.getElementById('range').value);
var output="";
var final=[];
for(i=1;i<=range;i++)
{output=i*num;
final.push(output)
final=final.replace(",","<br/>")}
document.getElementById('result').innerHTML=final
}
</script>
There is 2 issues in it:
the for loop i you need to declare as a var or let(I recommend let)
there is no replace method in array, you can use map in this case
So you can re write the method like this.
<script>
function know(){
var num =Number(document.getElementById('number').value);
var range=Number(document.getElementById('range').value);
var output="";
var final=[];
//for(i=1;i<=range;i++)
for(let i=1;i<=range;i++)
{output=i*num;
final.push(output)
//final=final.replace(",","<br/>")
}
final = final.map((d, ind) => d*(ind+1))
document.getElementById('result').innerHTML=final
}
</script>
I am doing an ajax call to the server to get latitude and longitude data and pushing them to a list containing google.maps.latLng coordinates. I'm doing some calculations because the matrix containing the data comes from a sparse matrix(will of course be rewritten because it's ugly so far). I know that my tempLong and tempLat values are correct, but I keep getting [Uncaught TypeError: google.maps.latLng is not a function] when i perform the ajax call. I suspect that this has do with the asynchronous call that ajax performs, but I'm not sure. I looked at this post and decided to put all my code in the .done{} function, but that does not help.
Anyway, here is my code:
function getMultipleArrays(){
var div = document.getElementById("map2");
$.get('getJSONObject', function(data) {
}).done(function(data){
var minLat = data.minLat;
var minLong = data.minLong;
var m = data.m;
var n = data.n;
var val = data.val;
var col = data.col;
var row = data.row;
var list =[];
var minLatCoo= minLat*1000000;
var minLongCoo= minLong*1000000;
var i, j, k,zeroIndex;
for(i=0; i<m;i++){
zeroIndex=0;
for(k=row[i]; k<row[i+1];k++){
j= col[k];
while(zeroIndex<j){
zeroIndex++;
}
minLatCoo= minLat*1000000;
minLongCoo= minLong*1000000;
for(var l=val[k]; l>0;l--){
minLatCoo+=zeroIndex;
var tempLat = minLatCoo/1000000;
minLongCoo+=i;
var tempLong = minLongCoo/1000000;
list.push(new google.maps.latLng(tempLong,tempLat));
console.log(tempLong + " - " + tempLat);
}
zeroIndex++;
}
}
console.log(list.length);
console.log("DONE!");
return list;
});}
The error occurs on the list.push(new google.maps.latLng(tempLong,tempLat)); line. Any ideas why it fails?
The class is LatLng, not latLng. Javascript's case-sensitive; it should be
list.push(new google.maps.LatLng(tempLong,tempLat));
... not
list.push(new google.maps.latLng(tempLat, tempLong));
Also you do new google.maps.latLng(tempLong,tempLat) from which I inferred you've got your latitude and longitude mixed up.
See:
https://developers.google.com/maps/documentation/javascript/reference#LatLng
I generate some javascript from a php file. In this generated code, I create a multi-dimensional array and then populate it. The first nested array populates without problems but the second ones throws a TypeError: myArray[idx] is undefined.
Here's a code snippet:
function initialize() {
var arrayLabels = [];
var arrayMarkers = [];
var idx = 0;
arrayMarkers[idx] = [];
var mapLatlng = new google.maps.LatLng(40.6029248937, 7.7861327300);
var mapOptions = { center: mapLatlng,
zoom: 13,
mapTypeId: google.maps.MapTypeId.SATELLITE };
var map = new google.maps.Map(document.getElementById("karte"), mapOptions);
var bounds = new google.maps.LatLngBounds();
arrayMarkers[idx]['breite'] = 44.4114053473682;
arrayMarkers[idx]['laenge'] = 8.91858100891113;
arrayMarkers[idx]['farbe'] = "http://new.kfb.localhost:8888/img/ico/button2_gruen.png";
arrayMarkers[idx]['hafen'] = "Ab/bis Genua";
arrayMarkers[idx]['link'] = "Karte, Wetter und<br>Landausflüge für<br><a href='hafen.php?hafen=172'>Genua</a><br>Sa, 16.03.13";
idx++;
arrayMarkers[idx]['breite'] = 43.3449053146323;
The error is thrown at the last line, right after the index has been incremented. Any ideas what the problem is?
Thanks
MK
You're incrementing idx, and then doing this:
arrayMarkers[idx]['breite'] = 43.3449053146323;
You've never put any object at arrayMarkers[idx], and so you end up trying to add a property to undefined, which causes an error.
If you want to create an array at the new index, add the second line shown here:
idx++;
arrayMarkers[idx] = []; // <=== Add this
arrayMarkers[idx]['breite'] = 43.3449053146323;
Side note: The things you're putting in arrayMarkers[idx] are arrays ([]), but you're not using them as arrays, you're using them as objects. You can do that in JavaScript (because those arrays aren't really arrays at all), but unless you're going to make use of the fact they're arrays, I'd just use objects:
arrayMarkers[idx] = {}; // Instead of []
idx++;
arrayMarkers[idx]['breite'] = 43.3449053146323;
should be
arrayMarkers[idx]['breite'] = 43.3449053146323;
idx++;
So that you move to next index after all operations are complete. Using them otherwise increments the value to next index which does not exist
Or if it has to stay the same way then you can initialize that value like
idx++;
arrayMarkers[idx] = [];
arrayMarkers[idx]['breite'] = 43.3449053146323;
jQuery.get("ChkNewRspLive.php?lastmsgID=" + n, function(newitems){
//some code to separate values of 2d array.
$('#div1').append(msgid);
$('#div2').append(rspid);
});
Let's say the value of newitems is [["320","23"],["310","26"]]
I want to assign "320" and "310" to var msgid.
I want to assign "23" and "26" to var rspid.
How to do that?
I tried to display newitems and the output is "Array". I tried to display newitems[0] and the output is blank.
If I redeclare var newitems = [["320","23"],["310","26"]]; it works. So I guess the variable newitems from jQuery.get is something wrong. Is it I cannot pass the array from other page to current page through jQuery directly?
Regarding the array on other page, if echo json_encode($Arraytest); the output is [["320","23"],["310","26"]] but if echo $Arraytest; the output is Array. How do I pass the array from other page to currently page by jQuery.get?
I don't totally understand the question but I'm going to assume you want the values in an array, as two values can't be stored in one (scalar) variable simultaneously.
jQuery.get("ChkNewRspLive.php?lastmsgID=" + n, function(newitems){
//some code to separate values of 2d array.
var msgid = [],
rspid = [];
for( i = 0 ; i < newitems.length ; i++){
msgid[msgid.length] = newitems[i][0];
rspid[rspid.length] = newitems[i][1];
}
//msgid now contains ["320","310"]
//rspid now contains ["23","26"]
});
Bear in mind those are in the function scope. If you want to use them outside of that scope instantiate them outside. see: closure
You can use pluck from underscore.js: http://documentcloud.github.com/underscore/#pluck
var msgid = _(newitems).pluck(0)
var rspid = _(newitems).pluck(1)
Try this:
function getArrayDimension(arr, dim) {
var res = [];
for(var i = 0; i < arr.length; i++) {
res.push(arr[i][dim]);
}
return res;
}
var newitems = [["320","23"],["310","26"]];
var msgid = getArrayDimension(newitems, 0);
var rspid = getArrayDimension(newitems, 1);
msgid and rspid are arrays holding the 'nth' dimention.
Tnx
I have a google maps api function place markers which I'm using from the tutorial found here:Google Maps API with JQuery
By any means, I had to modify the javascript to account for my application. I'm pulling markers from an XML file like before, though this time I'm getting multiple requests, and multiple standard deviation, time to serve, and means for these requests. I've set up the XML to have these with a counter appended to the tag, but it looks like it's not rendering into an array correctly.
To note, I've never used Javascript, and am mostly flying by the seat of my pants on this, so if it's an atrocity of Javascript, feel free to let me know, the entire generation of the XML is in Python.
Sample of the XML: (I apologize, I don't know how to show < or > on stack overflow without it simply hiding it as a tag. Around each "markers" "marker" "name" "requestX" "timetoserveX" etc. is the < and > for tags in XML.
markers
marker
name Simpletown, CA /name
request0 /resource/ /request0
timetoserve0 .001 Seconds to serve request /timetoserve0
mean0 .5309 Mean in seconds /mean0
std_dev0 .552 Standard Deviation in Seconds /std_dev0
request1 /resource2/ /request1
timetoserve1 0.015626 Seconds to serve request /timetoserve1
mean1 0.0011 Mean in seconds /mean1
std_dev1 0.004465 Standard Deviation in Seconds /std_dev1
/marker
/markers
MYMAP.placeMarkers = function(filename) {
$.get(filename, function(xml){
$(xml).find("marker").each(function(){
var name = $(this).find('name').text();
var count = 0;
var requeststring = 'request' + Integer.toString(count)
var request = new Array();
var timetoserve = new Array();
var mean = new Array();
var std_dev = new Array();
var timetoservestring = 'timetoserve' + Integer.toString(count);
var meanstring = 'mean' + Integer.toString(count);
var std_devstring = 'std_dev' + Integer.toString(count);
while ($(this).find(requeststring).text()){
timetoservestring = 'timetoserve' + Integer.toString(count);
meanstring = 'mean' + Integer.toString(count);
std_devstring = 'std_dev' + Integer.toString(count);
request[count] = $(this).find(requeststring).text();
timetoserve[count] = $(this).find(timetoservestring).text();
mean[count] = $(this).find(meanstring).text();
std_dev[count] = $(this).find(std_devstring).text();
count++;
requeststring = 'request' + Integer.toString(count)
}
// create a new LatLng point for the marker
var lat = $(this).find('lat').text();
var lng = $(this).find('lng').text();
var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));
// extend the bounds to include the new point
MYMAP.bounds.extend(point);
var marker = new google.maps.Marker({
position: point,
map: MYMAP.map
});
var infoWindow = new google.maps.InfoWindow();
var html = ""
for (i=0;i<count;i++){
html=html+'<strong>'+name+'</strong.><br />'+request[i]+'<br />'+timetoserve[i]+'<br />'+mean[i]+'<br />'+std_dev[i]+<br />;
//var html='<strong>'+name+'</strong.><br />'+request+'</strong.><br />'+timetoserve+'</strong.><br />'+mean+'</strong.><br />'+std_dev;
}
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(MYMAP.map, marker);
});
MYMAP.map.fitBounds(MYMAP.bounds);
});
});
}
With the updates, it's not longer having the array issues it looks like, even though I am getting the map to render at this point. The "Show Markers" button is not populating the map with markers. Running FireBug with this seems to only spew endless amounts of "Break on error" hits and warnings for jQuery.
var request[count] = $(this).find(requeststring).text();
var timetoserve[count] = $(this).find(timetoservestring).text();
var mean[count] = $(this).find(meanstring).text();
var std_dev[count] = $(this).find(std_devstring).text();
to
request[count] = $(this).find(requeststring).text();
timetoserve[count] = $(this).find(timetoservestring).text();
mean[count] = $(this).find(meanstring).text();
std_dev[count] = $(this).find(std_devstring).text();
-assuming these are the arrays that are not working correctly for you.
If I may make a suggestion (RE your comment this morning):
while ($(this).find(requeststring).text()){
//...omitted
}
if you were to rename all your tags to the same thing and give them an id attribute (which is valid in xml) you can do this:
var requests = $(this).find('request');
var timetoserves = $(this).find('timetoserve');
// etc...
for (var i=0; i<requests.length; i++) {
request[i] = requests.eq(i).text();
timetoserve[count] = timetoserves.eq(i).text();
//etc...
}
which would probably provide better performance.