1

I'm using javascript with the GOOGLE API, it works with snapping the point to the nearest street manually by clicking it. Example taken from (http://econym.org.uk/gmap/snap.htm)

I have a database of coordinates I want to feed through this thing, to snap the points to the closest street, I'm using PHP, but I can't get the mysql PHP variables to "talk" with the javascript.

I want it to pretty much be close to real time, read URL variables add them to database then have it snap it on road on Google maps

I can manually do it from the URL variables, but I can't get PHP and the database coordinates to be read by the javascript GoogleAPI snap to road function

//Fetching from your database table.
            $query = "SELECT DISTINCT * FROM $usertable";
            $result = mysql_query($query);

            if ($result) {
                while($row = mysql_fetch_array($result)) {
                    $name = $row["$latitude_db"];
                    $name1 = $row["$longitude_db"];


  echo "var dirn = new google.maps.DirectionsService();";
  echo "var map;";
echo "
  function initialize() {
    var center = new google.maps.LatLng(40.04, -79.23);
    var myOptions = {
      zoom:10,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: center
    }
    ";
    echo "map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);";

    echo "var urllat = getUrlVars()['lat'];";

    echo "var urllong = getUrlVars()['long'];";



    echo "var combo = new google.maps.LatLng('$name', '$name1');";
    echo "alert($name1);";





    echo "var request = {";
            echo "origin: combo,";
            echo "destination: combo,";
            echo "travelMode: google.maps.DirectionsTravelMode.DRIVING";
        echo "};";
        echo "dirn.route(request, function(response, status) {";
          echo "if (status == google.maps.DirectionsStatus.OK) {";
               echo " if(response.routes && response.routes.length > 0){";
                   echo " route = response.routes[0];";
                   echo " if(route.overview_path && route.overview_path.length > 0)";
            echo "{";

                        echo "pos = route.overview_path[0];";


                            echo "new google.maps.Marker({";
                                echo "position: pos,";
                                echo "map: map";
                            echo "});";



                    echo "}";
                echo "}";
          echo "}";
        echo "});";

    echo "}";



                }
            }




}            

echo "</script>";

This code would be in a loop or on page load...

4

0 回答 0