通过Parse Cloud Code使用Google Maps API进行地理编码

blu*_*ula 2 javascript google-maps-api-3 parse-platform

我正在处理一些JavaScript,我对其知之甚少,以便对存储在Parse.com上的类中的对象上的地址进行地理编码.通过Cloud Code,我在PFObect上有一个属性,我需要将其传递给地理编码器,然后解析lat和long的结果JSON返回,然后最终将其保存为同一个PFObject上的属性.

我花了很多时间寻找解决方案,而且大多数建议似乎都是使用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 ();

                   });
Run Code Online (Sandbox Code Playgroud)

我希望有这方面经验的人可以指导我朝着正确的方向前进.上面的代码在Parse Cloud Code中正确部署,但我不确定它是否实际被调用,因为日志或错误控制台中没有任何内容.我觉得我缺少一些基本的东西 - 我感谢任何帮助或建议.

小智 5

google.maps.Geocoder是一个javascript,可以在浏览器中工作,使用地理编码,如下所示使用Parse.Cloud.httpRequest,(你需要在goolge API控制台中启用Geocoding 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); } });