javascript - 查找最近的已知位置 : Google Reverse Geocoding

标签 javascript google-maps google-maps-api-3 reverse-geocoding

我需要使用 Google Maps API 获取给定坐标的格式化地址。我用Google Reverse Geo coding用于查找位置名称。当 Google map 数据库中有可用的位置名称时,此方法就可以正常工作。

大多数时候,给定的坐标来自远离城市边界的位置(例如在高速公路上)。由于 map 上没有定义名称,该函数返回ZERO_RESULTS。要求是找到要返回的最近的已知位置地址。

从功能上来说,这听起来很不错,但从技术上讲,如何去做呢?

目前我正在找到距此点几(公里)米的位置,检查该位置是否有名称,然后递归直到获得名称。

就我个人而言,不喜欢这种方法,原因如下:

  1. 无法猜测该往哪个方向才能找到有名称的位置

  2. 我可能会朝一个方向走,但已知的地方距离相反方向只有几米。

  3. 在增量过程中我可能会走得太远,例如在 15 的地方 公里之外有一个名字。我搜索 10 公里外的名字并查看 再次 20 公里,增量标识为 10 公里。

以下是完整的代码,它可以工作,但存在上述问题。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Map Test</title>
     <script type="text/javascript" src="//code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>

</head>
<body>
    <span id="spanId">Loading...</span>
    <script>
        Number.prototype.toRad = function () {
            return this * Math.PI / 180;
        }

        Number.prototype.toDeg = function () {
            return this * 180 / Math.PI;
        }

        google.maps.LatLng.prototype.destinationPoint = function (brng, dist) {
            dist = dist / 6371;
            brng = brng.toRad();

            var lat1 = this.lat().toRad(), lon1 = this.lng().toRad();

            var lat2 = Math.asin(Math.sin(lat1) * Math.cos(dist) +
                                 Math.cos(lat1) * Math.sin(dist) * Math.cos(brng));

            var lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(dist) *
                                         Math.cos(lat1),
                                         Math.cos(dist) - Math.sin(lat1) *
                                         Math.sin(lat2));

            if (isNaN(lat2) || isNaN(lon2)) return null;

            return new google.maps.LatLng(lat2.toDeg(), lon2.toDeg());
        }

        var pointA = new google.maps.LatLng(32.6811,74.8732);

        getLocation(pointA);
var distance = 0;
        function getLocation(info) {

            var myCenter = info; //new google.maps.LatLng(info.split(",", 3)[1], info.split(",", 3)[2]);
            var gc = new google.maps.Geocoder();

            gc.geocode({ 'location': myCenter }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if (results[1]) {
                        document.getElementById('spanId').innerHTML = results[1].formatted_address + ', ' + distance + ' kms away from original point' ;
                    } else {
                        window.alert('No results found');
                    }
                } else {
                    if (status == 'ZERO_RESULTS' )
                    {
                        var radiusInKm = 10;
                        distance += radiusInKm;
                        document.getElementById('spanId').innerHTML = 'Getting results from  ' + distance + ' kms away';
                        var pointB = pointA.destinationPoint(90, distance);
                        setTimeout(function(){
                            getLocation(pointB);
                        }, 2000);
                    }

               }
            });
        }


    </script>
</body>
</html>

如果有人有好的解决方案,我将不胜感激。

JSFiddle 链接:https://jsfiddle.net/hbybs68q/1/

谢谢。

最佳答案

您需要修改getLocation函数中的if条件。 检查结果[0]而不是结果[1]。

if (status == google.maps.GeocoderStatus.OK) {
      if (results[0]) {
            document.getElementById('spanId').innerHTML = results[0].formatted_address + ', ' + distance + ' kms away from original point' ;
      } else {
              window.alert('No results found');
      }
} else {
       window.alert('Geocoder failed due to - ' + status)
}

关于javascript - 查找最近的已知位置 : Google Reverse Geocoding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31875483/

相关文章:

google-maps - 在 Google Maps 或 Earth 中使用 KML 在多边形中绘制洞不起作用。有人有想法吗?

google-maps - 谷歌地图信息窗口

javascript - gmaps4rails - 在不刷新页面的情况下动态更新 map 上数据库中的标记

javascript - Google Maps API - 如何在不显示 map 的情况下从自动完成中获取纬度和经度?

python - 找不到 PyMaps 变量

javascript - 将字符串转换为具有出现次数的对象

javascript - 我可以将具有 HTML 5 Doctype 的页面加载到具有 Frameset Doctype 的页面上的框架中吗?

ios - 标记谷歌地图 Swift 信息窗口

JavaScript 数组范围

javascript - 函数和构造函数的区别