0

I have a MySQL database with a table full of geographic points, latitudes and longitudes. I want these coordinates displayed on a Google map as points. Is it possible for JavaScript to directly access the database or would I need to do that first using PHP?

1
  • You'll need a server side script/program to interact with the database, so using PHP would be the way to go. Commented Sep 16, 2011 at 18:58

3 Answers 3

2

Yes, it's possible and you'd have to use PHP to retrieve your points from the database. For examples of the Google part of your request, have a look at the Google Maps Javascript API V3 reference and update your question when you've put some code together.

Sign up to request clarification or add additional context in comments.

1 Comment

Here's a more direct link to the code samples from the API reference: code.google.com/apis/maps/documentation/javascript/examples/…
0

You would need some sort of server side language to be involved. Likely ajax/json request or pulling an XML file and looping through the data. If you're more comfortable with PHP you would loop through your results within a script tag:

<script type="text/javascript">

    var mapArray = new Array;

    <?php

        $i = 0;

        $result = mysql_query('SELECT * FROM location');

        while ($row = mysql_fetch_assoc($result)) {

            echo 'mapArray[' .$i++ . '] = new Array(' . $row['lat'] . ',
                         ' . $row['lng'] . '");

        ';
        }

    ?>

    for (var i in mapArray) {

        var myLatLng = new GLatLng(mapArray[i][0], mapArray[i][1]);
        GMarker(myLatLng);
    }
</script>

Comments

0

It is possible to use a PHP script as a link between JavaScript and the SQL database witht he use of the JavaScript XMLHttpRequest object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.