3

레일즈 개발자의 주니어 루비입니다. 나는 Google지도 자바 스크립트 API를 사용하는 웹 응용 프로그램을 개발했으며 양식 입력에 자동 완성 기능을 사용합니다.Google지도 자동 완성 js는 localhost에서 작동하지만 heroku에서는 작동하지 않습니다.

자동 완성 기능은 localhost에서 잘 작동하지만 heroku에 배포 한 후에는 더 이상 작동하지 않습니다.

아래 내 html.erb입니다. 완벽한 정보를 얻으려면 양식이 페이지 바닥 글에 있습니다. 이 양식은 부분적으로 숨겨져 있으며 표시되는 유일한 부분은 비디오 파일을 업로드하는 데 사용되는 단추에 해당합니다. 이 버튼을 클릭하고 비디오 파일을 선택하면 나머지 양식이 포함 된 모달이 표시됩니다. 그것이 'text_field_tag'(id = "user_input_autocomplete_address")가 표시되는 곳이며 자동 완성이 작동해야하는 곳입니다.

<% if user_signed_in? %> 
 

 
<div class="footer-check hidden-md hidden-lg"> 
 
    <div class="container text-right"> 
 
    <div class="row"> 
 
     <div class="flexbox"> 
 
     <div class="footer-home active"> 
 
      <%= link_to root_path do %> 
 
      <i class="fa fa-home" aria-hidden="true"></i> 
 
      <% end %> 
 
     </div> 
 
     <div class="footer-search"> 
 
     <!-- later put link_to reviews_path --> 
 
      <%= link_to search_path do %> 
 
      <i class="fa fa-search" aria-hidden="true"></i> 
 
      <% end %> 
 
     </div> 
 
     <div class="footer-button"> 
 
     <%= simple_form_for(Review.new) do |f| %> 
 
      <div id="add_video_image_btn"> 
 
      <label for="review_video", class="add_video_image"><%= image_tag "plus.png" %></label> 
 
      <%= f.input :video, label: false, input_html: { accept: ".mp4, .mov, .m4v, .wmv, .webm, .avi", class: 'hidden'} %> 
 
      <%= f.input :video_cache, as: :hidden, class: "add_video_image file required" %> 
 
      </div> 
 
     </div> 
 
     <div class="footer-likes"> 
 
      <%= link_to dashboard_users_path do %> 
 
      <i class="fa fa-heart" aria-hidden="true"></i> 
 
      <% end %> 
 
     </div> 
 

 
     <div class="footer-user"> 
 
      <%= link_to user_path(current_user) do %> 
 
      <i class="fa fa-user" aria-hidden="true"></i> 
 
      <% end %> 
 
     </div> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div> 
 

 
<!--Beginning Modal --> 
 
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
 
<div class="modal-dialog" role="document"> 
 
    <div class="modal-content"> 
 
    <div class="modal-body"> 
 
     <div id="add_video_form_step2"> 
 
     <%= f.error_notification %> 
 

 
     <fieldset class="form-group" style="margin-bottom:0;"> 
 

 
      <div class="section"> 
 
      Place 
 
      </div> 
 
      <div class="inner-wrap"> 
 
      <div id=""> 
 
       <%= text_field_tag :place, nil, class: 'form-control', id: 'user_input_autocomplete_address', placeholder: 'Start typing...', size: 80 %> 
 
      </div> 
 
      </div> 
 

 
      <div class="section"> 
 
      Comment 
 
      </div> 
 
      <div class="inner-wrap"> 
 
      <%= text_field_tag :comment, nil, class:'form-control', placeholder: 'Food is good but the place is a bit noisy', size: 80 %> 
 
      </div> 
 

 
      <div class="inner-wrap" style="padding:15px;"> 
 
      <div class="emoji-toggle emoji-happy"> 
 
       <input type="checkbox" id="toggle1" class="toggle" name="review[mood]" value="false"> 
 
       <div class="emoji"></div> 
 
       <label for="toggle1" class="well"></label> 
 
      </div> 
 
      </div> 
 

 
      <div class=""> 
 
      <%= f.button :submit, class: "btn btn-primary form-control", id: "click-trigger" %> 
 
      </div> 
 

 
      <%= hidden_field_tag :name, id: 'name' %> 
 
      <%= hidden_field_tag :street_number, id: 'street_number' %> 
 
      <%= hidden_field_tag :route, id: 'route' %> 
 
      <%= hidden_field_tag :locality, id: 'locality' %> 
 
      <%= hidden_field_tag :administrative_area_level_1, id: 'administrative_area_level_1' %> 
 
      <%= hidden_field_tag :postal_code, id: 'category_0' %> 
 
      <%= hidden_field_tag :country, id: 'country' %> 
 
      <%= hidden_field_tag :formatted_address, id: 'formatted_address' %> 
 
      <%= hidden_field_tag :phone_number, id: 'phone_number' %> 
 
      <%= hidden_field_tag :website, id: 'website' %> 
 
      <%= hidden_field_tag :gplace_id, id: 'gplace_id' %> 
 
      <%= hidden_field_tag :category_0, id: 'category_0' %> 
 
      <%= hidden_field_tag :category_1, id: 'category_1' %> 
 
      <%= hidden_field_tag :category_2, id: 'category_2' %> 
 
      <%= hidden_field_tag :category_3, id: 'category_3' %> 
 
      <%= hidden_field_tag :category_4, id: 'category_4' %> 
 
      <%= hidden_field_tag :category_5, id: 'category_5' %> 
 
      <%= hidden_field_tag :hours_open_day_0, id: 'hours_open_day_0' %> 
 
      <%= hidden_field_tag :hours_open_day_1, id: 'hours_open_day_1' %> 
 
      <%= hidden_field_tag :hours_open_day_2, id: 'hours_open_day_2' %> 
 
      <%= hidden_field_tag :hours_open_day_3, id: 'hours_open_day_3' %> 
 
      <%= hidden_field_tag :hours_open_day_4, id: 'hours_open_day_4' %> 
 
      <%= hidden_field_tag :hours_open_day_5, id: 'hours_open_day_5' %> 
 
      <%= hidden_field_tag :hours_open_day_6, id: 'hours_open_day_6' %> 
 
     </fieldset> 
 
     </div> 
 
    </div> 
 
    </div> 
 
    </div> 
 
