A Database Driven Earth App: Using PHP & MySQL with the Earth API

687 views
Skip to first unread message

jishin

unread,
Jun 1, 2012, 4:32:52 AM6/1/12
to google-earth-...@googlegroups.com
Dear Friends,
This is the first example for me try to put place-marker into the Google earth map on then internet
I follow the website for 2 days what they said  I have done. But still could not achieved to see place markers on the map.
The link of example which I used as follows.

https://developers.google.com/earth/articles/phpsqlearth#next

the xml2 can access to mysql database and can read data as follows
<markers>
<marker name="Pan Africa Market" address="1521 1st Ave, Seattle, WA" lat="47.608940" lng="-122.340141" type="restaurant"/>
<marker name="Buddha Thai & Bar" address="2222 2nd Ave, Seattle, WA" lat="47.613590" lng="-122.344391" type="bar"/>
<marker name="The Melting Pot" address="14 Mercer St, Seattle, WA" lat="47.624561" lng="-122.356445" type="restaurant"/>
<marker name="Ipanema Grill" address="1225 1st Ave, Seattle, WA" lat="47.606365" lng="-122.337654" type="restaurant"/>
<marker name="Sake House" address="2230 1st Ave, Seattle, WA" lat="47.612823" lng="-122.345673" type="bar"/>
<marker name="Crab Pot" address="1301 Alaskan Way, Seattle, WA" lat="47.605961" lng="-122.340363" type="restaurant"/>
<marker name="Mama&apos;s Mexican Kitchen" address="2234 2nd Ave, Seattle, WA" lat="47.613976" lng="-122.345467" type="bar"/>
<marker name="Wingdome" address="1416 E Olive Way, Seattle, WA" lat="47.617214" lng="-122.326584" type="bar"/>
<marker name="Piroshky Piroshky" address="1908 Pike pl, Seattle, WA" lat="47.610126" lng="-122.342834" type="restaurant"/>
</markers>
then
mapfile can draw map but could not draw placemarkers on the map.
here is the map file also


<!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>Google Earth API + mySQL/PHP Example</title>
    <script src="https://www.google.com/jsapi?key=AIzaSyA0L5EpH3B3DGV6kaRWBBOwYa6SY8jPJDk"> </script>
    <script>
var ge;

google.load("earth", "1");
google.load("maps", "2");
function init() {
  google.earth.createInstance('map3d', initCallback, failureCallback);
}

function initCallback(instance){
  ge = instance;
  ge.getWindow().setVisibility(true);
  var la = ge.createLookAt('');
  la.set(47.616319, -122.349992, 25, ge.ALTITUDE_RELATIVE_TO_GROUND,
         -10, 60, 4000);
  ge.getView().setAbstractView(la);
  ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);
  ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true); 
  ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);
  GDownloadUrl("phpsqlajax_genxml3.php", function(data) {
  var xml = GXml.parse(data);
  var markers = xml.documentElement.getElementsByTagName("marker");
  for (var i = 0; i < markers.length; i++) {
    var name = markers[i].getAttribute("name");
    var address = markers[i].getAttribute("address");
    var type = markers[i].getAttribute("type");
    var lat = markers[i].getAttribute("lat");
    var lng = markers[i].getAttribute("lng");
    createPlacemark(name,address,type,lat,lng); 
    }
  });
}

function failureCallback(errorCode) {
}

function createPlacemark(name,address,type,lat,lng){
  var placemark = ge.createPlacemark('');
  placemark.setName(name);
  var point = ge.createPoint('');
  point.setLatitude(parseFloat(lat));
  point.setLongitude(parseFloat(lng));
  placemark.setGeometry(point);
  placemark.setStyleSelector(createIcon(type));
  ge.getFeatures().appendChild(placemark);
  placemark.setDescription(address);
  if(type=="bar"){
    icon.setHref('http://maps.google.com/mapfiles/kml/pal2/icon27.png');
  } else{
    icon.setHref('http://maps.google.com/mapfiles/kml/pal2/icon63.png');
  }
  var style = ge.createStyle('');
  style.getIconStyle().setIcon(icon);
}
</script>
  </head>

  <body onload="init()">
    <div id="map3d" style="width: 500px; height: 500px"></div>
  </body>
</html>


the point which I also say that when you click following hyperlink it does open different file from published one in the box on the internet page.

The full HTML that accomplishes this is shown below (phpsqlajax_map.htm):

This cause confusion on my mind which one could I use. I try both but stil could not draw placemarkers on the map. Please
Help me What I'm doing wrong.
Also xml file creater php code I used as follows and as I said it works well.
<?php
require("phpsqlajax_dbinfo.php");

function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','&lt;',$htmlStr);
$xmlStr=str_replace('>','&gt;',$xmlStr);
$xmlStr=str_replace('"','&quot;',$xmlStr);
$xmlStr=str_replace("'",'&apos;',$xmlStr);
$xmlStr=str_replace("&",'&amp;',$xmlStr);
return $xmlStr;
}

// Opens a connection to a mySQL server
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
  die('Not connected : ' . mysql_error());
}

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

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

header("Content-type: text/xml");

// Start XML file, echo parent node
echo '<markers>';

// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  // ADD TO XML DOCUMENT NODE
  echo '<marker ';
  echo 'name="' . parseToXML($row['name']) . '" ';
  echo 'address="' . parseToXML($row['address']) . '" ';
   echo 'lat="' . $row['lat'] . '" ';
   echo 'lng="' . $row['lng'] . '" ';
  echo 'type="' . $row['type'] . '" ';
  echo '/>';
}

// End XML file
echo '</markers>';

?>



Rossko

unread,
Jun 2, 2012, 6:56:26 AM6/2/12
to KML Developer Support - Google Earth Plug-in
> I follow the website for 2 days what they said  I have done.

Not much point in pasting the example code here. How about sharing
the URL to your map that does not work?
Reply all
Reply to author
Forward
0 new messages