无法获得财产&getSOouthWest'未定义或空引用

时间:2015-03-05 17:53:05

标签: google-maps geolocation

我在Chrome上进行了所有开发,今天决定打开可怕的IE浏览器,看看它们的外观。我偶然发现了地理位置代码的问题。我必须得到该位置的getSouthWestgetNorthEast边界,这是通过以下代码在名为codeLatLng()的函数内完成的:

if (results[0]) {
    split1 = results[0].formatted_address.split(', ');
    var swlat = results[0].geometry.bounds.getSouthWest().lat();
    var swlng = results[0].geometry.bounds.getSouthWest().lng();
    var nelat = results[0].geometry.bounds.getNorthEast().lat();
    var nelng = results[0].geometry.bounds.getNorthEast().lng();
});

IE上出现控制台错误:

  

'无法获得财产' getSouthWest'未定义或空引用'。

经过一番挖掘,我将结果参考从结果[0]改为结果[1],这很有效。然而,如果没有找到结果[1]的界限,那么错误让我有点紧张,因为我的脚本可能会再次破坏。

在使用地理位置时,是否有更高效的内置方式来获取界限?我还通过以下代码获取地理位置时的纬度和经度:

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);
}
var geocoder1 = new google.maps.Geocoder();
function codeLatLng(lat, lng) {
    var latlng = new google.maps.LatLng(lat, lng);
    geocoder1.geocode({'latLng': latlng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
        // I have a bunch of code here that creates hidden
        // input fields with the lat, lng, swlat, swlng, nelat and nelng
        });
    });
});

我知道使用Places,我可以使用以下代码:

swlat = place.geometry.viewport.getSouthWest().lat();
// etc for swlng, nelat & nelng

我可以在successFunction()内使用类似的内容吗?我环顾四周并尝试了一些事情:

1)我在var bounds = new google.maps.LatLngBounds();下添加了新的var latlng,然后将var swlat更改为:

var swlat = bounds.getSouthWest().lat();

但这返回了180的纬度。

2)我将上面1)中的var bounds更改为var bounds = map.getBounds();,但这给了我整个英国的界限!

我现在真的在挠头。任何人都可以指导我解决方案吗?我已经读过HTML Geolocation,显然position.coords.southwest.latitude没有定义,这是一种耻辱。

我能想到的唯一另一件事就是迭代结果,从结果[0]开始,直到我们找到一组地理定位的边界。但这似乎有很多工作,如果有一个可以随时用于地理位置的界限就好了,就像有一个lat和lng一样。

任何帮助都非常感激。

更新

我尝试使用代码:

var bounds = map.getBounds(new google.maps.LatLngBounds(latlng, latlng));
var ne = bounds.getNorthEast();
var sw = bounds.getSouthWest();

这似乎是一个好主意,但回归的是整个英国的界限。不久的一天,我会到达那里!

1 个答案:

答案 0 :(得分:2)

无法保证退回的bounds - 属性。

e.g。当location_type是ROOFTOP时,结果是精确匹配(坐标)。

边界通常会返回街道和地区,如国家,州等。

如果没有返回任何边界,您可以将所有变量设置为返回的位置或使用viewport - 属性(它也是LatLngBounds - 对象)