redfrogsbinary
redfrogsbinary

Reputation: 629

Putting mysql stored points on a google map

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?

Upvotes: 0

Views: 1192

Answers (3)

Steve Robbins
Steve Robbins

Reputation: 13822

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>

Upvotes: 0

stealthyninja
stealthyninja

Reputation: 10371

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.

Upvotes: 2

Sceak
Sceak

Reputation: 21

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.

Upvotes: 0

Related Questions