views:

124

answers:

3

Hello Code Wizards,

I am a total NOOB in programming (but this is only my second question on stackoverflow :-) ).

By a foreach function I get 5 different string values for $Loncoord, $Latcoord, $gui; this I can see with the print_r in the code written below:

"-5.68166666667","+24.6513888889","IMG_3308",

But I now want to create 5 different markers in the $map->addMarkerByCoords (function is it ?).

print_r ("$Loncoord");
print_r ("$Latcoord");
print_r ("$gui");

$map->addMarkerByCoords("$Loncoord","$Latcoord","$gui",'OldChicago');

Is this possible?

Do I need to put them in a array and call these in the (function ?) or do I need to use a foreach function? I tried both for a week now but I can't get it working.

Can you help me?

The anwsers you produced gave me a turn in the right direction. Thank you for the quick responses and the explaining part.

But for the addMarkerByCoord (fuction! (stupid me)) i found this in the googlemaps API:

function addMarkerByCoords($lon,$lat,$title = '',$html = '',$tooltip = '') {
        $_marker['lon'] = $lon;
        $_marker['lat'] = $lat;
        $_marker['html'] = (is_array($html) || strlen($html) > 0) ? $html : $title;
        $_marker['title'] = $title;
        $_marker['tooltip'] = $tooltip;
        $this->_markers[] = $_marker;
        $this->adjustCenterCoords($_marker['lon'],$_marker['lat']);
        // return index of marker
        return count($this->_markers) - 1;
    }
A: 

Try losing some of the quotes...

$map->addMarkerByCoords($Loncoord,$Latcoord,$gui,'OldChicago');

To answer the question properly though, we would need to know what addMarkerByCoords was expecting you to pass to it.

rikh
+1  A: 

If you want to call the addMarkerByCoords for 5 times with 5 different values for each parameter then you can build an array for every parameter and then iterate with the foreach function:

$Loncoord=array(1,2,3,4,5);
$Latcoord=array(1,2,3,4,5);
$gui=array(1,2,3,4,5);
$city=array('OldChicago','bla','bla','bla','bla');
foreach($Loncoord as $k=>$v)
   $map->addMarkerByCoords($Loncoord[$k],$Latcoord[$k],$gui[$k],$city[$k]);
mck89
+1  A: 

It depends on the implementation of map::addMarkerByCoords()

The method (not a function) name, and its signature, suggests that you are only able to add one coord at a time. But to be sure you'ld need to know the methods true signature. So the question is: does the method allow arrays as arguments?

Usually, a method that allows you to add multiple items at once, has the plural name of the intended action in it's name:

map::addMarkersByCoords() // note the s after Marker

If the 'map' class is your own implementation, you are free to implement it the way you like of course, but in that case keep the descriptive names of the methods in mind. So, add one marker:

map::addMarkerByCoords()

Add multiple markers at once:

map::addMarkersByCoords()

Typically you would implement the plural method as something like this:

public function addMarkersByCoords( array $markers )
{
  foreach( $markers as $marker )
  {
      $this->addMarkerByCoord( $marker[ 'long' ], $marker[ 'lat' ], $marker[ 'img ' ], $marker[ 'name' ] );
  }
}

Basically, the plural method accepts one array, and adds each individual marker by calling the singular method.

If you wanna get even more OOP, you could implement the plural and singular method to accept (an array of) Marker objects. But that is not particalarly relevant for this discussion.

Also, the suggested expantion of the Map's interface with a plural method doesn't nessecarily mean you can't add multiple markers outside the object with calling the singular method in a foreach loop. It's up to your preference really.

fireeyedboy