1

I want to recommend the visitors the hotels near by him based on his current location. My problem is how to get not login user latitude and longitude so that i can recommend him nearby hotel.

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>

I got null in controller controller:

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

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

Here i am getting latitude and longitude at console in home.blade.php page .But now got in controller

Route:

Route::get('/geo', 'ProductController@geo');

Route::get('/getgeo', 'ProductController@getCoordinate');
4
  • have you echo all request data before return function Commented Nov 18, 2017 at 7:39
  • You have to use Geo location using their ip address. Form that you can calculate where he is now. You don't need user to login. Eg. Like a google map in mobile. It use to get location using GPS / IP address. Hope you understand Commented Nov 18, 2017 at 7:42
  • echo out the values that 'getgeo' is receiving Commented Nov 18, 2017 at 7:43
  • i got empty array [ ] Commented Nov 18, 2017 at 7:57

1 Answer 1

1

Change following points:-

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', 'ProductController@getCoordinate');
Sign up to request clarification or add additional context in comments.

4 Comments

i got no thing,only blank page
try to change route "get to any see" my updated answer
i have to send automatic ajax request to particular URL without any user click event. I mean if user lands on home page,then send their lat and longitude to particular controller.user do not click any button to send request,there should be automatic process to transfer latitude and longitude to controller.(ie /getgeo url)
are you sure you are getting latitude and longitude? if you want to run on page loads.... you have to put your script in ready function ... $(document).ready(function){ ...... });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.