I am developping a web application using Code Igniter 4. For some reason, I would like to get the user geolocalisation coordinates (latitude and longitude) in decimal angles.
I can define user coordinates manually (by entering numbers) but I would like to have the browser agent give them automatically when clicking on 'get_coords' button to make the user experience more friendly. Let's avoid users to have to go on Google Maps to get their coordinates and come back to my web app page.
I have tried some methods but I can't make it work. My analysis is that the position rendered by getLocation script function is not stored in the $_SESSION['USER_COORDS'] variable.
While running tests, I have got the"Permission denied by the user"
error but the browser has never asked me to authorise geolocalisation use. The button does not do anything.
Both make_latitude and make_longitude functions are helper functions used to transform a coordinates string ('49.12345, 2.35468') in two decimal angles ('49.12345' and '2.35468').
In the rest of my application, I display error and success notifications by declaring session() FlashData in the controller before rendering the view :
$this->data['session']->setFlashdata('msg', "My error message");
$this->data['session']->setFlashdata('msg_type', "error");
return view ('myview', $this->data);
Here is my code so far.
Controller file :
// Get user position
if($this->request->getVar('user-localise')){
if($this->request->getVar('user-latitude') && $this->request->getVar('user-longitude')) {
session()->set('USER_COORDS', $this->request->getVar('user-latitude') . ", " . $this->request->getVar('user-longitude'));
} else {
session()->set('USER_COORDS', null);
}
}
View file :
<?= $this->extend('layouts/main.php');?>
<?= $this->section('content'); ?>
<div class="mb-3">
<?php if(session()->getFlashdata('msg')):?>
<?php echo view("errors/alert",['session' => session()]);?>
<?php endif;?>
</div>
<form method = "post">
<div class = "cf-spar mb-3" style="margin-top:20px">
<input type="hidden" name="user-localise" value="true">
<button id="get_coords" onclick="getLocation()" class="btn btn-primary btn-md" style="margin-right:10px" type="submit"><?= lang("locale.user-position-me")?></button>
<?php if($agent->isMobile()):?>
<input type="text" id="user_lat" name="user-latitude" class="form-control" style="margin-right:10px" placeholder=<?= "'" . lang("locale.user-position-latitude-short") . "'" ?> value=<?= isset($_SESSION['USER_COORDS']) ? "'" . make_latitude($_SESSION['USER_COORDS']) . "'" : "" ?>>
<input type="text" id="user_lon" name="user-longitude" class="form-control" style="margin-right:10px" placeholder=<?= "'" . lang("locale.user-position-longitude-short") . "'" ?> value=<?= isset($_SESSION['USER_COORDS']) ? "'" . make_longitude($_SESSION['USER_COORDS']) . "'" : "" ?>>
<?php else:?>
<input type="text" id="user_lat" name="user-latitude" class="form-control" style="margin-right:10px" placeholder=<?= "'" . lang("locale.user-position-latitude-long") . "'" ?> value=<?= isset($_SESSION['USER_COORDS']) ? "'" . make_latitude($_SESSION['USER_COORDS']) . "'" : "" ?>>
<input type="text" id="user_lon" name="user-longitude" class="form-control" style="margin-right:10px" placeholder=<?= "'" . lang("locale.user-position-longitude-long") . "'" ?> value=<?= isset($_SESSION['USER_COORDS']) ? "'" . make_longitude($_SESSION['USER_COORDS']) . "'" : "" ?>>
<?php endif;?>
<button type="submit" name="save-user-coords" class = "btn btn-success btn-md"><?= lang("locale.save") ?></button>
</div>
</form>
<script>
var x = document.getElementById("get_coords");
function getLocation(){
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition,showError);
}
}
function showPosition(position){
lat = position.coords.latitude;
lon = position.coords.longitude;
document.getElementById("user_lat").value = lat;
document.getElementById("user_lon").value = lon;
}
function showError(error){
switch(error.code){
case error.PERMISSION_DENIED:
Session['msg']="Permission denied by user."
break;
case error.POSITION_UNAVAILABLE:
Session['msg']="Position is unavailable."
break;
case error.TIMEOUT:
Session['msg']="Timeout delay."
break;
case error.UNKNOWN_ERROR:
Session['msg']="Unknown error."
break;
}
Session['msg_type'] = "error";
}
</script>
<?= $this->endSection('content'); ?>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
CodeIgniter is a framework that is known for requiring a minimum amount of customization to get it up and running. This allows those who choose it to work at a good pace. It has been updated many times since its inception in 2006. Now the most recent version is 4.0.3.
https://www.codeigniter.com/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.