Javascript 使用Google Maps API和解析云代码时定义geocoder变量时出错
我正在尝试使用解析云代码来使用GoogleMapsAPI v3对地址进行地理编码。根据谷歌的建议代码,我使用以下代码:Javascript 使用Google Maps API和解析云代码时定义geocoder变量时出错,javascript,google-maps-api-3,parse-platform,Javascript,Google Maps Api 3,Parse Platform,我正在尝试使用解析云代码来使用GoogleMapsAPI v3对地址进行地理编码。根据谷歌的建议代码,我使用以下代码: Parse.Cloud.afterSave ('IncidentDetails', function(request, status) { var address = request.object.get("address1"); var geocoder;
Parse.Cloud.afterSave ('IncidentDetails', function(request, status) {
var address = request.object.get("address1");
var geocoder;
function initialize () {
geocoder = new google.maps.Geocoder();
codeAddress (address);
}
function codeAddress () {
geocoder.geocode( { 'address':address}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var lat = results[0].geometry.location.lat();
var lng = results[0].geometry.location.lng()
request.object.set("lat", lat);
request.object.set("lng", lng);
} else {
alert('Geocode was not successful for the following reasons: ' + status);
}
});
}
initialize ();
});
保存PFObject后,代码运行正常,但在上失败
geocoder = new google.maps.Geocoder ();
错误为:ReferenceError:google未定义
我在HTML中看到定义了源代码和api密钥集,但在纯java脚本示例中没有看到相同的情况
任何关于正确初始化geocoder变量的帮助都将不胜感激 google.maps.Geocoder是一种javascript,用于在浏览器中工作,要使用geocode,请使用Parse.Cloud.httpRequest,如下所示(您需要在goolge API控制台中启用Geocoding API)
Parse.Cloud.httpRequest({
方法:“张贴”,
url:“”,
参数:{
地址:地址:,
钥匙:你的钥匙
},
成功:函数(httpResponse){
var响应=httpResponse.data;
如果(response.status==“OK”){
var langLat=response.results[0].geometry.location;
控制台日志(langLat);
}
},
错误:函数(httpResponse){
console.error('请求失败,响应代码'+httpResponse.status);
}
});代码>您没有包含google API脚本(或者您正在尝试在加载脚本之前使用google.maps对象)。对。。。您是否参考了加载API的适当脚本?我可以找到有关如何/在何处对html执行此操作的参考,但不能找到普通javascript。用于html+javascript的Google示例代码包含一个源定义,而普通javascript示例没有。