{"id":2231,"date":"2020-05-05T13:30:01","date_gmt":"2020-05-05T18:30:01","guid":{"rendered":"https:\/\/xlinesoft.com\/blog\/?p=2231"},"modified":"2026-08-22T16:53:31","modified_gmt":"2026-08-22T21:53:31","slug":"using-geolocation-data-in-your-web-application","status":"publish","type":"post","link":"https:\/\/xlinesoft.com\/blog\/2020\/05\/05\/using-geolocation-data-in-your-web-application\/","title":{"rendered":"Get a User&#8217;s Geolocation in a PHPRunner Web Application"},"content":{"rendered":"<p>A PHPRunner application can use the browser Geolocation API to request the user&#8217;s current latitude and longitude. Those coordinates can then be passed to the server and used for features such as nearby results, maps, service-area checks or location-aware filtering.<\/p>\n<p>The basic flow is:<\/p>\n<ol>\n<li>Request the user&#8217;s location in JavaScript.<\/li>\n<li>Read the latitude and longitude returned by the browser.<\/li>\n<li>Send those values to the server.<\/li>\n<li>Store or use the coordinates in the PHPRunner application.<\/li>\n<\/ol>\n<h2>Before you start<\/h2>\n<p>Browser geolocation requires the user&#8217;s permission. The browser may display a prompt asking whether the user wants to share their location, and your application should continue to work if permission is denied.<\/p>\n<p>Geolocation also requires a secure context. In production, the application should be served over <strong>HTTPS<\/strong>. Browsers generally allow geolocation on localhost for development as well.<\/p>\n<p>The accuracy of the returned coordinates depends on the user&#8217;s device and environment. A phone with GPS may provide a very accurate location, while a desktop computer may return a less precise estimate based on Wi-Fi or other available information.<\/p>\n<p><!--more--><\/p>\n<h2>1. Request the user&#8217;s location<\/h2>\n<p>Add the following code to the <strong>JavaScript OnLoad<\/strong> event of the page where you want to request the user&#8217;s location. The Login or Menu page can be a convenient choice if you want the coordinates available throughout the user&#8217;s session.<\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"javascript\" name=\"mshighlighter\" >\r\nif (navigator.geolocation) {\r\n\r\n    navigator.geolocation.getCurrentPosition(\r\n        locationFound,\r\n        locationError,\r\n        {\r\n            enableHighAccuracy: false,\r\n            timeout: 10000,\r\n            maximumAge: 60000\r\n        }\r\n    );\r\n\r\n} else {\r\n    console.log(\"Geolocation is not supported by this browser.\");\r\n}\r\n\r\nfunction locationFound(position) {\r\n\r\n    var lat = position.coords.latitude;\r\n    var lng = position.coords.longitude;\r\n\r\n    $.post(\"\", {\r\n        lat: lat,\r\n        lng: lng\r\n    });\r\n}\r\n\r\nfunction locationError(error) {\r\n\r\n    switch (error.code) {\r\n\r\n        case error.PERMISSION_DENIED:\r\n            console.log(\"Location permission was denied.\");\r\n            break;\r\n\r\n        case error.POSITION_UNAVAILABLE:\r\n            console.log(\"Location information is unavailable.\");\r\n            break;\r\n\r\n        case error.TIMEOUT:\r\n            console.log(\"The location request timed out.\");\r\n            break;\r\n\r\n        default:\r\n            console.log(\"Unable to retrieve the user's location.\");\r\n    }\r\n}<\/textarea><\/pre>\n<\/div>\n<p>The browser calls <strong>locationFound()<\/strong> when a location is available. The latitude and longitude are then posted back to the current application page.<\/p>\n<p>The error callback is important because location access is never guaranteed. The user can deny permission, the device may be unable to determine its location, or the request may take too long.<\/p>\n<h2>2. Store the coordinates on the server<\/h2>\n<p>The following example stores the coordinates in session variables. Add it to the <strong>AfterAppInit<\/strong> event.<\/p>\n<p><strong>PHP<\/strong><\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"php\" name=\"mshighlighter\" >\r\n$lat = postvalue(\"lat\");\r\n$lng = postvalue(\"lng\");\r\n\r\nif ($lat !== \"\" && $lng !== \"\") {\r\n    $_SESSION[\"lat\"] = $lat;\r\n    $_SESSION[\"lng\"] = $lng;\r\n}<\/textarea><\/pre>\n<\/div>\n<p>It is better to check whether the values were actually supplied instead of simply testing them as true or false. Latitude or longitude can legitimately be zero.<\/p>\n<p><strong>C#<\/strong><\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"\" name=\"mshighlighter\" >\r\ndynamic lat = MVCFunctions.postvalue(\"lat\");\r\ndynamic lng = MVCFunctions.postvalue(\"lng\");\r\n\r\nif(lat.ToString() != \"\" && lng.ToString() != \"\")\r\n{\r\n    XSession.Session[\"lat\"] = lat;\r\n    XSession.Session[\"lng\"] = lng;\r\n}<\/textarea><\/pre>\n<\/div>\n<h2>3. Use the location elsewhere in the application<\/h2>\n<p>Once the coordinates are stored in the session, they can be used by other pages and events in the application.<\/p>\n<p>For example, the PHP values are available as:<\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"php\" name=\"mshighlighter\" >\r\n$lat = $_SESSION[\"lat\"];\r\n$lng = $_SESSION[\"lng\"];<\/textarea><\/pre>\n<\/div>\n<p>You could use these coordinates to:<\/p>\n<ul>\n<li>show locations nearest to the current user;<\/li>\n<li>calculate the distance to stores, offices or service locations;<\/li>\n<li>display the user&#8217;s approximate position on a map;<\/li>\n<li>filter records to a geographic service area;<\/li>\n<li>save the location together with a newly created record.<\/li>\n<\/ul>\n<h2>Optional: store the coordinates in the database<\/h2>\n<p>Session variables are useful when the location only needs to be available temporarily. If the location should be associated with a user, order, visit or another record, save the coordinates to the appropriate database fields instead.<\/p>\n<p>For example, after obtaining the coordinates you could use PHPRunner&#8217;s Database API to update a record:<\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"php\" name=\"mshighlighter\" >\r\n$data = array();\r\n$data[\"latitude\"] = $_SESSION[\"lat\"];\r\n$data[\"longitude\"] = $_SESSION[\"lng\"];\r\n\r\nDB::Update(\"users\", $data, array(\"id\" => $_SESSION[\"UserID\"]));<\/textarea><\/pre>\n<\/div>\n<p>Adapt the table name, field names and key value to your own database structure.<\/p>\n<h2>Privacy and user experience<\/h2>\n<p>Location data can be sensitive, so request it only when the application has a clear reason to use it. It is usually better to request location at the point where it provides an obvious benefit rather than asking every visitor immediately after opening the application.<\/p>\n<p>Your application should also have a fallback for users who decline location access. Depending on the use case, this might be a manually entered address, ZIP code, city or other location selection.<\/p>\n<p>The browser Geolocation API gives PHPRunner applications a straightforward way to add location-aware functionality. The main considerations are permission, HTTPS, error handling and deciding how the coordinates should be used once they reach the server.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A PHPRunner application can use the browser Geolocation API to request the user&#8217;s current latitude and longitude. Those coordinates can then be passed to the server and used for features such as nearby results, maps, service-area checks or location-aware filtering. The basic flow is: Request the user&#8217;s location in JavaScript. Read the latitude and longitude returned by the browser. Send those values to the server. Store or use the coordinates in the PHPRunner application. Before you start Browser geolocation requires the user&#8217;s permission. The browser&#8230;<span class=\"clearfix clearfix-post\"><\/span><a href=\"https:\/\/xlinesoft.com\/blog\/2020\/05\/05\/using-geolocation-data-in-your-web-application\/\" class=\"more-link\">Continue Reading <span class=\"screen-reader-text\">&#8220;Get a User&#8217;s Geolocation in a PHPRunner Web Application&#8221;<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,1,8],"tags":[],"_links":{"self":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2231"}],"collection":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/comments?post=2231"}],"version-history":[{"count":8,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2231\/revisions"}],"predecessor-version":[{"id":3459,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2231\/revisions\/3459"}],"wp:attachment":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/media?parent=2231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/categories?post=2231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/tags?post=2231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}