0

Just want to pass php variables $x and $y to javascript map function (as lat and long). Please help. Here is my controller:

<?php
$x = 25.114667;
$y = 55.137797;
?>

Here is my JS file:

function initialize()
    {
    var myCenter = new google.maps.LatLng(lat,long);
    var mapOptions = {
      center:myCenter,
      zoom:12,
      mapTypeId:google.maps.MapTypeId.ROADMAP
      };
    var map=new google.maps.Map(document.getElementById("googleMap")
      ,mapOptions);

    }   google.maps.event.addDomListener(document.getElementById('showhidemap'), 'click', initialize);
9
  • Try to debug you code-> alert(lat) below the var long = "<?php echo $y; ?>"; and see value is coming or not. Commented May 24, 2014 at 12:09
  • the alert message was <?php echo $y; ?> Commented May 24, 2014 at 12:14
  • the alert message was <?php echo 'can i see that?'; ?> Commented May 24, 2014 at 12:22
  • the javascript code is in external file. Can it be a problem? Commented May 24, 2014 at 12:23
  • Ok, you can't run PHP it seems.. I suspect this is a plain .js file, you need to execute your php echos inside a php file, which will product a javascript object, then use this object in the .js file. Commented May 24, 2014 at 12:25

3 Answers 3

2

you are passing string in you js variable lat and long. assign value directly as float
try this

 var lat = <?php echo $x; ?>;
 var long = <?php echo $y; ?>;

also must include the script with google map key on above of google map initialization.

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=GOOGLE_MAP_KEY&sensor=true"> </script>

GOOGLE MAP KEY : must be generate for your site.

see some urls might help you

https://developers.google.com/maps/tutorials/fundamentals/adding-a-google-map
https://developers.google.com/maps/signup?csw=1
https://developers.google.com/maps/documentation/javascript/tutorial

Sign up to request clarification or add additional context in comments.

18 Comments

I have already tried in that way. Codeigniter shows syntax errors.
what kind of syntax error? show me. may be syntax error for another reason.
I took a printscreen of the error. Please advise how can I send it to you.
@TProDeveloper update your question with that image.
I just did it. Please have a look.
|
0

did you try to add

$x = 25.114667;
$y = 55.137797;

to some tag like

<div data-lat="<?php echo $y; ?>" data-long="<?php echo $y; ?>" id="showhidemap">show map</div>

Comments

0

You need to create a Javascript object in your .php file, just before your include your javascript file (the one containing the above fragment), which will be filled by your longitude/latitude information. Like this:

YOUR PHP FILE

<html>
   ...
   <script type="text/javascript">
      var mapLangLat = {
         long:<?php echo $x; ?>,
         lati:<?php echo $y; ?>
      };
   </script>
   <script src="my_remote_script.js"></script>
   ...

YOUR JAVASCRIPT FILE

<script>
function initialize()
    {

    var lat = window.mapLangLat.lati;
    var long = window.mapLangLat.long;

    ...

It's required to find the PHP part above to be able to code that little javascript object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.