Click here to Skip to main content
15,896,118 members

Refreshing google maps in javascript automatically

Member 12031462 asked:

Open original thread
I want to refresh location in google maps in javascript automatically if variables latitude and longitude present in the database changes.

My code : It is working fine. But it shows a static map. I want to show current location in database and not a static location. I added a refresh button. But it is not an effective method. So help me to refresh location automatically. Thanks in advance.

//code starts here //javascript code is in last

locate_driver.php

$did = $_POST[did];
$cid = $_POST[cid];

$con=mysqli_connect("mysql4.000webhost.com","username","password","database_name");

if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

if($cid==null)
{
$sql = "SELECT busno, latitude, longitude, noofpassengers FROM schedule where did=$did";
$result = mysqli_query($con,$sql) or die(mysql_error());

while ($array = mysqli_fetch_array($result))
{
$busno=$array['busno'];
echo " Bus No : ";
echo $busno; echo "
";
$lat = $array['latitude'];
echo "Latitude : ";
echo $lat; echo "
";
$long=$array['longitude'];
echo "Longitude : ";
echo $long; echo "
";
$nop=$array['noofpassengers'];
if($nop!=null)
{
echo " No of passengers : ";
echo $nop; echo "
";
}
else
{ echo "NA"; }
}
}

else
{
$sql = "SELECT busno, latitude, longitude, noofpassengers FROM schedule where cid=$cid";
$result = mysqli_query($con,$sql) or die(mysql_error());

while ($array = mysqli_fetch_array($result))
{
$busno=$array['busno'];
echo " Bus No : ";
echo $busno; echo "
";
$lat = $array['latitude'];
echo "Latitude : ";
echo $lat; echo "
";
$long=$array['longitude'];
echo "Longitude : ";
echo $long; echo "
";
$nop=$array['noofpassengers'];
echo " No of passengers : ";
echo $nop; echo "

";
}
}

?>

<html>
<body onload="sp()">



<form action="locate_driver.php" method="POST">
<input type="hidden" name="did" value="">
<input type="hidden" name="cid" value="">
<input type="submit" value="Refresh">
</form>



<script src="http://maps.google.com/maps/api/js?sensor=false"></script>

<script>
var x = document.getElementById("demo");

function sp() {
lat = ;
lon = ;
latlon = new google.maps.LatLng(lat, lon)
mapholder = document.getElementById('mapholder')
mapholder.style.height = '320px';
mapholder.style.width = '270px';

var myOptions = {
center:latlon,zoom:14,
mapTypeId:google.maps.MapTypeId.ROADMAP,
mapTypeControl:false,
navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL}
}

var map = new google.maps.Map(document.getElementById("mapholder"), myOptions);
var marker = new google.maps.Marker({position:latlon,map:map,title:"Bus is here!"});
}

function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}
</script>

</body>
</html>







Tags: Javascript, jQuery, PHP, Google Map, Location

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900