dpgui8229808 2015-07-24 15:34
浏览 58
已采纳

从SQL数据库生成映射的Javascript文件

I am working on created a Leaflet map that has lots and lots of markers. To make keeping the map up to date easier I have all the markers stored in a MySQL database. I have a php file that pulls the data and puts it in a form that javascript can read and then assigns it to a variable.

The problem: I want to then use some method (from what I've been reading AJAX is the safest and best method) to pull the variable from the php file and give it to the .js file to use.

I've read many different questions regarding how to do this and I just can't seem to understand exactly how I use AJAX to pull the variable. I don't know how to do this in Ajax.

Here are some code examples of what i'm trying to do:

get-data.php

//do some php stuff here to pull data
$data = json_encode($datapulledfromDB);

marker.js

//do some ajax here to pull $data

//now here's my javascript that then uses $data
markerLayer = L.geojson(data, {
//e.t.c.

mapindex.php

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

<script>
map.addLayer(markerLayer);

And just wanted to say that my whole experience creating this map has been wonderful, I've learned a lot and that's mainly because of StackOverflow, I have a problem and I come here and find so many resources that allow me to actually learn what I'm doing wrong. But this problem just has me tearing my hair out!

  • 写回答

1条回答 默认 最新

  • dongwei7245 2015-07-24 15:48
    关注

    Your get-data.php have to return the value you retrieved form the database. This can be done e.g. by using echo :

    $data = json_encode($datapulledfromDB);
    echo $data;
    

    Then you can e.g. use JavaScript code like:

    <script type="text/javascript">
    var result = null;
    $.get("path/to/get-data.php", function(data) {
        result = data;
    });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题