Jump to content

Help with coding on Google Map


a2jagger

Recommended Posts

Hi I have been working on this code for almost two weeks and can't get over the last hurdle.  If someone can put a fresh set of eys on it I would appreciate it.  I have 3 files -  a geocoder php script which I can't get to work, but it is exactly like the example given on Google except that I have my addresses broken down by street, city, state and zip.

- marker.php which is echoing the data correctly, and my map

- location.html which pulls up a map but no markers. 

 

my geocoder script is below:

<?php

define("MAPS_HOST", "maps.google.com");
define("KEY", "mykey");

// Opens a connection to a MySQL server
$connection = mysql_connect("server", "user","pwd");
if (!$connection) {
  die("Not connected : " . mysql_error());
}

// Set the active MySQL database
$db_selected = mysql_select_db("Db", $connection);
if (!$db_selected) {
  die("Can\'t use db : " . mysql_error());
}

// Select all the rows in the markers table
$query = "SELECT * FROM table WHERE 1";
$result = mysql_query($query);
if (!$result) {
  die("Invalid query: " . mysql_error());
}

// Initialize delay in geocode speed
$delay = 0;
$base_url = "http://" . MAPS_HOST . "/maps/geo?output=xml" . "&key=" . KEY;

// Iterate through the rows, geocoding each address
while ($row = @mysql_fetch_assoc($result)) {
  $geocode_pending = true;

  while ($geocode_pending) {
    $street = $row["street"];
    $city = $row["city"];
    $state = $row["state"];
    $zip_code = $row["zip_code"];
    $id = $row["id"];
    $request_url = $base_url . "&q=" . urlencode($street + $city + $state + $zip_code);
    $xml = simplexml_load_file($request_url) or die("url not loading");

    $status = $xml->Response->Status->code;
    if (strcmp($status, "200") == 0) {
      // Successful geocode
      $geocode_pending = false;
      $coordinates = $xml->Response->Placemark->Point->coordinates;
      $coordinatesSplit = split(",", $coordinates);
      // Format: Longitude, Latitude, Altitude
      $lat = $coordinatesSplit[1];
      $lng = $coordinatesSplit[0];

      $query = sprintf("UPDATE table " .
             " SET lat = '%s', lng = '%s' " .
             " WHERE id = '%s' LIMIT 1;",
             mysql_real_escape_string($lat),
             mysql_real_escape_string($lng),
             mysql_real_escape_string($id));
      $update_result = mysql_query($query);
      if (!$update_result) {
        die("Invalid query: " . mysql_error());
      }
    } else if (strcmp($status, "620") == 0) {
      // sent geocodes too fast
      $delay += 100000;
    } else {
      // failure to geocode
      $geocode_pending = false;
      echo "Address " . $street . " failed to geocoded. ";
      echo "Received status " . $status . "
\n";
    }
    usleep($delay);
  }
}
?>

 

my html file looks like the following:

<!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>location</title>

    <script src="http://maps.google.com/maps?file=api&v=2&key=mykey" 

       type="text/javascript"></script>

    <script type="text/javascript">

    

    var iconFlag = new GIcon(); 

    iconFlag.image = 'images/redflag.png';

    iconFlag.iconSize = new GSize(12, 20);

    iconFlag.iconAnchor = new GPoint(6, 20);

    iconFlag.infoWindowAnchor = new GPoint(5, 1);

    

    var iconBlue = new GIcon(); 

    iconBlue.image = 'images/glossy-ball.png';

    iconBlue.iconSize = new GSize(12, 20);

    iconBlue.iconAnchor = new GPoint(6, 20);

    iconBlue.infoWindowAnchor = new GPoint(5, 1);



    var iconRed = new GIcon(); 

    iconRed.image = 'images/redbutton.png';

    iconRed.iconSize = new GSize(12, 20);

    iconRed.iconAnchor = new GPoint(6, 20);

    iconRed.infoWindowAnchor = new GPoint(5, 1);



    var customIcons = [];

    customIcons["Db1"] = iconFlag;

    customIcons["Db2"] = iconBlue;

    customIcons["Db3"] = iconRed;



    function load() {

      if (GBrowserIsCompatible()) {

        var map = new GMap2(document.getElementById("map"));

        map.addControl(new GSmallMapControl());

        map.addControl(new GMapTypeControl());

        map.setCenter(new GLatLng(39.0000, 72.0000), 11);



        // Change this depending on the name of your PHP file

        GDownloadUrl("marker.php", function(data) {

          var xml = GXml.parse(data);

          var markers = xml.documentElement.getElementsByTagName("markers");

          for (var i = 0; i < markers.length; i++) {

            var informant = markers[i].getAttribute("id");

            var firstname = markers[i].getAttribute("firstname");

               var lastname = markers[i].getAttribute("lastname");

            var phone = markers[i].getAttribute("phone");

            var zip_code = markers[i].getAttribute("zip_code");

            var type = markers[i].getAttribute("type");

            var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),

                                    parseFloat(markers[i].getAttribute("lng")));

            var marker = createMarker(point, id, firstname, lastname, street, city, state, zip_code, type);

            map.addOverlay(marker);

           }

        });

      }

    }



    function createMarker(point, id, firstname, lastname, street, city, state, zip_code, type) {

      var marker = new GMarker(point, customIcons[type]);

      var html = "<b>" + id + "</b> <br/>" + firstname + "<br/>" + lastname + "<br/>" + phone + "<br/>" + street + "<br/>" + city + "<br/>" + state + "<br/>" + zip_code +"<br/>" + type;

      GEvent.addListener(marker, 'click', function() {

        marker.openInfoWindowHtml(html);

      });

      return marker;

    }

    //]]>

  </script>

  </head>

<body background="images/bg2.jpg">

  <body onload="load()" onunload="GUnload()">

    <center><div id="map" style="width: 800px; height: 600px"></div></center>

  </body>

</html>

 

Thanks for any help

Link to comment
https://forums.phpfreaks.com/topic/255166-help-with-coding-on-google-map/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.