边界变化时,如何动态地将点添加到Google Map? [英] How do I dynamically add points to a Google Map when the bounds change?

查看:187
本文介绍了边界变化时,如何动态地将点添加到Google Map?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var latlng = new GLatLng(lat,lng);我可以在地图窗口中放置静态点: 
map.addOverlay(new GMarker(latlng,markerOptions));

我知道如何获得窗口的边界。我也可以编写一个页面,它将采用任何格式并返回它们内部的点。



我现在遇到的问题:


  1. moveend 事件捕获所有移动,包括缩放更改?或者我需要单独监视该事件?

  2. 我应该如何调用外部数据源? (我应该做一个JQuery $。get request?)

  3. 该数据源应该返回什么?

  4. 如何在地图上显示该数据?


解决方案

在我的一个网站中使用。我认为这正是你正在寻找的。但要注意的是,gmaps-utility-library插件有一些bug /错误代码,所以最好仔细检查一下,仔细检查一切是否正常运行(我在标记管理器中没有遇到任何错误,但在某些其他来自该库的插件)。



以下是 reference and examples



即使你想自己编码,这也是一个很好的起点。 >

编辑

我的答案是3-4:

这真的取决于情况。如果它是静态的(你只需要在map> 300上管理很多点),那么你可以将所有的点与地图所在的页面一起提供(例如JavaScript数组)。如果用户与数据交互,那么最好使用AJAX。如果你在你的网站中使用jQuery(或任何其他JS库),那么如果没有,那么使用该库中的ajax函数,然后使用来自gmaps的函数。这是因为在整个站点中使用相同的AJAX函数非常好,而不是使用2来完成相同的工作。



如果您采用AJAX路径,您有两个选项: p>


  1. 在一次请求中加载整个地图的所有标记。 小的利润。

如果您希望用户希望看到大图或他希望看到所有/大部分要点然后去选择1 +标记管理器(如我推荐的或自己类似的)。



如果真的有很多点,用户永远不会对大多数感兴趣然后去选项2 +管理器或只是这个简单的算法:清除地图 - >请求地图窗口边界点+ magin /(缓存) - >绘制点 - >为每个移动/缩放重复。



从个人经验(我使用过两种AJAX选项),标记管理器确实相当不错,可以处理很多点,而且整体用户体验很多更轻松,然后加载点只是视口。请求新点并在地图上绘制它们相当滞后/不连贯。当然这取决于点数。

I can currently place static points in the map window:

var latlng = new GLatLng(lat, lng);
map.addOverlay(new GMarker(latlng, markerOptions));

And I know how to get the bounds of the window. I can also code a page that will take the bounds and return the points that are inside them in any format.

The problems I have now:

  1. Does the moveend event capture all moves, including zoom changes? Or do I need to watch for that event separately?
  2. How should I call my external datasource? (should I just do a JQuery $.get request?)
  3. What should that datasource return?
  4. How do I display that data on the map?

解决方案

I'm using this in one of my sites. I think it's exactly what you are looking for. But be warned "gmaps-utility-library" plugins have some buggy/bad code so it's a good idea to go over it and double check if everything is working as it should (I haven't encountered any bugs in marker manager but in some other plugins from that library).

Here's reference and examples.

Even if you want to code your own this one is probably a good starting point.

EDIT

My answer for 3-4:

It really depends on situation. If it's static (you just need to manage a lot points on map > 300) then you could serve all points together with the page where the map is located in (as JavaScript array for example). If user interacts with data then probably it's better to use AJAX. If you use jQuery (or any other JS library) in your site then use ajax function from that library if not then go with the one which comes from gmaps. It's because it's nice to use same AJAX function in whole site instead of using 2 which does the same job.

If you are taking AJAX path you have 2 options:

  1. Load all markers for whole map in one request.
  2. Load markers that shows up on user screen + small margin.

If you expect that user wants to see the big picture or that he will want to see all/most of the points then go for option 1 + marker manager (like the one I recommended or your own similar).

If there's really a lot of points and user will never be interested in most of them then go for option 2 + manager or just this simple algorithm: Clear map -> Request points for map window bounds + magin / (Cache) -> Draw points -> Repeat for each move/zoom.

From personal experience (I've used both AJAX options) marker manager does quite nice job, can handle a lot points and overall user experience is a lot smoother then loading points for just viewport. Requesting new points and drawing them on map is quite laggy/choppy. Of course it depends on amount of points.

这篇关于边界变化时,如何动态地将点添加到Google Map?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