I'm trying to get the google maps for websites API working on my php website. for that I have a php page that loads users variables for the maps. the javascript works fine in the webpage with default values, but I'm not able to load the $php variable into the script.

maybe some javascript expert can give a hand

function onProfileDisplay() {
            $user           = CFactory::getRequestUser();
            $document       =& JFactory::getDocument();
            $document->addStyleSheet($css);
            $my         = CFactory::getUser();
            $pluginParams = $this->params;
            $param = new stdClass;
            $param = $Lat = $pluginParams->get('Lat');
            $param = $Lng = $pluginParams->get('Lng');
            $param = $width = $pluginParams->get('width');
            $param = $height = $pluginParams->get('height');
            $param = $zoom = $pluginParams->get('zoom');
            return '<body onload="onprofileDisplay()">  <div id="map_canvas" style="width:800px; height:500px"></div>
                    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
                    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
                    <script type="text/javascript">

 function onProfileDisplay() {
            var latlng = new google.maps.LatLng(57.0442, 9.9116);
            **var zoom = <?php echo ($zoom) ?>;**
            var settings = {
                    **zoom: zoom,**
                    center: latlng,
                    mapTypeControl: true,
                    mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
                    navigationControl: true,
                    navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                    };
            var map = new google.maps.Map(document.getElementById("map_canvas"), settings);
            var companyPos = new google.maps.LatLng(57.0442, 9.9116);
            var companyMarker = new google.maps.Marker({
            position: companyPos,
            map: map,
            title:"Some title"
            });
            }
            </script>
                    ';
            }

to test I'm only trying to load the php variable $zoom into the javascript, but it does not work, inside the javascript:

var zoom = <?php echo ($zoom) ?>;
zoom: zoom,

but it does not work, if I change the javascript to original remove var zoom = ; zoom: 15, then it works but with static default values

Help appreciated.

This is the outuput code:

<script type="text/javascript">
    function onProfileDisplay() { 
    var latlng = new google.maps.LatLng(57.0442, 9.9116);
    var zoom = ".$zoom.";
            var settings = {
                    ".$zoom.":".$zoom.",
                    center: latlng,
                    mapTypeControl: true,
                    mapTypeControlOptions: {style:   google.maps.MapTypeControlStyle.DROPDOWN_MENU},
                    navigationControl: true,
                    navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                    };
            var map = new google.maps.Map(document.getElementById("map_canvas"), settings);
            var companyPos = new google.maps.LatLng(57.0442, 9.9116);
            var companyMarker = new google.maps.Marker({
            position: companyPos,
            map: map,
            });
            }
    </script>

The original code looks like:

     public function onProfileDisplay() {
                $user           = CFactory::getRequestUser();
                $document       =& JFactory::getDocument();
                $document->addStyleSheet($css);
                $my         = CFactory::getUser();
                $pluginParams = $this->params;
                $param = new stdClass;
                $param = $api_key = $pluginParams->get('api_key');
                $param = $Lat = $pluginParams->get('Lat');
                $param = $Lng = $pluginParams->get('Lng');
                $param = $width = $pluginParams->get('width');
                $param = $height = $pluginParams->get('height');
                $param = $zoom = $pluginParams->get('zoom');
                 return  '<body onload="onProfileDisplay()">
                        <div id="map_canvas" style="width:800px; height:500px"></div
</body>
                        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
                        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
                        <script type="text/javascript">
                function onProfileDisplay() {
                var latlng = new google.maps.LatLng(57.0442, 9.9116);
                var zoom = ".$zoom.";
                var settings = {
                        ".$zoom.":".$zoom.",
                        center: latlng,
                        mapTypeControl: true,
                        mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
                        navigationControl: true,
                        navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                        };
                var map = new google.maps.Map(document.getElementById("map_canvas"), settings);
                var companyPos = new google.maps.LatLng(57.0442, 9.9116);
                var companyMarker = new google.maps.Marker({
                position: companyPos,
                map: map,
                });
                }
                </script>';
                }
    }
    ?>
share|improve this question

1 Answer

up vote 0 down vote accepted

Why are you putting PHP into the return from PHP->javascript function? Seems like a v cumbersome way to get your result.

In any event - why not just put the actual values in there from the PHP function? Once they are in javascript on the client you won't be able to use the server-side value anyway.

example:

function onProfileDisplay() {
var latlng = new google.maps.LatLng(57.0442, 9.9116);
var zoom = " . $zoom . "
var settings = {
" . $zoom . ":" . $zoom . ",
center: latlng,

share|improve this answer
thanks for the help, the script works but the zoom value is not coming from the php, I tested it with document.write(zoom); and the output is $zoom, not the php value for the variable $zoom – Alforreca May 9 '12 at 20:32
codevar zoom = " .$zoom. "; var settings = { " .$zoom. ": " .$zoom. ", center: latlng,code you change actually do not breack the script and the google maps layer shows up blank, because the value being passed for zoom is $zoom and not the php variable – Alforreca May 9 '12 at 20:44
Do a 'view source' on the output and put it in the question – ethrbunny May 10 '12 at 13:20
I saved the page, opened it from hard disk and control+U, the output is in the original question. Thanks – Alforreca May 10 '12 at 17:28
What does the original code look like now? – ethrbunny May 10 '12 at 17:47
show 4 more comments

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

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