我目前正在开发一个项目来制作一个位置感知网站。本质上,用户来到该页面,使用 HTML5 方法找到他们的位置,然后使用 jQuery,将该位置发布到一个页面,该页面将位置/地址保存到 codeigniter session 中,但如果他们想要更新其位置,或更改为不同的位置(即他们想要使用他们的工作地址作为位置而不是他们现在的地址),有一个 jQuery 颜色框可以显示并让他们输入自定义地址。
一切都完美地获取初始位置,但是当我尝试保存更新的位置时,我收到错误“Uncaught TypeError: Object [object DOMWindow] has no method 'lat'”然后 Google Chrome 引用该错误不是在 jQuery 中,而是在 Google Maps API 文件中。有什么建议吗?
jQuery('.inputsubmit').click(function() {
//Takes values from user submitted fields and parses them into an address string
var street = jQuery('.inputaddress').val();
var city = jQuery('.inputcity').val();
var state = jQuery('.inputstate').val();
var address = street +" "+ city +", " +state;
geocoder = new google.maps.Geocoder();
var geocoderresult= geocoder.geocode({'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var newlocation = results[0].geometry.location;
//Posts coordinates and address string to a CodeIgniter function to update users session information
jQuery.post("somepage", {location: newlocation, address: address},function(data) {
alert("Data Loaded: " + data);
});
} else {
alert("Geocoder failed due to: " + status);
}
});
});
我已经尝试了一切我能想到的方法来让这个帖子发挥作用。到目前为止,所有代码都有效,我已经注释掉了帖子行,一切正常。这是我们主要网站的功能之一,可根据位置提供即时、快速的结果。
谢谢!
最佳答案
我认为除了原始页面之外,您还需要将 Google API 代码加载到 somepage
上。由于 $.POST
找不到 lat
对象,因此 geolocation.js 文件似乎未包含在 somepage
中。
关于jquery - 地理定位和 jQuery - 无法使用 ajax 发布结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7797242/