Google Maps API/v2 applications with PHP.
Results 1 to 7 of 7

Thread: Google Maps API/v2 applications with PHP.

  1. #1
    PHP Citizen AliEzz's Avatar
    Join Date
    Jun 2011
    Location
    Egypt, Cairo
    Posts
    7

    Google Maps API/v2 applications with PHP.

    Here is the source code for the book: googlemapsbook.com/gmaps_php_code.tar.gz
    I just run the source code of CHP 2 & 3 as they are ... copied then pasted.

    I started to run CHP 2 's source code. It works fine, but an XML Error appears.
    (The same error appear when i run CHP 3 's source code)

    I need someone familiar with Google Maps Applications.
    Ofcourse if you dont have a Google Maps API Key ... please obtain one from google .. its free.
    Change the API Key in the source code file.

  2. #2
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,390
    Uh, did you happen to take a look at the homepage of the site you linked to?

    Not only do they warn you that their book is "severely out of date", they even go on to reference someone else's book and to "NOT BUY [THEIRS]." All of this deprecation happened close to 3 years ago, so it's probably even more out of date...

    (Not to mention that after a quick peek at the source code, it doesn't look extremely well written in the first place - even for something released 5+ years ago.)

  3. #3
    PHP Citizen AliEzz's Avatar
    Join Date
    Jun 2011
    Location
    Egypt, Cairo
    Posts
    7
    The Sample code is fine written ... i need to know why this XML error keeps appearing.
    Try to copy and paste it .. you shall see the error.

    I need this solution very deeply

  4. #4
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    Quote Originally Posted by AliEzz View Post
    The Sample code is fine written ... i need to know why this XML error keeps appearing.
    I would submit that code which produces an error is not "fine." Further, I suspect you would agree, or you wouldn't be asking for help in the first place.

    Quote Originally Posted by AliEzz View Post
    Try to copy and paste it .. you shall see the error.
    I need this solution very deeply
    Why don't you *tell us* what the error is?

  5. #5
    PHP Citizen AliEzz's Avatar
    Join Date
    Jun 2011
    Location
    Egypt, Cairo
    Posts
    7
    My error is

    "XML Parsing Error: junk after document element
    Location: http://127.0.0.1/GoogleMaps/CH3/stor....4982866541374
    Line Number 2, Column 1:"

    The form is supposed to take the values and save it in an XML file.
    Although the the record is saved in the XML file, but this XML error appears to me as well

  6. #6
    PHP Citizen AliEzz's Avatar
    Join Date
    Jun 2011
    Location
    Egypt, Cairo
    Posts
    7
    These are the 4 files involved:

    map_functions.js
    var centerLatitude = 37.4419;
    var centerLongitude = -122.1419;
    var startZoom = 12;

    var map;

    function init() {
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);

    //Note: this call to retrieve markers is required for Listing 3-8
    retrieveMarkers();

    /* [listing 3-4] */
    GEvent.addListener(map, "click", function(overlay, latlng) {
    //only perform the click if the window is closed and the click was directly on the map.
    if(!overlay) {
    //create an HTML DOM form element
    var inputForm = document.createElement("form");
    inputForm.setAttribute("action","");
    inputForm.onsubmit = function() {storeMarker(); return false;};

    //retrieve the longitude and lattitude of the click point
    var lng = latlng.lng();
    var lat = latlng.lat();

    inputForm.innerHTML = '<fieldset style="width:150px;">'
    + '<legend>New Marker</legend>'
    + '<label for="found">Found</label>'
    + '<input type="text" id="found" style="width:100%;"/>'
    + '<label for="left">Left</label>'
    + '<input type="text" id="left" style="width:100%;"/>'
    + '<input type="submit" value="Save"/>'
    + '<input type="hidden" id="longitude" value="' + lng + '"/>'
    + '<input type="hidden" id="latitude" value="' + lat + '"/>'
    + '</fieldset>';

    map.openInfoWindow (latlng,inputForm);
    }
    });
    /* [listing 3-4 end] */

    }

    window.onload = init;

    /* [listing 3-5] */
    function storeMarker(){
    var lng = document.getElementById("longitude").value;
    var lat = document.getElementById("latitude").value;

    var getVars = "?found=" + document.getElementById("found").value + "&left=" + document.getElementById("left").value + "&lng=" + lng + "&lat=" + lat ;

    var request = GXmlHttp.create();

    //open the request to storeMakres.php on your server
    request.open('GET', 'storeMarker.php' + getVars, true);
    request.onreadystatechange = function() {
    if (request.readyState == 4) {
    //the request in complete

    var xmlDoc = request.responseXML;

    //retrieve the root document element (response)
    var responseNode = xmlDoc.documentElement;

    //retrieve the type attribute of the node
    var type = responseNode.getAttribute("type");

    //retrieve the content of the responseNode
    var content = responseNode.firstChild.nodeValue;

    //check to see if it was an error or success
    if(type!='success') {
    alert(content);
    } else {
    //Create a new marker and add it's info window.
    var latlng = new GLatLng(parseFloat(lat),parseFloat(lng));

    var marker = createMarker(latlng, content);

    map.addOverlay(marker);
    map.closeInfoWindow();
    }
    }
    }
    request.send(null);
    return false;
    }

    function createMarker(latlng, html) {
    var marker = new GMarker(latlng);
    GEvent.addListener(marker, 'click', function() {
    var markerHTML = html;
    marker.openInfoWindowHtml(markerHTML);
    });
    return marker;
    }
    /* [listing 3-5 end] */

    /* [listing 3-8] */
    function retrieveMarkers() {
    var request = GXmlHttp.create();

    //tell the request where to retrieve data from.
    request.open('GET', 'retrieveMarkers.php', true);

    //tell the request what to do when the state changes.
    request.onreadystatechange = function() {
    if (request.readyState == 4) {
    var xmlDoc = request.responseXML;

    var markers = xmlDoc.documentElement.getElementsByTagName("marker");
    for (var i = 0; i < markers.length; i++) {
    var lng = markers[i].getAttribute("lng");
    var lat = markers[i].getAttribute("lat");
    //check for lng and lat so MSIE does not error
    //on parseFloat of a null value
    if(lng && lat) {
    var latlng = new GLatLng(parseFloat(lat),parseFloat(lng));

    var html = '<div><b>Found</b> '
    + markers[i].getAttribute("found")
    + '</div><div><b>Left</b> '
    + markers[i].getAttribute("left")
    + '</div>';

    var marker = createMarker(latlng, html);
    map.addOverlay(marker);
    }
    } //for
    } //if
    } //function

    request.send(null);
    }
    /* [listing 3-8 end] */
    index.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" xmlns:v="urn:schemas-microsoft-com:vml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Final Map</title>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=type_in_your_key" type="text/javascript"></script>
    <script src="map_data.php" type="text/javascript"></script>
    <script src="map_functions.js" type="text/javascript"></script>
    <link href="style.css" rel="stylesheet" type="text/css" />
    <!--[if IE]><style type="text/css">v\:*{behavior:url(#default#VML);}</style><![endif]-->
    </head>
    <body>
    <div id="map"></div>
    </body>
    </html>
    storeMarker.php
    <?php
    header('Content-Type: text/xml;charset=UTF-8');

    //see Listing 3-9 for icon related items in the code

    $lat = (float)$_GET['lat'];
    $lng = (float)$_GET['lng'];
    $found = htmlspecialchars(strip_tags(utf8_encode($_GET['found'])));
    $left = htmlspecialchars(strip_tags(utf8_encode($_GET['left'])));
    $icon = htmlspecialchars(strip_tags(utf8_encode($_GET['icon'])));

    //Create an XML node
    $marker = <<<MARKER
    <marker lat="$lat" lng="$lng" found="$found" left="$left" icon="$icon"/>
    MARKER;

    //open the data.xml file for appending
    $f=@fopen('data.xml', 'a+');
    if(!$f) die('<?xml version="1.0"?>
    <response type="error"><![CDATA[Could not open data.xml file]]></response>
    ');

    //add the node
    $w=@fwrite($f, $marker);
    if(!$w) die('<?xml version="1.0"?>
    <response type="error"><![CDATA[Could not write to data.xml file]]></response>');

    @fclose($f);

    //return a response
    $newMarkerContent = "<div><b>Found</b> $found</div><div><b>Left</b> $left</div>";

    echo <<<XML
    <?xml version="1.0"?>
    <response type="success" icon="$icon"><![CDATA[$newMarkerContent]]></response>
    XML;
    ?>
    retrieveMarkers.php
    <?php
    header('Content-Type: text/xml;charset=UTF-8');
    $markers = file_get_contents('data.xml');
    echo <<<XML
    <markers>
    $markers
    </markers>
    XML;
    ?>

  7. #7
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    That error means that there is extra content outside of the root XML element. For example, this is valid:
    Code:
    <root>
       <item/>
       <other/>
       <whatever/>
    </root>
    this is not:
    Code:
    <root>
       <item/>
       <other/>
    </root>
    <root2>
       <whatever/>
    </root2>
    and neither is this:
    Code:
    <root>
       <item/>
       <other/>
    </root>
    <b>Warning:</b> PHP printed an error message
    I hope this helps you understand what's going on, but I'm with brad -
    don't use code that even the authors say shouldn't be used.
    Last edited by traq; 12-04-2012 at 10:59 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •