PDA

View Full Version : Dynamic variable names



Leighn79
09-17-2006, 04:21 PM
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

jscheuer1
09-18-2006, 05:55 AM
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:


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.

Leighn79
09-18-2006, 03:17 PM
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?

jscheuer1
09-18-2006, 06:14 PM
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:



<? 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.