0

I have a DB query which returns a bunch of location based data from my DB. I end up with a list of objects (which I put in to a single object and console.log() it to see whats inside).

Each object represents an entry from a user which contains notes on the location:

It contains: store name, address, userID, notes.

So many people can visit the location and write different notes. What I want to do is put the locations on a Google Map (JS API) and kind of group by location, so when a marker is clicked, it contains all notes for that location.

SO I thought about grouping the objects returned by address, then for each address, plot the marker and loop through.

for (var i = 0; i < rowCountVisits; i++) {
    var storelatlng = new google.maps.LatLng(
    parseFloat(result[2].latVisit[i]),
    parseFloat(result[2].lngVisit[i]));
    locationsObjVisits.push({
        storelatlng: storelatlng,
        vName: result[2].vName[i],
        address: result[2].locationVisit[i],
        date: result[2].dateVisit[i],
        notes: result[2].notes[i],
        usersName: result[2].user.thisName[i],
        thisColour: result[2].user.thisColour[i]
    });
}

So the locationsObjVisits... I am not sure what to do with this. Any ideas?

1 Answer 1

2

I would store the locationsObjVisits in an object with array properties, using the addresses as keys. So:

var locationsObjs = {};

for (var i = 0; i < rowCountVisits; i++) {
    var storelatlng = new google.maps.LatLng(
    parseFloat(result[2].latVisit[i]),
    parseFloat(result[2].lngVisit[i]));
    var address = result[2].locationVisit[i];

    locationsObjs[address] = locationsObjs[address] || [];
    locationObjs[address].push({
        storelatlng: storelatlng,
        vName: result[2].vName[i],
        address: address,
        date: result[2].dateVisit[i],
        notes: result[2].notes[i],
        usersName: result[2].user.thisName[i],
        thisColour: result[2].user.thisColour[i]
    });
}

Now you have all the objects grouped by address, and you can retrieve them from the object in an array using the address as the key. You can also loop through the keys of the locationsObjs object to get a unique list of addresses. So:

for(var address in locationsObjs)
    if(locationsObjs.hasOwnProperty(address))
        // Plot marker using the address.
Sign up to request clarification or add additional context in comments.

2 Comments

This is exactly what I am after, but theres a little problem. It does group the arrays by address, but each of the locationsObj's sub arrays contain every single object returned from the server, its not including only the ones with the matching address... do you know what I mean?
Yeah thats it! Thanks mate! Wouldnt have thought of doing anything like that, eally elegant solution

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.