最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting data from SQL into Leaflet - Stack Overflow

programmeradmin0浏览0评论

I am trying to pull data from a database and then map it using Leaflet. My hunch is that directly querying my database with Javascript isn't a good idea (because I found another stackoverflow answer that said "that isn't a good idea), but I'm not sure what a good alternative might be.

I'm extremely new to Javascript (and Leaflet) and moderately capable with SQL. I do know some PHP and XML if I need to make some intermediate querying page (and then Leaflet grabs that info...?)

I am trying to pull data from a database and then map it using Leaflet. My hunch is that directly querying my database with Javascript isn't a good idea (because I found another stackoverflow answer that said "that isn't a good idea), but I'm not sure what a good alternative might be.

I'm extremely new to Javascript (and Leaflet) and moderately capable with SQL. I do know some PHP and XML if I need to make some intermediate querying page (and then Leaflet grabs that info...?)

Share Improve this question asked Mar 4, 2013 at 19:44 EricaErica 2,4775 gold badges29 silver badges34 bronze badges 3
  • @JayWalker: It's largely been googling -- AJAX seems like a possibility, although it may not be able to execute a stored procedure that would create the XML files? This is the SO question: link. – Erica Commented Mar 4, 2013 at 20:10
  • 1 You don't want to embed the connection string in the client. You could expose the data via OData so it can be easily consumed by javascript. See this post: stackoverflow./questions/10112376/… – Jay Walker Commented Mar 4, 2013 at 20:19
  • Thank you Jay -- the connection string security was my biggest worry, so that was the answer I needed. Now to go learn how to do it :-) – Erica Commented Mar 4, 2013 at 20:28
Add a ment  | 

1 Answer 1

Reset to default 6

Apart from the issue of directly querying from your db with javascript...

Once you get the data from your db you'll want to loop through each data point and add it to the map. For example, if you had your 'intermediate querying page' return a JSON formatted object, then you could do something like this:

$.getJSON('path-to/intermediate-querying-page.php', function(data) {

    $.each(data, function(key, place) {
        var marker = L.marker([place.latitude, place.longitude]).addTo(map);
    });

});
发布评论

评论列表(0)

  1. 暂无评论