Grab latitude/longitude and send to Google Maps API

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
james64
Forum Newbie
Posts: 5
Joined: Tue Nov 04, 2008 3:39 pm

Grab latitude/longitude and send to Google Maps API

Post by james64 »

I know when it comes to the Google Maps API that a lot of it has to do with JavaScript, but the part I need help with is the php portion of what I am trying to do. What I am trying to do is grab an address from my database, get the lagitude/longitude coordinates and plot them in a certain method. I can grab the address from my database, calculate the latitude/longitude coordinates, but I can't for the life of me figure out how to insert those latitude/longitude coordinates in the method where I need them.

Using static coordinates, everything works great. The thing is, I can't use static coordinates... Here is the code I am using (demo at http://gmaps-samples.googlecode.com/svn ... _lazy.html):

Code: Select all

<?php require_once("connection.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>API</title>
    <script type="text/javascript" src="http://maps.google.com/maps?file=api&&v=2.x&key=[my api key would be here]"></script>
    <script type="text/javascript">
    
    <?php
                //Code to collect information from database
        $q_getaddress = "select * from sector WHERE id = " . $_GET['id'];
        $r_getaddress = mysql_query($q_getaddress);
        $dbaddress = mysql_fetch_object($r_getaddress);
    ?>
    //<![CDATA[
 
var panorama;
var currentYaw = 180;
var currentPitch = 0;
var timer;
var currentZoom = 0;
var zoomingIn = true;
 
function getLocation() {
   var address = "<?php echo $dbaddress->streetAddress?>";
   var city = "<?php echo $dbaddress->city ?>";
   var state = "<?php echo $dbaddress->state ?>";
   var zip = "<?php echo $dbaddress->zip ?>";
   
   // Retrieve location information, pass it to GetCoordinates()
   geocoder.getLocations(address, GetCoordinates);
}
 
   // This function adds the point to the map
   function GetCoordinates(response)
   {
      // Retrieve the object
      place = response.Placemark[0];
 
      // Retrieve the latitude and longitude coordinates
      point = new GLatLng(place.Point.coordinates[1],
                          place.Point.coordinates[0]);
   }
 
function load() {
  panorama = new GStreetviewPanorama(document.getElementById("pano"));
  panorama.setLocationAndPOV(new GLatLng(45.595683, -122.503509), {yaw: currentYaw, pitch: currentPitch, zoom: currentZoom});
  timer = window.setInterval(spiral, 200);
}
 
function spiral() {
  currentYaw += 2;
  panorama.panTo({yaw:currentYaw, pitch:currentPitch});
}
 
function stopAndZoom() {
  clearInterval(timer);
  zoomingIn = true;
  timer = window.setInterval(zoom, 500);
}
 
function zoom() {
  if (zoomingIn) {
    currentZoom++;
  } else {
    currentZoom--;
  }
 
  panorama.panTo({yaw:currentYaw, pitch:currentPitch, zoom:currentZoom});
  if (currentZoom == 2) {
    zoomingIn = false;
  }
  if (currentZoom == 0) {
    clearInterval(timer);
    timer = window.setInterval(spiral, 200);
  }
}
    </script>
    <body onload="load()" onunload="GUnload()">
 
     <div id="pano" style='width:500px; height:400px'></div> 
     <br/>
     <input type="button" onclick="stopAndZoom()" value="Click here to Stop and Zoom"/>
    </body>
</html>
 
My problem is, instead of having static coordinates in

Code: Select all

panorama.setLocationAndPOV(new GLatLng(45.595683, -122.503509), {yaw: currentYaw, pitch: currentPitch, zoom: currentZoom});
I want to use the dynamic coordinates calculated in

Code: Select all

// Retrieve the latitude and longitude coordinates
      point = new GLatLng(place.Point.coordinates[1],
                          place.Point.coordinates[0]);
I have been searching and trying to figure this out for hours, but with no success. If anyone has any ideas on how I could use my dymically-generated coordinates instead of the static ones, I would greatly appreciate it. Thank you.
james64
Forum Newbie
Posts: 5
Joined: Tue Nov 04, 2008 3:39 pm

Re: Grab latitude/longitude and send to Google Maps API

Post by james64 »

Crisis averted; I figured out a solution. Instead of just trying to convert the address to a latitude/longitude coordinate and inserting it into the appropriate area, I just passed the latitude/longitude coordinates from a previous page to this one. Then all I had to do was replace the static coordinates with the dynamic ones found via $_GET and everything is working the way it should.

Hopefully this will help someone else out who may be having a similar problem.
Post Reply