0

Am trying to create a health service locator using Google maps API on my Drupal 7 site. (FYI, I cant use location module or open layers, because, Create a hospital locator in drupal 7 )

OK, so now I have a form, which takes the users location, type of service (eg, hospital, pharmacy etc) and the proximity around which they wish to find the result, using Google Maps API and Google Places API I fetch the results (details about those near by places such as hospital) in JSON format from Google. The fetched data includes each place's name, address, rating, phone number, latitude, longitude etc. now I store those values in form_state['storage'] and then sends it to the JS file using the drupal_add_js() method.

The below code is sending the values,

<?php
for($i=0;$i<$length;$i++)
 {
  drupal_add_js(array('a'.$i =>array(
             'lat'=> $form_state['storage']['results'][$i]['geometry']['location']['lat'],
             'long'=> $form_state['storage']['results'][$i]['geometry']['location']['long'],  
         'name'=>  $form_state['storage']['results'][$i]['name'],
         'rating'=>$form_state['storage']['results'][$i]['rating'],
         'vicinity'=>$form_state['storage']['results'][$i]['vicinity'],
         'formatted_address'=>$form_state['storage']['results'][$i]['formatted_address'],
         'international_phone_number'=>$form_state['storage']['results'][$i]['international_phone_number'],
         'website'=>$form_state['storage']['results'][$i]['website'],
)),'setting');

}
?>.

the details of first place is in a0, second place detail is in a1 etc.

FYI, each place details are in Drupal.settings.a0, Drupal.settings.a1 etc. I mean, I can access those variables in js file using,

Drupal.settings.a0.name and Drupal.settings.a0.website similarly for second place, Drupal.settings.a1.name etc.

Now my problem is I need to iterate through these different places.

I can iterate through different variables of one particular place usin the below for loop,

for(var x in Drupal.settings.a0)
{
 alert(Drupal.settings.a0.x);
}

But, I cant iterate through different places, like a0, a1 etc. I mean I cant attach a variable to drupal.settings..!

I tried tried putting all places in an array, i.e, a0,a1,a2 are in an array named arr[] (I have the total no. of places in Drupal.settings.my_vars.length) then, I iterate through this array and tried attaching arr[i] to Drupal.settings as Drupal.settings.arr[i].x but it doesn't work.

How can I do it...?? Is there any better way of achieving it...??

Thanks in advance.

3
  • First your array should be added in your module's namespace ($your_array = array('yourmodule' => $your_array); and then drupal_add_js as usual). Then, I don't see what could prevent you from looping in your Drupal.settings.yourmodule array, for example with JQuery $_each().
    – Countzero
    Mar 30 '12 at 15:40
  • you are right, but in my case, I have many "yourmodule" kind of arrays, i.e, a0, a1 etc. I need to know how can I iterate through these. Mar 30 '12 at 15:49
  • If you add your arrays the way I told above, you have to do something like : $.each(Drupal.settings.yourmodule, function(location) {do_something(location.name);});
    – Countzero
    Mar 30 '12 at 15:58
2

You can add your module name to settings like this

$locations = array();
for($i=0; $i < $length; $i++) {
  $locations[] = array('a'.$i =>array(
    'lat'=> $form_state['storage']['results'][$i]['geometry']['location']['lat'],
    'long'=> $form_state['storage']['results'][$i]['geometry']['location']['long'],  
  );
}

drupal_add_js(array('mymodule' => $locations), 'setting');

and loop is js like

for (var x in Drupal.settings.mymodule) {
  // ...
}
2
  • how can I access the location of 1st place , 2nd lace etc..?? each place will have its own set of locations, name,address etc. Can you please explain your answer a little bit more..? Mar 30 '12 at 14:53
  • so you'll have an array of places and each place has one or more locations, so you have to use a nested loop, use something like $locations[$placename][] on line 3 i the example above, and on the js side something like: for (var place in Drupal.settings.mymodule) { for (var location in place) {
    – Attiks
    Mar 31 '12 at 12:39
1

I got the answer...! The below code works..!

var i = 0, j=0;
while(Drupal.settings["a" + i])
{
  for(j in Drupal.settings["a" + i])
  {
    if(Drupal.settings["a" + i].hasOwnProperty(j))
    {
      alert(Drupal.settings["a" + i][j]);
    }
  }
  i++;
}

But I would like know what exactly is hasOwnProperty(j)...?? (I got this answer in drupal forum)

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.