Results 1 to 4 of 4

Thread: Dynamic variable names

  1. #1
    Join Date
    Sep 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Dynamic variable names

    Hi guys

    I'm trying to include multiple Google maps on my page, however, I need these to be dynamically generated. The code to include one map on my page is:

    <script type="text/javascript">
    //<![CDATA[
    function load() {
    if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.setCenter(new GLatLng(37.4419, -122.1419), 13);
    }
    }
    //]]>
    </script>

    <div id="map" style="width: 250px; height: 250px"></div>

    I also have a MapID which comes from a mySQL DB:

    <?php $MapID = $row_RS_Property['RecordID']; ?>

    I basically need to append the MapID to each instance of 'map' within the Javascript code, but I'm not really sure how to write this. I've tried several connotations but with no joy.

    Can anyone help?

    Cheers
    Leigh

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'm not sure that I understand you but, that javascript is object oriented so, you should be able to have as many maps as you like:

    Code:
    function load() {
    if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.setCenter(new GLatLng(37.4419, -122.1419), 13);
    
    var map2 = new GMap2(document.getElementById("map2"));
    map2.addControl(new GSmallMapControl());
    map2.setCenter(new GLatLng(37.4419, -122.1419), 13);
    
    var map3 = new GMap2(document.getElementById("map3"));
    map3.addControl(new GSmallMapControl());
    map3.setCenter(new GLatLng(37.4419, -122.1419), 13);
    }
    }
    You can use different GLatLng's for each one. Each will appear in the element with the id of map, map2 and map3 respectively.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Sep 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi John

    That's what I'm trying to do, however the javascript is contained within a repeat region so i need to find a way of giving each 'var map' a unique ID, any thoughts?

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    This belongs in the PHP forum then.

    I would advise you that I had a look at the terms for this service at Google and it looks to me as though, with enough iterations of this and/or enough traffic to your site, you could get over the daily limit on use pretty quickly and/or run afoul of some of the other usage terms limitations. With that said, if your traffic isn't really high and there are only going to be a few iterations of the code - no matter what a user may decide to do, it shouldn't be a problem.

    What you are looking for is a javascript/PHP hybrid. Something like:

    PHP Code:
    <? while  $MapID $row_RS_Property['RecordID']{ ?>
    <script type="text/javascript">
    //<![CDATA[
    function load() {
    if (GBrowserIsCompatible()) {
    var <? $MapID.name ?> = new GMap2(document.getElementById("<? MapID.name ?>"));
    <? $MapID.name ?>.addControl(new GSmallMapControl());
    <? $MapID.name ?>.setCenter(new GLatLng(<? $MapID.lat ?><? $MapID.long ?><? $MapID.zoom ?>);
    }
    }
    //]]>
    </script>
    <? }; ?>
    There are other ways this can be done, for example with the javascript code part being echo'ed so as not to need to jump into and out of PHP mode so much. However, the precise syntax depends upon the $MapID's data objects and proper PHP usage, that is why I would take this to the PHP forum (I'm a real novice at PHP). It would help them there if you show them the above stab at the code that I wrote, or one of your attempts.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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