我有一个网站,允许用户发布包含谷歌地图地址的表单。
我对谷歌地图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");
});
};
POST ROUTE:
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");
}
});
});
});
});
});
答案 0 :(得分:1)
在这个例子中,我将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");
}
});
});
});