使用 Google 地理定位获取边界,是否正确?

obtaining bounds with Google Geolocation, is this correct?

我使用 Google 地理定位取得了巨大成功。昨天,我们通过从用户位置的边界进行搜索来改进我们的 storelocator 脚本。所以我们在地理定位的时候,还需要用户东北和西南的经纬度边界。

我们正在从控制台获取这些信息,如下所示(我粘贴了整个地理定位功能,以防万一):

$('#geomate').click(function() { 
var geocoder;
var split1;
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
}
//Get the latitude and the longitude;
function successFunction(position) {
    var lat = position.coords.latitude;
    var lng = position.coords.longitude;
    codeLatLng(lat, lng);
}
function errorFunction(){
    alert("Geolocator failed. If you're on a mobile device, please switch on your Location settings (GPS)");
}
geocoder = new google.maps.Geocoder();
function codeLatLng(lat, lng) {
    var latlng = new google.maps.LatLng(lat, lng);
    geocoder.geocode({'latLng': latlng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            console.log(results)
            if (results[1]) {
                split1 = results[1].formatted_address.split(', ');
                swlat = results[1].geometry.bounds.Ca.k;
                swlng = results[1].geometry.bounds.va.j;
                nelat = results[1].geometry.bounds.Ca.j;
                nelng = results[1].geometry.bounds.va.k;
                //formatted address

                //if zipcode has a value, clear it first
                if ($('input[name="zipcode"]').val() != '') {
                $('input[name="zipcode"]').val('');
                }
                //now enter the geolocation
                $('input[name="zipcode"]').val(split1[1] + ', ' + split1[2]);
                // if hidden geomtery fields exist, remove them
                $('.storelocator_body form').find('.geomerty').remove();
                $('.storelocator_body form').append('<input type="hidden" class="geomerty" name="lat" value="' + lat + '">');
                $('.storelocator_body form').append('<input type="hidden" class="geomerty" name="lng" value="' + lng + '">');
                $('.storelocator_body form').append('<input type="hidden" class="geomerty" name="swlat" value="' + swlat + '">');
                $('.storelocator_body form').append('<input type="hidden" class="geomerty" name="swlng" value="' + swlng + '">');
                $('.storelocator_body form').append('<input type="hidden" class="geomerty" name="nelat" value="' + nelat + '">');
                $('.storelocator_body form').append('<input type="hidden" class="geomerty" name="nelng" value="' + nelng + '">');
            } else {
                alert('No results found');
            }
        } else {
            alert('Geocoder failed due to: ' + status);
        }
    });
}

我想知道的是,有没有更简单的方法来从地理定位请求中获取边界?例如,我尝试通过以下代码获取边界:

var bounds = new google.maps.LatLngBounds(swLatLn, neLatLn);

但是我没有成功,因为控制台 returns 错误“Uncaught ReferenceError: swLatLn is not defined”。

非常感谢!

不要使用 results[1].geometry.bounds.Ca.k; 等。这些是可能会更改的未记录的属性。

而是使用 documented LatLngBounds methods getNorthEast()getSouthWest() 其中 return 一个 LatLng.

如果您需要纬度和经度值,您可以使用 lat()lng() 方法获取它们:

var lat = results[1].geometry.bounds.getSouthWest().lat();
var lng = results[1].geometry.bounds.getSouthWest().lng();
// And so on...

希望对您有所帮助。