如何避免使用Google Maps API调用服务器获取重复数据 [英] How to avoid calling server for duplicate data using using Google Maps API

查看:93
本文介绍了如何避免使用Google Maps API调用服务器获取重复数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,用户可以使用Google Maps API滚动浏览Google Map,以查看放置在地图上的各种标记和其他数据.每当用户发出dragend事件时,我都会向服务器发送请求,将地图中心的当前经度/纬度以及半径值传递给它.我的服务器获取了这些经度,经度和半径值,并查询数据库以查看是否应在地图上显示任何相关对象.如果有,它们将返回浏览器,并在地图上呈现.

Within my application, users can scroll around a Google Map to see various markers and other data that have been placed on the map using the Google Maps API. Whenever the user emits the dragend event, I send a request to my server passing it the current lat/lng of the center of the map, as well as a radius value. My server takes these lat, lng, and radius values and queries the database to see if there are any relevant objects that should be displayed on the map. If there are, they are returned to the browser, and rendered on the map.

我试图避免每次用户拖动地图时都向我的服务器打电话.基本上,如果将要进行的呼叫不包括任何新点,则不应进行呼叫.

I'm trying to avoid making a call to my server every single time the user drags the map. Basically, if the call that would be made would not include any new points, the call should not be made.

我试图在下图中直观地显示我的意思:

I tried to show visually what I mean in the image below:

蓝色实心圆圈表示由于用户将地图放置在每个圆圈的中心而已被查询的区域.当他们这样做时,将调用服务器,并返回由实心蓝色圆圈覆盖的区域的数据.

The filled in blue circles represent areas that have already been queried due to the user positioning the map in the center of each circle. When they do this, a call is made to the server, and data for the area covered by the filled in blue circle is returned.

当它们在地图上拖动时,所有这些蓝色区域的并集表示已经从服务器中检索到数据的位置.现在,红色小圆圈表示一个位置,如果用户要放置地图,我会要调用服务器来加载新数据.这是因为由该中心点和半径组成的整个区域已被检索;它已经是蓝色区域的一部分.

As they drag around the map, the union of all of these blue areas represents locations for which data has already been retrieved from the server. Now, the small red circle represents a position where if the user were to position the map, I would not want to call the server to load new data. That's because the entire area made up by that center point and radius has already been retrieved; it's already part of the blue area.

另一方面,绿色位置代表我想要要调用服务器以获取数据的位置,因为由中心点和半径组成的某些区域不在蓝色区域.

On the other hand, the green position represents a location for which I would want to call the server to get data, since some of the area made up by the center point and radius fall outside of the blue area.

我想知道是否有人有解决此问题的方法.这让我感到难过.

I'm wondering if anyone has any approaches for solving this problem. It's got me stumped.

推荐答案

解决方案很简单.

1)用户拖动事物或其他任何东西.在后端,将该位置存储在会话或数据库中.照常加载响应.

1) User drags the thing or whatever.. On the backend, store that location in a session or a database. Load the response as usual.

2)用户再次执行任何操作.这次,从后端检查之前保存的会话位置,计算新位置与上次通话中保存的位置之间的距离.您可以为此使用 Google的API .如果该距离小于所使用的半径,则说明会存在一些重叠,可以在将数据返回到前端之前过滤掉这些结果.

2) User does whatever again. This time, on the back end check the saved session location from before, calculate the distance between the new position and the one you saved during the last call. You can use Google's API for this. If the distance is smaller than the radius used, you know there will be some overlap and you can filter out those results before returning the data to the front end.

这将减少PHP仅给新的结果.如果要避免一起进行Ajax调用,唯一的方法是将整个数据库加载到javascript中.您希望javascript如何知道是否有任何行.

This will reduce the results PHP gives to only the new ones. If you want to avoid making an ajax call alltogether the only way to do that is to load the entire database into javascript. How else would you expect javascript to know if there are any rows.

这篇关于如何避免使用Google Maps API调用服务器获取重复数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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