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 server
side 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.
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!