doulianqi3870 2015-12-06 21:07
浏览 35
已采纳

Laravel 5 Geolocation Dynamic Page

Here's the main issue:

I need to render either a basic or dynamic page depending on whether or not the user allows geolocation. The dynamic page will make a number of calls through Google Maps API, thus changing the content of the page. Where and how should I access geolocation, and make the respective API calls, while minimizing the negative effect on page load time. I'm not asking for API calls, just where can I make them in this framework and workflow? My main issue is that geolocation is activated via JavaScript which is handled on the client side while the main app is built on PHP which is handled on the serverside as explained here.

My current execution order is as follows:

1: User requests domain.app/ which is given to route

2: Route::get('/', 'NavigationController@indexWithoutGeoLocation');

3: NavigationController.php

/* Load the home page without geolocation activated */
public function indexWithoutGeoLocation(){
  $theaters = Theater::get();
  return view('home',[
    'theaters' => $theaters
  ]); 
}

Theaters is a table which only uses name and address for this code. The theater's address will be compared to the user's geolocation.

  1. domain.app/

Geolocation must be requested from the user so I include the following in the <head> of the page:

    <script>
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        } else {
            x.innerHTML = "Geolocation is not supported by this browser.";
        }
        function showPosition(position) {
            alert("Latitude: " + position.coords.latitude + 
            "<br>Longitude: " + position.coords.longitude); 
        }
    </script>

GeoCoder doesn't solve anything as I still have to transfer the JS to PHP.

My guess is to assign the contents of the PHP $Theaters that are passed when the view is loaded to a JS var Theaters. Then I'd be able to make all the Google Maps API calls in only JS. Serving the page becomes an issue now as it is in PHP and is completed before the JS even begins executing, resulting in the need to reload the page or use AJAX to erase the preloaded contents with the new data.

I'm new to Laravel and this seems a bit too hacky, so I came to SO for assistance, thanks!

  • 写回答

1条回答 默认 最新

  • duanjiebian6712 2015-12-07 01:17
    关注

    i have similiar app that need to handle some logic of user location on server side, and of course you can handle your logic from PHP, you only need to pass your lat and long from your page to your controller

     $.ajax({
            url: "{{URL::to('your-routes')}}",
            type: 'GET',
            data: {
                lat: position.coords.latitude,
                long: position.coords.longitude
            },
            success: function(data) {
                // you can manipulation your html here
            }
        });
    

    And if you need to calculate the distance of user geolocation and some location you can use this function

     function distance($lat1, $lon1, $lat2, $lon2, $unit) { 
    
     $theta = $lon1 - $lon2; 
     $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) *    cos(deg2rad($lat2)) * cos(deg2rad($theta)); 
     $dist = acos($dist); 
     $dist = rad2deg($dist); 
     $miles = $dist * 60 * 1.1515;
     $unit = strtoupper($unit);
    
       if ($unit == "K") {
         return ($miles * 1.609344); 
       } else if ($unit == "N") {
         return ($miles * 0.8684);
       } else {
         return $miles;
       }
    
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。