简体   繁体   English

API中的许多请求与许多单独的请求

[英]Many requests in an API vs Many separate requests

I am making an application based on Google maps API. 我正在基于Google Maps API制作应用程序。 This requires requesting for distance between two cities. 这需要请求两个城市之间的距离。 Now I want distances between many cities. 现在我想要许多城市之间的距离。

So should I use "for loop" and make many requests separately or should I send all the cities names in one link. 因此,我应该使用“ for循环”并分别发出多个请求,还是应该在一个链接中发送所有城市的名称。 Which one will work faster? 哪一个会更快? And which one will be better? 哪一个会更好?

For sure you should avoid sending multiple requests, because each roundtrip to a server takes time. 当然,应避免发送多个请求,因为每次往返服务器都需要时间。

However when you are grouping many requests this can also take a long time (both to send, and to process on the server), and affect the user experience (long waiting time). 但是,当您对许多请求进行分组时,这也可能要花费很长时间(发送和在服务器上进行处理),并且会影响用户体验(较长的等待时间)。

In your case I suspect that the "for loop" will not load to a lot of data, and server side processing will also not be too heavy, so sending a grouped single request should be the way to go. 对于您的情况,我怀疑“ for循环”将不会加载大量数据,并且服务器端处理也不会太繁琐,因此发送分组的单个请求应该是解决之道。

You can use the "DirectionService" sevices ,which is providing by Google ie "api3". 您可以使用Google提供的“ DirectionService”服务,即“ api3”。 You can find the distance between the Many cities ,it takes one origin point ,destination point and 8 way points (total 10 places) for one request and it provides a JSON file in return ,which contains all the information (distance in KM,value meters,city names and lot more) .Please check this link, https://developers.google.com/maps/documentation/javascript/directions . 您可以找到许多城市之间的距离,对于一个请求,它需要一个起点,目的地点和8个航路点(总共10个地方),并且它会返回一个JSON文件,其中包含所有信息(以KM为单位的距离,值米,城市名称等)。请检查此链接, https://developers.google.com/maps/documentation/javascript/directions i hope this answer will meet your requirement,otherwise don't mind. 我希望这个答案能满足您的要求,否则不要介意。

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

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