www.webdeveloper.com
Results 1 to 2 of 2

Thread: Dynamic variable name in

  1. #1
    Join Date
    Apr 2014
    Location
    Posadas, Argentina.
    Posts
    1

    Dynamic variable name in

    Hi guys, Im coding in javascript a script for a google map, and Im facing a problem.
    I have data to create different markers in the map, and I put this data into an array, then I have a function that creates the markers and put them on the map. The problem is that I want to create this markers in a dynamic way, naming them different to each other, so I can manipulate them later. I was reading and I don't know which is the right way (if there is one!) to do his in javascript.
    Something like...

    var "marker" + places[i] = new google.maps.Marker({...

    Any ideas? Im in a dead end path! cannot resolve this!

    Put the full code below to better understanding...

    Code:
    /*this is the array that contains the data for using inside the function*/
    var arrayPlaces = [
    	  	['Posadas', -27.367467, -55.895501, 4, '<p>POSADAS</p><p>Misiones</p><p>Cordoba 1955</p>'],
    		['Eldorado', -26.403262, -54.634974, 5, '<p>ELDORADO</p><p>Misiones</p><p>Av. San Martin 1506</p>'],
    		['Apostoles', -27.904169, -55.751417, 3, '<p>APOSTOLES</p><p>Misiones</p><p>Mitre 220 casi Alvear</p>']
    	];
    
    /*this is function that creates the markers*/
    function setMarkers(map, places){
    	for (var i = 0; i < places.length; i++) {
        	var sucursal = places[i];
        	var myLatLng = new google.maps.LatLng(sucursal[1], sucursal[2]);
    		var marker = new google.maps.Marker({
                         position: myLatLng,
            	     map: map,
            	     title: sucursal[0],
            	     zIndex: sucursal[3]
        	        });	
      	}
    }
    
    function initialize() {
            [...]		
    	setMarkers(map, arrayPlaces);
    }

  2. #2
    Join Date
    Jul 2011
    Posts
    131
    You can add array/object keys dynamically, for example something like this:
    Code:
    var markers = [];
    function setMarkers(map, places){
    	for (var i = 0; i < places.length; i++) {
        	var sucursal = places[i];
        	var myLatLng = new google.maps.LatLng(sucursal[1], sucursal[2]);
    		// Or you can just use markers.push instead.
                    markers['marker' + i] = new google.maps.Marker({
                         position: myLatLng,
            	     map: map,
            	     title: sucursal[0],
            	     zIndex: sucursal[3]
        	        });	
      	}
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles