简体   繁体   English

在没有加载地图的情况下,获取给定边界框的Google Map边界?

[英]Get Google Map bounds, given a bounding box, without loading map?

I'm building a store finder and would like to implement the following workflow: 我正在构建商店查找器,并希望实现以下工作流程:

  1. User enters a location 用户输入位置
  2. We geocode it using Google Geocoder 我们使用Google Geocoder对它进行地理编码
  3. We examine the geocoded results for the bounding box 我们检查边界框的地理编码结果
  4. We zoom the map in to fit the specified bounds and query for stores within those bounds 我们将地图放大以适应指定范围,并查询这些范围内的商店
  5. If there are stores within the map bounds, display them all on the map 如果地图范围内有商店,则将它们全部显示在地图上
  6. But if there now no stores within the map bounding box, zoom one level further out, and repeat (5) until we find some stores 但是,如果现在在地图边界框内没有商店,则将地图进一步缩小一级,然后重复(5),直到找到一些商店

The above is fine, but it would be better UX if the map did not actually appear to move until some stores have been found. 上面的方法很好,但是如果在找到某些商店之前地图实际上并未移动,则最好使用UX。

So, is it possible to query Google Maps as follows: Given a bounding box, can we find the the bounds of the correctly zoomed Google Map that contains that particular bounding box, without actually loading the Google Map? 因此,是否可以按以下方式查询Google Maps: 给定边界框,我们是否可以找到包含特定边界框的正确缩放的Google Map的边界,而无需实际加载Google Map?

I'm not sure it's possible, because it depends on the width of the map div in my page, I guess. 我不确定是否可行,因为我猜这取决于页面中map div的宽度。

geocoder.geocode( {'address': search_text }, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    map.panTo(results[0].geometry.location);
    map.fitBounds(results[0].geometry.bounds);
    var bounds_to_check_for_stores = map.getBounds();
    // QUESTION: get bounds_to_check_for_stores without the three preceding steps?
    } 
 }

The google.maps.LatLngBounds api-doc has a method: extend(point:LatLng) which will allow you to extend a bounds to ensure that the bounds are adjusted to include the LatLng point passed as a parameter. google.maps.LatLngBounds api-doc具有一个方法: extend(point:LatLng) ,该方法可让您扩展边界以确保将边界调整为包括作为参数传递的LatLng点。 The extend method returns a LatLngBounds that has been adjusted. extend方法返回LatLngBounds已经调整。

With this, you could: 这样,您可以:

  1. Expand the radius of your geocode lookup until you find appropriate stores. 扩展地理编码查询的半径,直到找到合适的商店。
  2. Find the closest store or group of stores that you wish to use. 查找您要使用的最近的商店或商店组。
  3. Retrieve the current bounds from the map: Map.getBounds() . 从地图检索当前边界: Map.getBounds()
  4. Call LatLngBounds.extend to adjust the bounds, using the .geometry.location property of the stores you selected in Step 2. 使用在步骤2中选择的商店的.geometry.location属性,调用LatLngBounds.extend调整边界。
  5. Call Map.panToBounds to change the map's viewport, keeping the user's experience simple, because only one change to the view will be performed. 调用Map.panToBounds可以更改地图的视口,从而使用户的体验变得简单,因为将仅对视图执行一次更改。

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

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