foreach PHP for Javascript

Asked

Viewed 282 times

-1

I’m in the middle of a project I have to do clustering on a map. I found this algorithm which is in PHP. My doubt is in this foreach:

  foreach ($markers as $key => $target) {
        $pixels = pixelDistance($marker['lat'], $marker['lon'],
                                $target['lat'], $target['lon'],
                                $zoom);
        /* If two markers are closer than given distance remove */
        /* target marker from array and add it to cluster.      */
        if ($distance > $pixels) {
            printf("Distance between %s,%s and %s,%s is %d pixels.\n", 
                $marker['lat'], $marker['lon'],
                $target['lat'], $target['lon'],
                $pixels);
            unset($markers[$key]);
            $cluster[] = $target;
        }
    }

in which

$markers   = array();
$markers[] = array('id' => 'marker_1', 
               'lat' => 59.441193, 'lon' => 24.729494);
$markers[] = array('id' => 'marker_2', 
               'lat' => 59.432365, 'lon' => 24.742992);
$markers[] = array('id' => 'marker_3', 
               'lat' => 59.431602, 'lon' => 24.757563);
$markers[] = array('id' => 'marker_4', 
               'lat' => 59.437843, 'lon' => 24.765759);
$markers[] = array('id' => 'marker_5', 
               'lat' => 59.439644, 'lon' => 24.779041);
$markers[] = array('id' => 'marker_6', 
               'lat' => 59.434776, 'lon' => 24.756681);

$clustered = cluster($markers, 20, 11);

How to "translate" to Javascript?

  • 1

    Do you prefer Java, Javascript, or whatever?

  • @bigown Can be javascript

  • 1

    Google Maps only supports javascript. Please change your question to specify javascript and not to confuse it with Java

  • 1

    @Emersonrochaluiz my goal is not to apply the algorithm in a google maps map but to adapt to another type of map

2 answers

1

Javascript does not yet have an equivalent to foreach languages like PHP, Perl, etc.

A possible solution is to use for..in with an extra check:

for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
        value = obj[key];
    }
}

But apparently, your array has numeric indices, so the best way is to loop it for "common":

for (var i = 0; i < myArray.length; i++) {
    var value = myArray[i];

    var id = value['id'];
    var lat = value['lat'];
    // e assim por diante
}

To remove an item from a Javascript array, use the method splice of Array object:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

array.splice(0) // remove o primeiro elemento do array
  • You forgot to declare the i.

  • Javascript does not require variable declaration, but it is recommended to use for (var i=0; i < myArray.length; i++) to make sure that the i be unique in this context and be 0.

  • Very well observed, I will correct.

1


The best here is to create an object array.

So you can do:

var markers   = [];
markers.push = {id : 'marker_1', lat : 59.441193, lon : 24.729494);
// etc

And then use a numeric for loop. Example:

var markers = [];
markers.push({
    id: 'marker_1',
    lat: 59.441193,
    lon: 24.729494
});
markers.push({
    id: 'marker_2',
    lat: 99.441193,
    lon: -24.729494
});
markers.push({
    id: 'marker_3',
    lat: -59.441193,
    lon: -24.729494
});
// etc

for (var i = 0; i < markers.length; i++) {
    var esteObjecto = markers[i];
    var id = esteObjecto.id;
    var lat = esteObjecto.lat;
    var lon = esteObjecto.lon;
    console.log(id, lat, lon);
}

Example

Browser other questions tagged

You are not signed in. Login or sign up in order to post.