我想根据他目前的位置向游客推荐他附近的酒店。我的问题是如何获得未登录用户的纬度和经度,以便我可以推荐他附近的酒店。
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>我在控制器控制器中得到了null:
public function geo()
{
return view('home');
}
public function getCoordinate(Request $request)
{
return $request->latitude;
}在这里,我在控制台的home.blade.php页面中获得了纬度和经度,.But现在进入了控制器
路由:
Route::get('/geo', 'ProductController@geo');
Route::get('/getgeo', 'ProductController@getCoordinate');发布于 2017-11-18 15:43:01
更改以下几点:
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');https://stackoverflow.com/questions/47363709
复制相似问题