New to our community ?

Discover a world of possibilities! Join us and explore a vibrant community where ideas flourish and connections thrive.

One of Our Valued Members

Thank you for being part of our community. Your presence enriches our shared experiences. Let's continue this journey together!

Home php codes show active visitor on google map using php

show active visitor on google map using php


Welcome back to, In our previous post we learn how to change password script is run in php.
Today we will learn how to get the user’s current location from the browser and show multiple locations mark on the map.
get visitor location using php
before starting this tutorial, first of all, we make a scenario to perform this task. we split this task into 4 parts.

in our first part, we create a database (‘demo‘) and a database table with the name of (location). in a database table (location) we have 3 entities (id),(latitude),(longitude).

in the second part we create a page index.php . in the index.php page, we put our javascript and Ajax code in the head section. the javascript code
helps us to fetch user’s location from the browser and the ajax code is helpful to store the location into our database table.
in the third part, we create a page getLocation.php which is a help to insert ajax data into the database
and in the final part we create a page showlocation.php. in the showlocation.php page, we fetch the users location from database and
shows on the google map with location mark.


<!DOCTYPE html>
<html lang="en-US">
<title>Get Visitor Location using HTML5</title>
<script src=""></script>


    if (navigator.geolocation) {
    } else { 
        $('#location').html('Geolocation is not supported by this browser.');

function showLocation(position) {
    var latitude = position.coords.latitude;
	var longitude = position.coords.longitude;

                $("#location").html('Not Available');

<style type="text/css">
	p{ border: 2px dashed #009755; width: auto; padding: 10px; font-size: 18px; border-radius: 5px; color: #FF7361;}
    span.label{ font-weight: bold; color: #000;}
    <p><span class="label">Your Geo-location:</span> <span id="location"></span></p>


if(!empty($_POST['latitude']) && !empty($_POST['longitude'])){
    $con= mysqli_connect("localhost","root","root","demo");
    $qry="INSERT INTO `demo`.`location` (`latitude`, `longitude`) VALUES ('".$_POST['latitude']."', '".$_POST['longitude']."');";
    //Send request and receive json data by latitude and longitude
    $url = ''.trim($_POST['latitude']).','.trim($_POST['longitude']).'&sensor=true';
    $json = @file_get_contents($url);
    $data = json_decode($json);
    $status = $data->status;
        //Get address from json data
        $location = $data->results[0]->formatted_address;
        $location =  '';
    // dipslay address
    echo $location;


<!DOCTYPE html>

       <script src="" type="text/javascript"></script><script type="text/javascript">// <![CDATA[

        $geq="SELECT * FROM location";

        $e = "";
          $e[]= array("lat" => $rt['latitude'],"lng"=>$rt['longitude']);
var markers =<?php echo json_encode($e); ?>;
window.onload = function () {
var mapOptions = {
center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
var infoWindow = new google.maps.InfoWindow();
var lat_lng = new Array();
var latlngbounds = new google.maps.LatLngBounds();
for (i = 0; i < markers.length; i++) {
var data = markers[i]
var myLatlng = new google.maps.LatLng(, data.lng);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: data.title
(function (marker, data) {
google.maps.event.addListener(marker, "click", function (e) {
infoWindow.setContent(data.description);, marker);
})(marker, data);


// ]]></script></pre>
<div id="dvMap" style="width: 500px; height: 500px;"></div>