【问题标题】:Uncaught TypeError: Cannot read property 'geometry' of undefined未捕获的类型错误:无法读取未定义的属性“几何”
【发布时间】:2018-11-11 06:05:14
【问题描述】:

我已经让谷歌自动完成工作,但万一用户没有选择自动完成,我正在尝试将地址提交给谷歌并自己检索纬度和经度。 我遇到的问题是,当我在不使用自动完成的情况下提交时,我得到了 Uncaught

类型错误:

无法读取未定义和未捕获的属性“几何” TypeError:无法读取未定义的属性“位置”(我的没有 console.logs 被触发)。

如果我使用自动完成提交,我仍然会得到 Uncaught

TypeError: 无法读取未定义的属性“几何”

即使自动完成功能确实可以获取经纬度的地址。

我的 HTML

<form method="GET" id="my-form" action="/search" onsubmit="check()">
<input class="form-control" id="getaddy" type="text" placeholder="Search..." name="term" onFocus="geo()">
<input id="latitude" type="hidden" name="latitude">
<input id="longitude" type="hidden" name="longitude">
</form>

我调用geo() 自动完成,但如果他们不使用自动完成,我会尝试使用我的 check() 函数在提交时捕获它。这是我的 JS

function check() {
let latitude = document.getElementById("latitude").value;
if (latitude) {
     console.log(latitude);
  } else {

let term = $("#getaddy").val();

$.ajax({
    url: "https://maps.googleapis.com/maps/api/geocode/json?address="+encodeURIComponent(term) +"&key=GOOGLEKEY",
    type: 'get',
    success: function(data) {
        if (data.status === 'OK') {
            // Get the lat/lng from the response
            let lat = data.results[0].geometry.location.lat;
            let lng = data.results[0].geometry.location.lng;

            console.log("pass");

        }
    },
    error: function(msg) {
        console.log("fail");
    }
});
    return true;
}
}


function geo() {

  autocomplete = new google.maps.places.Autocomplete(
    (document.getElementById('getaddy')), {
      types: ['geocode']
    });
  autocomplete.addListener('place_changed', addlatlong);
}
function addlatlong() {

var place = autocomplete.getPlace();

var latitude = place.geometry.location.lat();
var longitude = place.geometry.location.lng();

document.getElementById("latitude").value = latitude;
document.getElementById("longitude").value = longitude;
}

非常感谢您的帮助!

【问题讨论】:

  • 那么,您已经有了latitudelongitude 两个属性值,并且您想使用这两个值来获取地址但收到这样的错误消息?
  • 天啊,更新编辑...
  • 如果他们在我的输入中使用自动完成功能 那么是的,我得到了纬度和经度。但是,如果他们只是在字段中输入地址而不从自动完成列表中进行选择,那么我就不会得到纬度和经度。这就是为什么我有 check() 函数来查看我是否得到了纬度。如果我没有,那么我会尝试将地址提交给谷歌以获取纬度和经度。

标签: javascript google-places-api google-places typeerror


【解决方案1】:

如果你已经有了latitudelongitude这两个属性值,建议你使用这段代码(谷歌提供)获取地址(不使用jquery ajax): p>

var geocoder = new google.maps.Geocoder;
var latlng = { lat: latitude, lng: longitude };

geocoder.geocode({ 'location': latlng }, function (results, status) {
    if (status === 'OK') {
        if (results[0]) {
            var address = results[0].formatted_address);
            console.log(address);
        } else {
            console.log('No results found');
        }
    } else {
        console.log('Geocoder failed due to: ' + status);
    }
});

来源:Reverse Geocoding (Address Lookup)

如果他们没有提交请求,你可以尝试使用jquery捕获searchBox失去焦点时的事件(focusout),然后调用事件places_changed获取latitudelongitude.

来源:Places Search Box

来源:Trigger places_changed in Google maps from submit button

【讨论】:

  • 如果用户从自动完成列表中选择他们的地址,我只会得到纬度和经度。如果用户只是在表单中手动输入地址,那么我不会得到纬度或经度。
  • @ChrisGrim 我已更新答案以在用户未提交请求时获取 latitudelongitude 值。
猜你喜欢
  • 2019-10-10
  • 2021-12-22
  • 2015-01-06
  • 2017-07-26
  • 2019-02-26
  • 2021-12-25
  • 1970-01-01
相关资源
最近更新 更多