1

您好,当用户在地图中输入城市时,我正在尝试获取地震信息。现在我通过硬编码制作边界框(geonames 地震 api 需要)。我想将边界框设为输入城市的大小。我在 geocoder api v3 中看到这样做:

var geocoder = new google.maps.Geocoder();
   geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        map.fitBounds(results[0].geometry.viewport);
      }
    });

问题 - 这不会让我得到 geonames 需要的东北西南。有人知道怎么做吗?

我当前的代码

var address = document.getElementById("address").value;
            geocoder.geocode( { 'address': address}, function(results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                  // Calculate bounding box based on the given address
                  var north = results[0].geometry.location.lat() + .5;
                  var south = results[0].geometry.location.lat() - .5;
                  var east = results[0].geometry.location.lng() + .5;
                  var west = results[0].geometry.location.lng() - .5;

                  var earthquake = 'http://api.geonames.org/earthquakesJSON?north=' + north + '&south=' + south + '&east=' + east + '&west=' + west + '&username=*****';

+5 -5 是扩展坐标中的数字以创建南北......等

4

1 回答 1

1

您想使用作为google.maps.LatLngBounds 对象的 result[0].geometry.viewport

var north = results[0].geometry.viewport.getNorthEast().lat();
var south = results[0].geometry.viewport.getSouthWest().lat();
var east   = results[0].geometry.viewport.getNorthEast().lng();
var west   = results[0].geometry.viewport.getSouthWest().lng();

固定小提琴

摆弄来自地名的返回消息

摆弄显示标记的硬编码数据

于 2013-12-20T23:41:56.337 回答