谷歌地图自动完成js正在localhost上工作但不在heroku上工作

我是铁轨开发人员的初级ruby。 我开发了一个使用谷歌地图JavaScript API的网络应用程序,我在表单输入上使用自动完成function。

自动完成在localhost上正常工作,但在heroku上部署后不再起作用。

这是我的html.erb。 为了获得完美的信息,表单位于页脚中。 此表单是部分隐藏的,唯一可见的部分对应于用于上传video文件的按钮。 单击此按钮并选择video文件后,我将显示包含表单其余部分的模式。 这就是显示’text_field_tag’(id =“user_input_autocomplete_address”)并且自动完成应该在哪里工作的地方。

    
$('#review_video').change(function(){ $('#myModal').modal('show'); });

页脚(收集模态和表单)在layouts / application.html.erb视图中调用,如下所示。 您会看到我在那里使用Google API密钥(隐藏在application.yml中)

          <meta name="msapplication-TileImage" content="">   <meta name="description" content="">  <meta property="og:title" content="" />  <meta property="og:url" content="" /> <meta property="og:image" content="" /> <meta property="og:description" content="" /> <meta property="og:site_name" content="" />   <meta name="twitter:site" content=""> <meta name="twitter:title" content=""> <meta name="twitter:description" content=""> <meta name="twitter:creator" content=""> <meta name="twitter:image:src" content="">  <meta itemprop="name" content=""> <meta itemprop="description" content=""> <meta itemprop="image" content="">       (function(e,b){if(!b.__SV){var a,f,i,g;window.mixpanel=b;b._i=[];b.init=function(a,e,d){function f(b,h){var a=h.split(".");2==a.length&&(b=b[a[0]],h=a[1]);b[h]=function(){b.push([h].concat(Array.prototype.slice.call(arguments,0)))}}var c=b;"undefined"!==typeof d?c=b[d]=[]:d="mixpanel";c.people=c.people||[];c.toString=function(b){var a="mixpanel";"mixpanel"!==d&&(a+="."+d);b||(a+=" (stub)");return a};c.people.toString=function(){return c.toString(1)+".people (stub)"};i="disable time_event track track_pageview track_links track_forms register register_once alias unregister identify name_tag set_config reset people.set people.set_once people.increment people.append people.union people.track_charge people.clear_charges people.delete_user".split(" "); for(g=0;g<i.length;g++)f(c,i[g]);b._i.push([a,e,d])};b.__SV=1.2;a=e.createElement("script");a.type="text/javascript";a.async=!0;a.src="undefined"!==typeof MIXPANEL_CUSTOM_LIB_URL?MIXPANEL_CUSTOM_LIB_URL:"file:"===e.location.protocol&&"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js".match(/^\/\//)?"https://cdn.mxpnl.com/libs/mixpanel-2-latest.min.js":"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js";f=e.getElementsByTagName("script")[0];f.parentNode.insertBefore(a,f)}})(document,window.mixpanel||[]); mixpanel.init("ENV['MIXPANEL_TOKEN']");              

最后,允许输入自动完成的javascript代码(将“user_input_autocomplete_address”作为id)位于assets / javascript / autocomplete.js中,如下所示:

 function initializeAutocomplete(id) { var element = document.getElementById(id); console.log('coucou'); if (element) { var autocomplete = new google.maps.places.Autocomplete(element, { types: ['geocode', 'establishment'] }); google.maps.event.addListener(autocomplete, 'place_changed', onPlaceChanged); } } function onPlaceChanged() { var place = this.getPlace(); console.log(place); // Uncomment this line to view the full object returned by Google API. for (var i in place.address_components) { var component = place.address_components[i]; // console.log(component.types); for (var j in component.types) { // Some types are ["country", "political"] var type_element = document.getElementById(component.types[j]); // Returns the div with their differents ids or null if the id doesnt exist // console.log(document.getElementById(component.types[j])); if (type_element) { type_element.value = component.long_name; } } } var formatted_address = document.getElementById('formatted_address'); // console.log(international_phone_number); if (formatted_address) { formatted_address.value = place.formatted_address; } for (var i in place.types) { // console.log(place.types); // console.log(i); // console.log(place.types[i]); var type_content = document.getElementById('category_' + i); // Returns the div with their differents ids or null if the id doesnt exist // console.log(document.getElementById(component.types[j])); if (type_content) { console.log(place.types[i]); type_content.value = place.types[i]; } } var international_phone_number = document.getElementById('phone_number'); // console.log(international_phone_number); if (international_phone_number) { international_phone_number.value = place.international_phone_number; } var name = document.getElementById('name'); if (name) { name.value = place.name; } var gplace_id = document.getElementById('gplace_id'); if (gplace_id) { gplace_id.value = place.place_id; } var website = document.getElementById('website'); if (website) { website.value = place.website; } for (var i in place.opening_hours.weekday_text) { var hours_open_in_db = place.opening_hours.weekday_text[i]; var hours_open_in_form = document.getElementById('hours_open_day_' + i); if (hours_open_in_form) { hours_open_in_form.value = hours_open_in_db; } } } google.maps.event.addDomListener(window, 'load', function() { initializeAutocomplete('user_input_autocomplete_address'); }); 

为了在heroku上部署所有这些,我已经做了以下事情:

  1. 终端中的“heroku运行db:migrate”(因为我有一些待定的迁移);
  2. 终端中的“figaro heroku:set -e production”,以便将我的所有API密钥都推送到heroku(我还检查过这些密钥确实在heroku的配置变量中)。
  3. 在Google的开发者控制台中,在我的项目中以及在Google地图javascript API中,我确保我的浏览器密钥可以用于我的locahost和我的域名。 为了非常精确,我使用相同的密钥用于我的locahost和我的域名。 到目前为止,在Google的控制台中,我唯一还没有完成的工作就是certificate我拥有自己的域名。
  4. 我检查过我还没有达到任何API配额。
  5. 在我的autocomplete.js文件中,我使用了一些console.log,以便在浏览器的检查器中找到代码的哪些部分正在工作。 似乎一切都有效,除了’OnPlaceChange’回调中的console.logs,因为Gmap的自动完成建议没有显示……;
  6. 我知道没有看到自动完成建议不是与.pac-container css元素关联的z-index的问题,因为我已经不得不修复此问题,因为我的键盘箭头无法访问Gmaps建议。
  7. 我读过这篇关于geocomplete的post( rails 4的geocomplete不能在Heroku中工作 )并尝试预编译我的资产。 这并没有导致任何变化。
  8. 在我的浏览器检查器中(在“网络”部分中),当我在表单输入中写入内容时,键盘上按下的每个新字母都会触发新请求。 这些请求名称是’AutocompletionService.GetPredictions …..’。 在“标题”部分,我可以看到它们是对googleapis的https请求,包含get方法和200状态代码。 在“响应”部分,我可以看到我的API密钥似乎有错误,因为消息是:
 /**/_xdc_._ty4oqn && _xdc_._ty4oqn( [3,null,null,"This API project is not authorized to use this API. Please ensure that this API is activated in the APIs Console: https://console.developers.google.com/apis/library?project=_ Please ensure this API is activated in the Google Developers Console: https://console.developers.google.com/apis/api/places_backend?project=_ For more information on authentication and Google Maps Javascript API services please see: https://developers.google.com/maps/documentation/javascript/get-api-key"] ) 

我按照错误消息中的链接,将我引导到Google的控制台,然后回到我的项目和API密钥,这些密钥在服务器端和浏览器端都已确定。 再一次,我在Google的控制台中唯一没办法做的就是certificate我拥有自己的域名。

最后一点是问题的根源(在这种情况下,我想我需要托管服务的帮助)或者你们看到另一个错误吗?

我也有同样的问题。 只需转到谷歌控制台,启用“谷歌地方API Web服务”,然后它将解决问题。

在Google API控制台上启用“Google Places API Web服务”。 它解决了我的问题,当网站在localhost上工作,但不在Locaweb服务器。

Interesting Posts