To make sure that every significant location is covered within the map viewport, you could use the map.fitBounds()
method.(为了确保每个重要位置都被地图视口覆盖,可以使用map.fitBounds()
方法。)
As per this doc , this method(根据此文档 ,此方法)
Sets the viewport to contain the given bounds.(将视口设置为包含给定的边界。)
I see that you are already implementing this method to display every users.(我看到您已经在实现此方法来显示每个用户。) That said, you just have to add the (what you call the) main address
on the bounds.(也就是说,您只需要在边界上添加(称为) main address
。)
To add the main address' coordinates to the bounds instance, you just have to use the bounds.extend()
method.(要将主地址的坐标添加到bounds实例,只需使用bounds.extend()
方法。)
I checked the code on the site and here's what you can do to achieve this:(我检查了网站上的代码,这是实现此目的的方法:)
Make the variable bounds
a global variable by moving it above your initMap()
function.(通过将变量bounds
移到initMap()
函数上方,使它成为全局变量。)
On your codeAddress()
function, add the main address
' coordinates by extending the bounds
.(在您的codeAddress()
函数上,通过扩展bounds
添加main address
的坐标。) Here's a code snippet for that:(这是一个代码片段:) // Add the main address' coordinates by using bounds.extend method // results[0].geometry.location contains the main address' coordinates bounds.extend(results[0].geometry.location);
Move the map.fitBounds()
method just below the bounds.extend()
method on Step #2.(移动map.fitBounds()
正下方的方法bounds.extend()
上步骤#2方法。) // fits map map.fitBounds(bounds);
Here's a working fiddle that's based on your code.(这是根据您的代码工作的小提琴 。) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…