0

I have an array:

var markerArray = [];//global var
var markerCnt = 0;//global var
for(var x=0;x<10;x++){
    markerArray[markerCnt] = new Array(agency, address, marker, onMarkerClick);
    //agency and agency = string
    //marker = google maps marker
    //onMarkerClick = function
    markerCnt++;
}

How do I call a specified onMarkerClick function? Would I just do:

markerArray[0][3]();
2
  • Assuming onMarkerClick references a function, then yes. You should have tried that before you posted your question :) Commented Aug 10, 2012 at 21:23
  • 1
    You can make the new array with [agency, address, marker, onMarkerClick] instead of new Array() Commented Aug 10, 2012 at 21:36

2 Answers 2

1

As an alternative solution, you can also do this:

var markerArray = [];
var markerCnt = 0;

for(var x=0;x<10;x++){
    markerArray[markerCnt] = {
        'agency' : agency, 
        'address' : address, 
        'marker' : marker, 
        'click' : onMarkerClick
    };
    markerCnt++;
}

//To call the click
markerArray[0].click();
Sign up to request clarification or add additional context in comments.

2 Comments

I dont want to click on the marker tho, I already have that functionality, I want to click on a list item outside of the map and have it open a infowindow. I figured it out.
Are you working with Google Maps API? You should have tagged that in your question. I just finished building a search form that returns a Google Map and markers on the addresses with a listing to the left with the addresses. I'm using the maps api to reverse lookup the lat/lon of the addresses. Clicking the address listing makes the marker bounce. Clicking the marker highlights the corresponding list item and pops up an info window with a link to a detailed information page for the address. It's all AJAX and jQuery and Javascript.
0

The answer to your question is yes.

You can execute any function stored in an array, no matter how many dimensions.

// perfectly valid
markerArray[0][3]()

// as is this
someArray[0][1][7][2]()

To go a little bit beyond than just answering your question, I would suggest using an array of objects so you don't have to execute an array member. This will increase readability of your code and saves you a few hours if you look at it in 6 months trying to figure out what you did.

var markerArray = [];//global var
var markerCnt = 0;//global var
for(var x=0;x<10;x++){
    markerArray[markerCnt] = {
        agency: agency
        address: address
        marker: marker
        onMarkerClick: onMarkerClick
    };
    //agency and agency = string
    //marker = google maps marker
    //onMarkerClick = function
    markerCnt++;
}

// then reference your function
markerArray[0].onMarkerClick();

Comments

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.