2014-09-19 75 views
-1

我試圖使用解析雲代碼來使用Google地圖API v3對地址進行地址解析。繼從谷歌的推薦代碼:我現在用的是以下幾點:使用Google地圖API和解析雲代碼時定義地理編碼變量時出錯代碼

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:谷歌沒有定義

我在HTML中看到源定義和api鍵集,但我看不到純Java腳本示例中的相同。

任何幫助獲取geocoder變量正確初始化將不勝感激。

+0

您不包括google API腳本(或者您正在加載腳本之前嘗試使用google.maps對象)。 – geocodezip 2014-09-19 22:53:39

+0

正確...你有沒有參考適當的腳本來加載API?我可以找到如何/在哪裏做HTML的參考,但不是簡單的JavaScript。 Google + javascript示例代碼包含一個源定義,而普通的javascript樣本沒有。 – blueHula 2014-09-19 23:06:00

回答

0

google.maps.Geocoder是指在瀏覽器中運行,使用地理編碼,請使用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); } });

一個javascript