gpt4 book ai didi

javascript - 无法读取未定义的属性 'geometry' - 有时?

转载 作者:行者123 更新时间:2023-12-03 03:12:47 25 4
gpt4 key购买 nike

我有一个网站,允许用户发布包含谷歌地图地址的表单。

我在使用 Google map API 时遇到了一个奇怪的问题。 偶尔我会收到错误“无法读取未定义的属性‘几何’”,有时则不会。也许 1/4 的时间我会收到错误。我知道这与查找位置有关,但我在发布表格之前验证位置,所以我不应该遇到这个问题?我以为我可能会超过谷歌每分钟的帖子数量或其他东西,但他们的限制非常高,所以这不是问题。知道为什么会发生这种情况吗?

参见下面的代码:

前端:

function initMap() {
var lat = <%= listings.lat %>;
var lng = <%= listings.lng %>;
var center = {lat: lat, lng: lng };
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 17,
center: center,
scrollwheel: false
});

var infowindow = new google.maps.InfoWindow({
content: contentString
});
var marker = new google.maps.Marker({
position: center,
map: map
});
marker.addListener('click', function() {
infowindow.open(map, marker);
});
}
</script>

地址验证:

  $("#checkingAddress").click(doGeocode);


function doGeocode(){

var addr = document.getElementById("location");
// Get geocoder instance
var geocoder = new google.maps.Geocoder();

// Geocode the address
geocoder.geocode({'address': addr.value}, function(results, status){
if (status === google.maps.GeocoderStatus.OK && results.length > 0) {

// set it to the correct, formatted address if it's valid
addr.value = results[0].formatted_address;;

// show an error if it's not
}else alert("Invalid address");
});
};

发布路线:

router.post("/", middleware.isLoggedIn, function(req, res, next){
var currentimages = allimages;
var name = req.body.name;
var acres = req.body.acres;
var rooms = req.body.rooms;
var baths = req.body.baths;
var footage = req.body.footage;
var directions = req.body.directions;
var schools = req.body.schools;
var link = req.body.link;
var mls = req.body.mls;
var createdby = req.body.createdby;
var createdbyemail = req.body.createdbyemail;
var desc = req.body.description;
var search = req.body.search;
var price = req.body.price;
geocoder.geocode(req.body.location, function (err, data) {
var lat = data.results[0].geometry.location.lat;
var lng = data.results[0].geometry.location.lng;
var location = data.results[0].formatted_address;
var amenities = req.body.amenities;
var author = {
id: req.user._id,
username: req.user.username,
email: req.user.email
}
var newListings = {acres: acres, landoption: landoption, rooms: rooms, baths: baths, footage: footage, directions: directions, schools: schools, link: link, mls: mls, name: name, currentimages: currentimages, createdby: createdby, createdbyemail: createdbyemail, description: desc, price: price, search: search, author:author, location: location, lat: lat, lng: lng, amenities: amenities}

Listings.create(newListings, function(err, newlyCreated){
if(err){
console.log('this2' + err);
} else {

res.redirect("/listings");
}
});
});
});
});
});

最佳答案

在此示例中,我将 newListings 对象拆分为初始函数运行时可用的字段以及仅在地理编码回调中可用的字段。

在地理编码回调中,我进行了快速测试以确保有一些结果,如果有,则将它们添加到 newListings 对象中。

还在地理编码回调中创建新的列表。这必须在回调中,否则它将在回调发生之前被触发。

router.post("/", middleware.isLoggedIn, function(req, res, next){

// create a newListings object with the data available to us on post

var newListings = {
acres: req.body.acres,
rooms: req.body.rooms,
name: req.body.name,
(etc, with the remaning fields)
};

// initiate a call to geocode. When the geocode is complete, add other fields to our newListings object
geocoder.geocode(req.body.location, function (err, data) {
if (data && data.results && data.results.length) { // there are some results
newListings['lat'] = data.results[0].geometry.location.lat;
newListings['long'] = data.results[0].geometry.location.long;
newListings['location'] = data.results[0].formatted_address;
}

// only once we have either tried and failed to process the geocode result, or succeeded and populated our newListings object, can we continue
Listings.create(newListings, function(err, newlyCreated){
if(err){
console.log('this2' + err);
} else {
res.redirect("/listings");
}
});
});
});

关于javascript - 无法读取未定义的属性 'geometry' - 有时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46894673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com