1

Rails 3.2.13, ruby ​​1.9.3p374 및 Gmaps4rails 1.5.6을 사용합니다.gmaps4rails : 마커에 클릭 가능한 링크를 구현하는 방법

나는 처음에는 상당히 똑바로 여겨야한다고 생각하여 꾸짖습니다. 클릭하면 특정 경로로 리디렉션되고 같은 경로에 경로가로드되는 표식을 표시하려고합니다.

Gmaps4rails에는 해당 description의 쉬운 옵션이 없습니다. 그래서 웹 검색에 셀 수없이 많은 시간을 보냈고 여기에서 StackOverflow : this의 올바른 냄새가 나는 곳에서 발견 된 두 가지 베스트 히트 인 this one도 유용하게 보입니다. 하여 처리 HTML에서 다음 생성이 제대로입니다

@gmapsjson = current_user.houses.all.to_gmaps4rails do | house, marker | 
    marker.title house.reference 
    marker.json({:link => polymorphic_url(house, :routing_type => :path)}) 
end 

(내 생각) : 결국

, 내 컨트롤러처럼 보이는

<script src="//maps.google.com/maps/api/js?v=3.8&amp;sensor=false&amp;client=&amp;key=&amp;libraries=geometry&amp;language=&amp;hl=&amp;region=" type="text/javascript"></script> 

<script type="text/javascript"> 
    Gmaps.map = new Gmaps4RailsGoogle(); 
    Gmaps.load_map = function() { 
    Gmaps.map.map_options.auto_zoom = false; 
    Gmaps.map.map_options.zoom = 3; 
    Gmaps.map.initialize(); 
    Gmaps.map.markers = [{"title":"First House","link":"/houses/1","lat":-3.4671425,"lng":12.5264373},{"title":"Second House","link":"/houses/2","lat":-4.5543296,"lng":-3.4151647}]; 
    Gmaps.map.create_markers(); 
    Gmaps.map.adjustMapToBounds(); 
    Gmaps.map.callback(); 
}; 
Gmaps.oldOnload = window.onload; 
    window.onload = function() { Gmaps.triggerOldOnload(); Gmaps.loadMaps(); }; 
</script> 

마커가 적절한 장소에 나타납니다. 그러나 이제 그 first relevant reference에 설명 해석 나는 결과 HTML에서 덤프는 다른 모든 자바 스크립트의 Gmaps4Rails 아래에 표시됩니다 확인하고,보기에 직접 입력 :

<script type="text/javascript"> 
    function redirect_to(url) { 
    window.location = url 
    }; 
    Gmaps.callback = function() { 
    for (var i = 0; i < Gmaps.map.markers.length; ++i) { 
     google.maps.event.addListener(Gmaps.map.markers[i].google_object, 'click', redirect_to(Gmaps.map.markers[i].link)); 
    } 
}; 
</script> 

하지만 지금은 내가 페이지를로드 할 때마다, 내 파이어 폭스 자바 스크립트 콘솔은 말할 것이다 :

TypeError: Gmaps[load_function_name] is not a function 

for (key in Gmaps) { 
    value = Gmaps[key]; 
    searchLoadIncluded = key.search(/load/); 
    if (searchLoadIncluded === -1) { 
    load_function_name = "load_" + key; 
    _results.push(Gmaps[load_function_name]());   <=== Points to this line 
    } else { 
    _results.push(void 0); 
    } 
} 

그래서, 무엇이 잘못 되었습니까? 사전에

감사합니다,

마르코

+0

이 답변을 시도 했습니까? http://stackoverflow.com/questions/8608602/make-map-marker-direct-link-onclick-for-gmaps4rails – Benj

+0

나는 그랬다. 그것은 원래 질문에서 "이것도 유용 할 것 같다"라고 언급되었습니다. 하지만 내 관심을 불러 주셔서 감사합니다. :) 그 접근 방식은 자바 스크립트 콘솔에서 알려지지 않은 "google"을 주었다. –

답변

6

오히려 당신이 당신의 컨트롤러에 링크되어 포함한 모든 HTML을 생성 할 수 있습니다 자바 스크립트에서하는 것보다. 참고 : 링크는 내가 따로 설정 올바르게 생성과 변수

이 내 locations_controller.rb에 포함 된 무엇에 할당하려면 :

@json = Location.all.to_gmaps4rails do |location, marker| 
    location_link = view_context.link_to location.name, location_path(location) 
    marker.title location.name 
    marker.infowindow "<h4><u>#{location_link}</u></h4> 
         <i>#{location.address}</i>" 
end 

당신이 올바르게 설정하면 당신이 얻을 것이다 이

{"title":"Home", 
"description":"<h4><u><a href=\"/locations/1\">Place</a></u></h4> 
<i>123 E Main St. Anytown, USA</i>", 
"lat":29.585019, 
"lng":-81.319479} 

위키 페이지 here처럼 보일 생성 된 해시가 날 도왔습니다. 희망이 당신을 도울