2015-10-13 91 views
1

我试图在我的Siebel应用程序的一个文本字段中使用Google API实现地址自动完成功能。我不断收到错误“谷歌没有定义”。我正在使用jquery以下是我的代码:Google API用于地址自动完成不起作用

 PhoneChangePR.prototype.BindData = function(a) { 
       SiebelAppFacade.PhoneChangePR.superclass.BindData.call(this, a); 
$.getScript("http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js", function() { 
      console.log("Google maps successfully added"); 
      }); 
        $.getScript("http://maps.google.com/maps/api/js?sensor=false&libraries=places&language=en-AU", function() { 
      console.log("Second script loaded successfully"); 
      }); 


      var autocomplete = new google.maps.places.Autocomplete($("input[aria-labelledby=NewAddressLine1_Label]")[0], {}); 

       google.maps.event.addListener(autocomplete, 'place_changed', function() { 
        var place = autocomplete.getPlace(); 
        console.log(place.address_components); 
    }); 

提前请help.Thanks。

+0

'Google不要defined'通常在地图API的js没有加载发生。所以一定要正确加载你的脚本。 –

+1

我得到了这个解决方案从http://stackoverflow.com/questions/4061125/autofill-address-google-maps-api 它似乎工作,当我在网上平台上试用它。我所做的一切是我已经改变了元素的名称。 –

+0

我很好奇你在用jQuery做什么......当你引用'$ .getScript()'时,你使用的是jQuery,但是你在'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js' ...但你必须已经在你的页面上有jQuery可用! – duncan

回答

1

你加载谷歌地图API异步,但你试图调用google.maps.places.Autocomplete构造函数的API已经被加载之前。

移动所有依赖于API代码加载到成功回电话。

PhoneChangePR.prototype.BindData = function(a) { 
    SiebelAppFacade.PhoneChangePR.superclass.BindData.call(this, a); 
    $.getScript("http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js", function() { 
     console.log("jQuery successfully loaded"); 
    }); 

    $.getScript("http://maps.google.com/maps/api/js?sensor=false&libraries=places&language=en-AU", function() { 
     console.log("Google maps successfully added"); 
     var autocomplete = new google.maps.places.Autocomplete($("input[aria-labelledby=NewAddressLine1_Label]")[0], {}); 

     google.maps.event.addListener(autocomplete, 'place_changed', function() { 
      var place = autocomplete.getPlace(); 
      console.log(place.address_components); 
     }); 
    }); 
}); 

而不是依靠jQuery的成功回调,你可能想使用谷歌自己的回调在地图JS已成功加载,通过添加callback URL参数上加载Maps API的请求。试试这个:

PhoneChangePR.prototype.BindData = function(a) { 
    SiebelAppFacade.PhoneChangePR.superclass.BindData.call(this, a); 
    $.getScript("http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js", function() { 
     console.log("jQuery successfully loaded"); 
    }); 

    $.getScript("http://maps.google.com/maps/api/js?sensor=false&libraries=places&language=en-AU&callback=setupAutocomplete"); 
}); 

function setupAutocomplete() { 
    console.log("Google maps successfully added"); 
    var autocomplete = new google.maps.places.Autocomplete($("input[aria-labelledby=NewAddressLine1_Label]")[0], {}); 

    google.maps.event.addListener(autocomplete, 'place_changed', function() { 
     var place = autocomplete.getPlace(); 
     console.log(place.address_components); 
    }); 
} 
+0

代码worked.Thanks很多 –

+0

嗨, 上述解决方案的工作sometimes.But大部分我收到错误的时间: 遗漏的类型错误:ü可以建议我一个解决方案无法读取的不确定 财产“自动完成”?谢谢 –

+0

@GeethuAnnJoseph一旦地图js加载 – duncan