</div> 
 

 
     </div> 
 
     </div> 
 
    </div> 
 
    </div> 
 
<% end %> 
 
<% end %> 
 

 
<!--End Modal --> 
 

 
<!-- Start of js to display the modal --> 
 
<!-- NB: the autocomplete module is in assets/javascript/autocomplete.js --> 
 
<%= content_for :after_js do %> 
 
    <script> 
 
    $('#review_video').change(function(){ 
 
     $('#myModal').modal('show'); 
 
    }); 
 
    </script> 
 
<% end %> 
 
<!-- End of js to display the modal -->

푸터 (모달 폼 수집)

아래 인 레이아웃/application.html.erb 뷰 내에서 호출된다. 당신은 내가 거기에 구글 API 키를 사용하여 (application.yml에 숨겨진) 것을 볼 수 있습니다

<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
    <title><%= meta_title %></title> 
 
    <%= csrf_meta_tags %> 
 

 
    <!-- Start Favicon --> 
 

 
    <%= favicon_link_tag '_/app/assets/images/favicon.ico' %> 
 

 
    <link rel="manifest" href="/manifest.json"> 
 
    <meta name="msapplication-TileColor" content="#ffffff"> 
 
    <meta name="msapplication-TileImage" content="<%= image_path 'ms-icon-144x144.png' %>"> 
 
    <meta name="theme-color" content="#ffffff"> 
 
    <!-- end Favicon --> 
 

 
    <meta name="description" content="<%= meta_description %>"> 
 

 
    <!-- Facebook Open Graph data --> 
 
    <meta property="og:title" content="<%= meta_title %>" /> 
 
    <meta property="og:type" content="website" /> 
 
    <meta property="og:url" content="<%= request.original_url %>" /> 
 
    <meta property="og:image" content="<%= meta_image %>" /> 
 
    <meta property="og:description" content="<%= meta_description %>" /> 
 
    <meta property="og:site_name" content="<%= meta_title %>" /> 
 

 
    <!-- Twitter Card data --> 
 
    <meta name="twitter:card" content="summary_large_image"> 
 
    <meta name="twitter:site" content="<%= DEFAULT_META["twitter_account"] %>"> 
 
    <meta name="twitter:title" content="<%= meta_title %>"> 
 
    <meta name="twitter:description" content="<%= meta_description %>"> 
 
    <meta name="twitter:creator" content="<%= DEFAULT_META["twitter_account"] %>"> 
 
    <meta name="twitter:image:src" content="<%= meta_image %>"> 
 

 
    <!-- Google+ Schema.org markup --> 
 
    <meta itemprop="name" content="<%= meta_title %>"> 
 
    <meta itemprop="description" content="<%= meta_description %>"> 
 
    <meta itemprop="image" content="<%= meta_image %>"> 
 

 

 

 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
 
    <link rel="apple-touch-icon" sizes="152x152" href="/apple-icon-152x152.png"> 
 
    <%= stylesheet_link_tag 'application', media: 'all' %> 
 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
 

 
    <!-- start Mixpanel --> 
 
    <script type="text/javascript">(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']"); 
 
    </script> 
 
    <!-- end Mixpanel --> 
 

 
    </head> 
 
    <body> 
 
    <%= render 'shared/flashes' %> 
 
    <%= yield %> 
 
    <%= render 'shared/footer' %> 
 
    <%= javascript_include_tag "http://maps.google.com/maps/api/js?libraries=places&key=#{ENV['GMAP_BROWSER_KEY']}" %> 
 
    <%= javascript_include_tag "http://cdn.rawgit.com/mahnunchik/markerclustererplus/master/dist/markerclusterer.min.js" %> 
 
    <%= javascript_include_tag 'application' %> 
 
    <%= yield :after_js %> 
 
    </body> 
 
</html>

마지막으로, 입력의 자동 완성을 허용하는 자바 스크립트 코드 (ID로 "user_input_autocomplete_address"를 가진

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'); 
 
});

