2014-09-19 100 views
2

我正在处理一些我并不了解的JavaScript,以便对我在Parse.com上的类中的对象上存储的地址进行地理编码。通过Cloud Code,我有一个PFObect的属性,我需要将其传递给地理编码器,然后解析生成的经纬度返回值,然后最终将其保存为同一个PFObject的属性。通过Parse Cloud使用Google Maps API进行地理编码代码

我花了大量时间寻找解决方案,而且大多数建议似乎都是使用Google Maps JavaScript API v3而不是HTTP请求。

这里是我的代码:

Parse.Cloud.afterSave ('incidentDetails', function(request, status) { 

        var address = request.object.get("address1"); 
        var geocoder; 

        function initialize() { 

        geocoder = new google.maps.Geocoder(); 
        codeAddress(); 

        } 

        function codeAddress() { 

        console.log("test"); 

        geocoder.geocode({ 'address':address}, function (results, status) { 

            if (status == google.maps.GeocoderStatus.OK) { 

            var latLng = results[0].geometry.location; 
            request.object.set("latLng", latLng); 

            } else { 

            alert('Geocode was not successful for the following reasons: ' + status); 
            } 
            }); 
        } 

        initialize(); 

        }); 

我希望有人与此有些经验可以指导我在正确的方向。上述代码在Parse Cloud Code中正确部署,但是我不确定它是否被实际调用,因为日志或错误控制台中没有任何内容。我觉得我错过了一些根本性的东西 - 我感谢任何帮助或建议。

回答

5

google.maps.Geocoder是意味着浏览器工作JavaScript,为使用地理编码,运用Parse.Cloud.httpRequest的如下,(你必须GOOLGE API控制台中启用地理编码API)

Parse.Cloud.httpRequest({ method: "POST", url: ' https://maps.googleapis.com/maps/api/geocode/json ', params: { address : address, key:YourKey }, success: function(httpResponse) { var response=httpResponse.data; if(response.status == "OK"){ var langLat=response.results[0].geometry.location; console.log(langLat); } }, error: function(httpResponse) { console.error('Request failed with response code ' + httpResponse.status); } });

+0

如果您在参数中讨论YourKey:{ address:address, key:YourKey },则它是Google地理编码API密钥而不是解析应用密钥。 – 2015-02-09 03:16:34

0

您不调用函数,因此代码不会执行。尝试:

initialize(); 

codeAddress(); 

如果你期望它的代码不能正常工作,你可以通过调用调试它:

console.log("Your info"); 
+0

好吧,我现在已经运行的代码,我能够打印到日志,但正如我怀疑我需要定义Google的某个地方,因为它返回以下错误:ReferenceError:谷歌没有定义...我认为我需要提供谷歌API的来源...只是不知道确切地在哪里?帮助那里将不胜感激。 – blueHula 2014-09-19 21:26:26

相关问题