【发布时间】:2012-02-27 17:40:41
【问题描述】:
我正在尝试构建一个 CakePHP 应用程序,它允许用户更新他们的地址信息,然后将新的纬度/经度存储在数据库中。我正在使用 Google 的地理编码器来检索纬度/经度信息,但我的功能不起作用。我几乎没有使用 Javascript 的经验,所以我不确定我做错了什么。我的代码是我在互联网上看到的内容的一种融合。请,如果有人可以建议我,那就太好了。
这是我的地理编码功能:
<script type="text/javascript">
function getLatLong(address){
var geo = new google.maps.Geocoder;
var address =<?php echo $this->data['Location']['address'].' '.$this->data['Location']['city'].', '.$this->data['Location']['state'].' '.$this->data['Location']['zip']; ?>;
geo.geocode({'address':address},function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
return results[0].geometry.location;
alert('The address is' + address);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>
在我的 html 之上,这里是在页面的 onLoad 中调用它的脚本:
<script>
if(window.attachEvent) {
window.attachEvent('onload', getLatLong());
} else {
if(window.onload) {
var curronload = window.onload;
var newonload = function() {
curronload();
getLatLong();
};
window.onload = newonload;
} else {
window.onload = getLatLong();
}
}
</script>
我的文档开头有:
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
当我加载页面并使用 chrome 检查时,我看到了
Uncaught ReferenceError: getLatLong is not defined (anonymous function)1:89
1:106 Uncaught SyntaxError: Unexpected identifier
【问题讨论】: