Jump to content

[SOLVED] problem with variables


jakebur01

Recommended Posts

I got my google map working here. I was trying to add an info window to it. When I enter my variable $Address in all I get back is null. I had to do that urlencode on my variable earlier in the script. How can I pass my variable down correctly so I will be able to display my info in the info window?

 

  <?php

   // Your Google Maps API key
   $key = "mykey";

   // Connect to the MySQL database
   $conn = mysql_connect("localhost", "myuser", "mypass");

   // Select the database
   $db = mysql_select_db("mydb");

   // Query the table
   $query = "SELECT id, Address, City, State FROM smithssc WHERE id=$id";
   $result = mysql_query($query) or die(mysql_error());

   // Loop through each row, submit HTTP request, output coordinates
   while (list($id, $Address, $City, $State) = mysql_fetch_row($result))
   {
      
  $mapaddress = urlencode("$Address $City $State");

      // Desired address
      $url = "http://maps.google.com/maps/geo?q=$mapaddress&output=xml&key=$key";

      // Retrieve the URL contents
      $page = file_get_contents($url);

      // Parse the returned XML file
      $xml = new SimpleXMLElement($page);

      // Parse the coordinate string
      list($longitude, $latitude, $altitude) = explode(",", $xml->Response->Placemark->Point->coordinates);

      // Output the coordinates
      //echo "latitude: $latitude, longitude: $longitude <br />";

   }

?>
<script src="http://maps.google.com/maps?file=api&v=2&key=mykey"
      type="text/javascript"></script>
    <script type="text/javascript">
    //<![CDATA[
    function load() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
map.addControl(new GMapTypeControl());
map.addControl(new GLargeMapControl());

map.setCenter(new GLatLng(<?php echo"$latitude"; ?>,<?php echo "$longitude"; ?>), 14);
    map.openInfoWindow(map.getCenter(),
document.createTextNode(<?php echo"$Address"; ?>));

        
  }
    }
    //]]>
    </script>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.