Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7447327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:34:38+00:00 2026-05-29T12:34:38+00:00

The code bellow tries to ask the user to upload kml file and then

  • 0

The code bellow tries to ask the user to upload kml file and then saves the file at the same time it shows the kml data in google maps using google maps api v2. I could successfully take the kml file and saves it at a specific directory, HOWEVER, i am facing some problem in displaying the file on google maps immediately after the user clicks on KML TEST button. The error is Uncaught ReferenceError: showKML is not defined

Here is the code that I used

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Find latitude and longitude with Google Maps</title>   
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAgrj58PbXr2YriiRDqbnL1RSqrCjdkglBijPNIIYrqkVvD1R4QxRl47Yh2D_0C1l5KXQJGrbkSDvXFA"
      type="text/javascript"></script>  

    <?php
    $upload = $_SERVER['PHP_SELF'];
    if(isset($_POST['kmltest'])) {
    $target_path = "uploads/";
    $fn =  basename( $_FILES['uploadedfile']['name']);

    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
    //echo $target_path ;
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
    echo "<script type=\"text/javascript\"> showKML(); </script>";


    } 
    else{
    echo "There was an error uploading the file, please try again!";
    }


}
?>

    <script src="egeoxml.js" type="text/javascript"></script>
    <script  type="text/javascript">

    var map;
    var options = {};
    var shapeCounter_ = 0;


        function initialize() {
        if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById("map"));
            map.setCenter(new GLatLng(25.22903, 55.46612), 13);
            map.addControl(new GSmallMapControl());
            map.addControl(new GMapTypeControl());
            map.clearOverlays();
            document.getElementById("lat").value = "25.22903";
            document.getElementById("lng").value = "55.46612";
        }
    }




    function startShape() {
        initialize();
        document.getElementById('lat').disabled = true;
        document.getElementById('lng').disabled = true;
        var polygon = new GPolygon([],"ff0000", 2, 0.7,"ff0000",0.2);
        startDrawing(polygon, "Shape " + (++shapeCounter_), function() {
            var cell = this;
            var area = polygon.getArea();
            cell.innerHTML = (Math.round(area / 10000) / 100) + "km<sup>2</sup>";
            });
        showcoor(polygon);
    }

    function startDrawing(poly, name, onUpdate) {
        map.addOverlay(poly);
        poly.enableDrawing(options);
        poly.enableEditing({onEvent: "mouseover"});
        poly.disableEditing({onEvent: "mouseout"});
        GEvent.addListener(poly, "endline", function() {
            GEvent.addListener(poly, "click", function(latlng, index) {
                if (typeof index == "number") {
                    poly.deleteVertex(index);
                }    
            });
        });    
    }

    function showcoor (poly) {
        GEvent.addListener(poly, "endline", function() {
            GEvent.addListener(poly, "click", function() {
                var str= "" ;
                for (var i = 0, I = this.getVertexCount(); i < I; ++i) {
                    var xy = this.getVertex(i);
                    str += xy.lat() + ', ' + xy.lng() + '<br />';
                }
                alert (str);
            });
        });
    }

    function drawpoint() {
        //initialize();
        document.getElementById('lat').disabled = false;
        document.getElementById('lng').disabled = false;
        var lat = document.getElementById('lat').value;
        var lng = document.getElementById('lng').value;
        var center = new GLatLng(parseFloat(lat),   parseFloat (lng));
        map.setCenter(center, 7);
        geocoder = new GClientGeocoder();
        var marker = new GMarker(center, {draggable: true});  
        map.addOverlay(marker);
        GEvent.addListener(marker, "dragend", function() {
            var point = marker.getPoint();
            map.panTo(point);
            document.getElementById("lat").value = center.lat().toFixed(5);
            document.getElementById("lng").value = center.lat().toFixed(5);
        });
        GEvent.addListener(map, "moveend", function() {
            map.clearOverlays();
            var center = map.getCenter();
            var marker = new GMarker(center, {draggable: true});
            map.addOverlay(marker);
            document.getElementById("lat").value = center.lat().toFixed(5);
            document.getElementById("lng").value = center.lng().toFixed(5);
            GEvent.addListener(marker, "dragend", function() {
                var point =marker.getPoint();
                map.panTo(point);
                document.getElementById("lat").value = point.lat().toFixed(5);
                document.getElementById("lng").value = point.lng().toFixed(5);
            });
        });
    }

    function showKML() {
    //alert(filename);
        initialize();
        document.getElementById('lat').disabled = true;
        document.getElementById('lng').disabled = true;
        var exml;
        exml = new EGeoXml("exml", map, ("uploads/test.kml"));
        exml.parse();
        exml.show(); 
    }



    </script>


    </head> 
<body onload="initialize()" onunload="GUnload()" >
 <form action = <?php echo $upload; ?> method = "post"  enctype="multipart/form-data"/>
 <p align="left">

 <table>
  <tr>
    <td><b>Latitude</b></td>
    <td><b>Longitude</b></td>
  </tr>
  <tr>
    <td> 
    <input type="text" name="lat" id="lat" /></td>
    <td>
    <input type="text" name="lng" id="lng" /></td>
    <td>
    <input type="button" name="point" id="point" value="Point" onclick="drawpoint()" /><td>

    <input type="button" name="shape_b" id="shape_b" value="Poly" onclick="startShape()" /><td>



    <input type="submit" name="kmltest" id="kmltest" value="KML TEST" /> </td></tr>
    <tr>
    <td>
     <input type="file" name="uploadedfile" id="uploadedfile" />
    </td>
    </tr>



</table>
 </p>
  <p>
  <div align="center" id="map" style="width: 600px; height: 400px"><br/></div>
   </p>

  <script type="text/javascript">
//<![CDATA[
if (typeof _gstat != "undefined") _gstat.audience('','pagesperso-orange.fr');
//]]>
</script>

</form>

</body>

</html>

your assistance is highly appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T12:34:40+00:00Added an answer on May 29, 2026 at 12:34 pm

    JavaScript <script> blocks are evaluated in blocks.

    Within the same block, a function can be called before it’s defined, because function declarations are evaluated before the rest of the code.

    However, when you move function showKML(){} to a different block, the function is called before it’s defined, hence the error.

    To solve the problem, you have to move the function declaration before the <?php .. ?> block, where showKML is called.

    A JSFiddle to demonstrate the problem: http://jsfiddle.net/SGbfu/1/


    More detailled explanation:

    1. When <script> and </script> is encountered, the code between is directly evaluated.
    2. All functions, declared using function x(){} are evaluated first. Then, the rest of the code is considered.
    3. Step 1-2 is repeated, till all <script> blocks in the document are evaluated.

    Example:

    <script>
    f(); // No error, f is declared below
    function f(){ alert('f() called'); }
    g(); // Error: g is not defined before, or within this block
    </script>
    <script>
    function g(){}
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below tries to parse a file containing these 3 lines: 0 2
I want to open an xlsx file, I have tried the below code,but neither
Below is my code, i tried to open my excel file in my c#
The code bellow is the small starting point for a new way ( for
Given the code bellow, how do I style the radio buttons to be next
I'm using iphone cookbook code to prompt a user for text in a UIAlertView.
I have this code to load a config file and read all the values
I ask for a user to input 4 numbers or letter from 1 -
I want to animate two jquery OBJECTS at the same time (using the jquery
I am having problems with template class specialization, see the code bellow, please. template

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.