2012-07-04 4 views
0

gmaps4rails gem을 사용하는 데 문제가 있습니다. 나는 javascript와 gmaps4rails의 혼합을 사용하고 있습니다. 다음은 내 코드 샘플입니다.자바 스크립트로 내 gmaps4rails에 마커 추가하기

 function handle_locations(position) { 

     alert("Length = " + Gmaps.map.markers.length); 
     var yourStartLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 

     var marker = new google.maps.Marker({position: yourStartLatLng}); 
     alert("Ola 2"); 

     if(document.getElementById("map") != null) 
      alert("diff"); 

     Gmaps.map.markers[0] = marker; 

     alert("Ola 333"); 
     //var map = $document.filter('#map'); 
     //var map = document.getElementById("map"); 
     //Gmaps.map.add_marker(marker); 
    } 

navigator.geolocation.getCurrentPosition(handle_locations); 

handle_locations 함수가 정상적으로 작동하고 내 위치를 얻을 수 있습니다. 문제는 gmaps를 사용하여 만든 맵에 마커를 추가하는 것입니다. 이 함수에서 맵에 내 geolocaton 마커를 어떻게 추가 할 수 있습니까?

답변

1

나는 비슷한 문제가 있었고 이것은 나를 위해 일했습니다.

lat = position.coords.latitude; 
lng = position.coords.longitude; 

Gmaps.map.callback = function() { 
    Gmaps.map.createMarker({ 
     Lat: lat, 
     Lng: lng, 
     rich_marker: null, 
     marker_picture: "" 
    }); 
} 

사용자의 위치를 ​​점점 asyncronous이고 시간이 걸릴 것이기 때문에 당신의 navigator.geolocation.getCurrentPosition(function(position) { 요청이 성공한 후에 만 ​​(propably 당신이 그렇게하고있다) position.coords.latitude/경도를 사용해야합니다.

당신은

alert(lat+" "+lng); 
(가) 오른쪽 위도와 LNG가 있는지 확인하실 수 있습니다
관련 문제