Google Maps API v3 - 地理编码器结果有边界问题 [英] Google Maps API v3 - Geocoder results issue with bounds

查看:19
本文介绍了Google Maps API v3 - 地理编码器结果有边界问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:我想要一个自定义搜索(地理编码)功能并且能够列出并点击每个结果并将其显示在地图上.

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.

通缉:相应地调整地图边界/缩放级别,即搜索MA, USA"应该缩放地图让我看到整个马萨诸塞州,同时搜索Boston, MA, USA"应该放大波士顿地区.列出多个结果时,点击结果时也应如此.

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.

问题:我可以使用带有 fitBounds 的 geometry.bounds 对象 - 但是 - 您使用地理编码器获得的某些结果没有该 geometry.bounds 对象.

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

美国马萨诸塞州波士顿
美国印第安纳州波士顿
美国肯塔基州波士顿
波士顿,GA 31626,美国
波士顿,塞勒姆,弗吉尼亚州 22713,美国
波士顿, NY 14025, 美国

Boston, MA, USA
Boston, IN, USA
Boston, KY, USA
Boston, GA 31626, USA
Boston, Salem, VA 22713, USA
Boston, NY 14025, USA

Boston, KY"和Boston NY 14025"都没有界限.

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:

  1. 正如文档所说,geometry.bounds 对象是可选的返回"
  2. 我们并不完全知道 geometry.bounds 对象是基于什么
  3. geometry.bounds可能与推荐的视口不匹配"并且通常不匹配
  4. geometry.bounds 返回任何大小和形状的正方形或矩形,而视口函数始终返回具有相同纵横比(大约 1.43)的矩形,无论您的地图容器尺寸是什么,到目前为止正如我测试的那样.
  1. the geometry.bounds object is "optionnaly returned" as the doc says
  2. we don't exactly know what the geometry.bounds object is based on
  3. geometry.bounds "may not match the recommended viewport" and often doesn't
  4. geometry.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.

以下是文档中提到的加利福尼亚州旧金山的示例.
红色使用 geometry.bounds,蓝色使用视口函数.

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屋!

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