:) 다음과 같은 자산/자바 스크립트/autocomplete.js에와있다 Heroku가 모든이를 배포하기 위해

, 나는 이미 한 다음

  1. : (좀중인 마이그레이션을했기 때문에) 터미널에서 "Heroku가 실행 DB 마이그레이션";
  2. "figaro heroku : set -e production"은 터미널에서 모든 API 키를 heroku로 푸시하기 위해 (이 키는 실제로 heroku의 config vars에 있음을 확인했습니다).
  3. Google 개발자 콘솔, 프로젝트 및 Google지도 javascript API 내에서 내 브라우저 키가 내 locahost와 내 도메인 이름 모두에 대해 사용 가능하도록했습니다. 매우 정확하기 위해서 나는 나의 로카 스트와 도메인 네임에 대해 같은 키를 사용하고있다. 지금까지 Google의 콘솔에서 내가 할 수 없었던 유일한 방법은 내가 내 도메인 이름을 소유하고 있음을 증명하는 것입니다.
  4. 확인했습니다. 아직 내 API 할당량에 도달하지 못했습니다.
  5. 내 autocomplete.js 파일에서 브라우저의 관리자가 코드의 어떤 부분이 작동하는지 알아 내기 위해 console.log를 사용했습니다. Gmap의 자동 완성 제안은 표시되지 않으므로 'OnPlaceChange'콜백에있는 console.logs를 제외하고 모든 것이 작동하는 것 같습니다.
  6. 자동 완성 제안이 .pac-container CSS 요소와 관련된 Z- 색인의 문제가 아니라는 것을 알고 있습니다.이 문제를 이미 해결해야하고 키보드 화살표 덕분에 Gmaps 제안에 도달 할 수 없기 때문입니다.
  7. geocomplete (geocomplete for rails 4 not working in Heroku)에 대한이 게시물을 읽고 자산을 사전 컴파일하려고했습니다. 그것은 어떤 변화도 가져 오지 않았습니다.
  8. 나의 브라우저 인스펙터 ('네트워크'섹션에 있음)에서, 양식 입력에 무언가를 쓸 때, 키보드에서 누를 때마다 새로운 요청이 트리거됩니다. 이러한 요청 이름은 'AutocompletionService.GetPredictions .....'입니다. "헤더"섹션에서 get 메소드 및 200 개의 상태 코드와 함께 googleapis에 대한 https 요청임을 확인할 수 있습니다.

/**/_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"])
내가 구글의 날을 선도하고, 오류 메시지의 링크를 따라하십시오 "응답"섹션에서는 메시지이기 때문에 내 API 키를 사용하여 오류가있는 것 같습니다 볼 수 있습니다 콘솔로 돌아가서 서버 측과 브라우저 측에서 모두 해결되는 프로젝트 및 API 키로 돌아갑니다. 다시 한번, Google 콘솔에서 할 수 없었던 유일한 것은 내 도메인 이름을 소유하고 있음을 증명하는 것이 었습니다.

이 마지막 포인트는 문제의 원점입니까 (이 경우 내 호스팅 서비스에서 도움이 필요합니다), 아니면 다른 실수를하는 사람입니까?

답변

6

나도 같은 문제가 있습니다. 그냥 Google 콘솔로 이동하여 'Google Places API 웹 서비스'를 사용 설정하면 문제가 해결됩니다.

+0

당신이 완전합니다! 고마워요! 그럼 어떻게 설명할까요? Google 지오 코딩 된 주소와 시설을 모두 찾고 있기 때문에 Google Places API 웹 서비스가 Google Javascript API 외에도 암시 적으로 호출됩니다. 맞습니까? – Quentin

+0

5 시간 후에 찢어진 머리카락을 내 하루 만에 저장했습니다. 고마워 친구! – domoarrigato

0

Google API 콘솔에서 'Google Places API 웹 서비스'를 사용하도록 설정합니다. 웹 사이트가 localhost에서 작동하지만 Locaweb 서버에서는 작동하지 않을 때 문제가 해결되었습니다.