2017-11-18 2 views
1

현재 위치를 기반으로 근처의 호텔에 방문자를 추천하고 싶습니다. 내 문제는 로그인 사용자 위도와 경도를 얻지 못하게하여 근처 호텔에 추천 할 수있게하는 것입니다.컨트롤러에서 Ajax 데이터를 보내는 방법

home.blade.php

나는 컨트롤러 컨트롤러를 널 가지고
<script> 
    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(function (position) { 
      console.log(position); 
      var lat = position.coords.latitude; 
      var lng = position.coords.longitude; 

      $.ajax({ 
       url:'http://localhost:8000/getgeo', 
       type:'get', 
       data:{latitude:lat,longitude:lng}, 

//even when i set some value here data:{latitude:1222323}, i did not get `1222323` in controller 


       success:function(data) 
       { 
        alert('success'); 
       } 

      }); 
     }); 
    } 

    </script> 

: 여기

public function geo() 
{ 
    return view('home'); 

} 
public function getCoordinate(Request $request) 
{ 
    return $request->latitude; 
} 

내가 지금 home.blade.php 페이지 나누었다 콘솔에서 위도와 경도를 얻고있다 컨트롤러에있어

경로 :

Route::get('/geo', '[email protected]'); 

Route::get('/getgeo', '[email protected]'); 
+0

는 리턴 기능 당신은 자신의 IP 주소를 사용하여 지리적 위치를 사용할 필요가 – kunal

+0

전에 모든 요청 데이터를 에코있다. 그가 지금 어디에 있는지 계산할 수있는 양식. 사용자는 로그인 할 필요가 없습니다. 예 : 모바일의 Google지도처럼. 그것은 GPS/IP 주소를 사용하여 위치를 얻는 데 사용됩니다. 'getgeo'값을 에코 당신이 –

+0

을 이해 희망하는 – jeff

답변

1

변경 다음 사항 : -

change ajax url : url:'/getgeo' 
And function :- 
public function getCoordinate(Request $request) 
{ 
    if($request->ajax()){ 
    $data = $request->all(); 
    echo "<pre>"; print_r($data); // print all data here 
    } 

} 
Try to change Route:- 
Route::any('/getgeo', '[email protected]'); 
+0

아직 내 업데이트 된 대답 –

+0

는 "어떤 볼에 도착"경로를 변경하는 것은 시도하지있어 – kunal

+0

내가 –