简体   繁体   English

有关Google Maps API的问题

[英]question regarding google maps api

if im loading data for the markers from a database do i write the output queried from the db into a javascript file or is there a cleaner way of doing it? 如果我从数据库中加载标记的数据,我是否将从db查询的输出写到javascript文件中,或者是否有更清洁的方法?

thanks 谢谢

Yeah, writing to a file is a good way to do it. 是的,写入文件是一种很好的方法。 Just write the data as JSON. 只需将数据写为JSON。 Your file would look like: 您的文件如下所示:

var map = {waypoints:[...]};

And then you can do: 然后您可以执行以下操作:

for(var i=o; i<map.waypoints.length; ++i) {
  addWaypoint(map.waypoints[i]);
}

I actually do some static caching of nodes using this method: http://www.trailbehind.com/site_media/javascript/gen/national-parks.js 我实际上使用此方法对节点进行了一些静态缓存: http : //www.trailbehind.com/site_media/javascript/gen/national-parks.js

We use that set of National Parks a lot, so we cache it. 我们经常使用该组国家公园,因此我们对其进行缓存。 But we also have urls where you can fetch JSON for a node on the fly, such as: http://www.trailbehind.com/map/node/7538973/632/735/ 但是,我们还有一些网址,您可以在其中动态获取节点的JSON,例如: http : //www.trailbehind.com/map/node/7538973/632/735/

This URL gets the map for node 7538973, and specifies the dimensions of their map in pixels as well. 该URL获取节点7538973的地图,并以像素为单位指定其地图的尺寸​​。

The needed Javascript can of course be wrapped in whatever language you prefer to use, see eg pymaps for a Python example. 当然,所需的Javascript可以用您喜欢使用的任何语言包装,例如,参见pymaps中的Python示例。 While pymaps is actualally inserting the JS code into an HTML template, if you're writing a web app you can perfectly well choose to serve that JS code on the fly at an appropriate URL and use that URL in a <script> tag in your pages. 虽然pymaps实际上是将JS代码插入HTML模板中,但是如果您正在编写Web应用,则可以很好地选择以适当的URL即时提供该JS代码,并在您的<script>标记中使用该URL页面。

I agree with Andrew's answer (+1). 我同意安德鲁的答案(+1)。

I guess the only point I would add is that rather than including some server side generated JavaScript, you could use an AJAX request to grab that data. 我想我要补充的唯一一点是,您可以使用AJAX请求来获取该数据,而不是包含一些服务器端生成的JavaScript。 Something like: 就像是:

var request = new Request.JSON (url: 'get_some_json.php',
                           onSuccess: function(data) {
                               // do stuff with the data
                           }).get ();

(This is a Mootools AJAX thing, but you could use any kind of AJAX request object). (这是Mootools AJAX的东西,但是您可以使用任何种类的AJAX请求对象)。

Edit : ChrisB makes a good point about the performance of parsing JSON responses and re-reading my answer I certainly didn't make myself clear. 编辑ChrisB很好地说明了解析JSON响应并重新读取我的答案的性能,我当然并不清楚。 I think AJAX requests are suitable for re-requesting data based on parameters generated by user interaction. 我认为AJAX请求适合根据用户交互生成的参数重新请求数据。 I guess an example use case might be, a user filtering the data displayed on the map. 我想一个示例用例可能是,用户过滤了地图上显示的数据。 You might grab the filtered data via an AJAX/SJON request rather than re-loading the page. 您可以通过AJAX / SJON请求获取经过过滤的数据,而不是重新加载页面。

Depending on the size of your application, you may want to consider printing out plain javascript. 根据您的应用程序的大小,您可能需要考虑打印纯JavaScript。

I have a map that uses server-side clustering, so markers update frequently. 我有一个使用服务器端集群的地图,因此标记会经常更新。 I found that parsing JSON markers slowed the app significantly, and simply wasn't necessary. 我发现解析JSON标记会大大降低应用程序的速度,根本没有必要。

If speed is an issue, I'd suggesting removing all of the unnecessary layers possible (JSON, AJAX, etc.). 如果速度是一个问题,我建议您删除所有可能的不必要层(JSON,AJAX等)。 If it's not, you'll be just fine with JSON, which is cleaner. 如果不是,那么使用JSON就可以了,它更干净。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM