Google Maps API v3 - 地理编码结果问题带有界限 [英] Google Maps API v3 - Geocoder results issue with bounds
问题描述
目标:我想要一个自定义搜索(地理编码)功能,并能够列出并点击
每个结果并将其显示在地图上。
通缉:相应地调整地图边界/缩放级别,即搜索MA,USA应缩小地图让我看到整个马萨诸塞州,美国马萨诸塞州波士顿应放大波士顿地区。在列出多个结果时,点击结果时也应该如此。
问题:我可以使用带有fitBounds的geometry.bounds对象 - 但是 - 使用geocoder得到的一些结果没有geometry.bounds对象。
一个简单的例子:搜索Boston返回
美国马萨诸塞州波士顿
波士顿美国印第安纳州
美国肯塔基州波士顿
波士顿,GA 31626,美国
波士顿,Salem,VA 22713,美国
波士顿,NY 14025,美国
波士顿,肯塔基州和Boston NY 14025没有限制。
问题:是否有可靠的方法在适当的地图上显示任何地理编码结果放大
等级?
现在我正在使用类似的东西,但我觉得这很丑陋,并没有解决缩放问题。
$ b
if(results [0] .geometry.bounds){
map.fitBounds(results [0] .geometry.bounds );
} else {
map.setCenter(results [0] .geometry.location);
//最终设置放大到某些中间值(丑陋)
}
没错。在尝试了这两种方法并进行了测试后,结果如下:
-
geometry.bounds
object是optionnaly返回的,正如文档所述
- 我们并不完全知道
geometry.bounds
对象的基础是什么
geometry.bounds
可能与推荐的视口不匹配并且通常不会
geometry.bounds
返回任意大小和形状的正方形或矩形,而视口函数总是返回具有相同纵横比(大约1.43)的矩形,无论您的地图容器尺寸是我测试过的。
- 我们并不完全知道
以下是文档中提到的加利福尼亚州旧金山的示例。
以红色使用 geometry.bounds
,并使用视口函数以蓝色显示。
该解决方案足够简单并且可靠。 / p>
var r esultBounds = new google.maps.LatLngBounds(
results [0] .geometry.viewport.getSouthWest(),
results [0] .geometry.viewport.getNorthEast()
);
map.fitBounds(resultBounds);
Goal: I want to have a custom search (geocode) function and be able to list and click each result and display it on the map.
Wanted: Adjust map bounds / zoom level accordingly, i.e. searching for "MA, USA" should zoom the map to let me see the whole Massachusetts state, while searching for "Boston, MA, USA" should zoom on the Boston area. When listing multiple results, the same should apply when clicking on a result.
Issue: I can use the geometry.bounds object with fitBounds - but - some results you get using the geocoder do not have that geometry.bounds object.
A quick example: searching for "Boston" returns
Boston, MA, USA
Boston, IN, USA
Boston, KY, USA
Boston, GA 31626, USA
Boston, Salem, VA 22713, USA
Boston, NY 14025, USA
Both "Boston, KY" and "Boston NY 14025" do not have bounds.
Question: Is there a reliable way to display any geocoder result on a map at the appropriate zoom level?
Right now I am using something like that but I find this ugly and it doesn't solve the zoom issue
if (results[0].geometry.bounds) {
map.fitBounds(results[0].geometry.bounds);
} else {
map.setCenter(results[0].geometry.location);
// eventually set zoom here to some middle-range value (ugly)
}
Right. After trying both methods and testing, it comes out that:
- the
geometry.bounds
object is "optionnaly returned" as the doc says - we don't exactly know what the
geometry.bounds
object is based on geometry.bounds
"may not match the recommended viewport" and often doesn'tgeometry.bounds
returns a square or rectangle of any size and shape while the viewport functions always return a rectangle with the same aspect ratio (around 1.43), whatever your map container dimensions are, as far as I tested.
Below is the example of San Francisco, CA, mentioned in the doc.
In red using geometry.bounds
and in blue using the viewport functions.
The solution is simple enough and is reliable.
var resultBounds = new google.maps.LatLngBounds(
results[0].geometry.viewport.getSouthWest(),
results[0].geometry.viewport.getNorthEast()
);
map.fitBounds(resultBounds);
这篇关于Google Maps API v3 - 地理编码结果问题带有界限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!