地理定位和 jQuery - 无法使用 ajax 发布结果
我目前正在开发一个项目来制作一个位置感知网站。本质上,用户来到该页面,使用 HTML5 方法找到他们的位置,然后使用 jQuery,将该位置发布到一个页面,该页面将位置/地址保存到 codeigniter 会话中,但如果他们想要更新其位置,或更改为不同的位置(即他们想要使用他们的工作地址作为位置而不是他们现在的地址),有一个 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);
}
});
});
我已经尝试了一切我能想到的方法来让这个帖子发挥作用。到目前为止,所有代码都有效,我已经注释掉了帖子行,一切正常。这是我们主要网站的功能之一,可根据位置提供即时、快速的结果。
谢谢!
I'm currently working on a project to make a location-aware site. In essence, the user comes to the page, and their location is found using the HTML5 method and then using jQuery, the location is posted to a page which saves the location/address to a codeigniter session, but if they want to update their location, or change to a different location(IE they want to use their work address as the location instead of their present address), theres a jQuery colorbox that displays and lets them type in a custom address.
Everything works flawlessly to get the initial location, but when I try and get the updated location saved, I receive the error "Uncaught TypeError: Object [object DOMWindow] has no method 'lat'" which then Google Chrome references as being an error not in jQuery, but in the file for Google Maps API. Any suggestions?
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);
}
});
});
I've tried everything I can think of to get the post to work. All of the code works up till the point, and I've commented out the post line and everything works correctly. This is one of our main website's features, to provide instant and quick results based off of location.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为除了原始页面之外,您还需要将 Google API 代码加载到
somepage
上。由于$.POST
找不到lat
对象,因此 geolocation.js 文件似乎未包含在somepage
中。i think you need to have your Google API code loaded on
somepage
in addition the original page . Because the$.POST
can't find thelat
object it seems the geolocation.js file isn't included onsomepage
.