开发者

Putting mysql stored points on a google map

开发者 https://www.devze.com 2023-04-05 15:48 出处:网络
I have a MySQL database with a table full of geographic points, latitudes and longitudes. I want thes开发者_开发知识库e coordinates displayed on a Google map as points. Is it possible for JavaScript t

I have a MySQL database with a table full of geographic points, latitudes and longitudes. I want thes开发者_开发知识库e 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?


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.


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>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消