I'm trying to show an login user's location who logged in and update the location every 12 seconds. The user login with php session.
Heres the code i try (i edited it from a code who show all user, i want to show only one user who login + objects):
function initialize () { <?php $sql = "select username from user where username=id='$_SESSION[id]'"; $query = mysqli_query($connection, $sql); while ($data = mysqli_fetch_array($query)) { ?> var marker = new google.maps.Marker({ position: new google.maps.LatLng(<?php echo $data['lat']; ?>, <?php echo $data['lon']; ?>), map: map }); markers.push(marker); var infowindow = new google.maps.InfoWindow({ content: "<img src='<?php echo $data['photo']; ?>' width='100' align='left' /><?php echo $data['status']; ?>", size: new google.maps.Size(50, 50), position: new google.maps.LatLng(<?php echo $data['lat']; ?>, <?php echo $data['lon']; ?>) }); infowindow.open(map); infowindows.push(infowindow); <?php } ?> } var markers=new Array(); var infowindows=new Array(); var refreshId2 = setInterval(function(){navigator.geolocation.getCurrentPosition(foundLocation, noLocation);}, 10000); function noLocation() { alert("No GPS"); } function foundLocation(position) { var lat2 = position.coords.latitude; var lon2 = position.coords.longitude; var uri = "savecurrent.php"; $.ajax({ type: 'POST', async: false, dataType: "html", url: uri, data: "lat="+lat2+"&long="+lon2, success: function(data) { } }); } var refreshId = setInterval(function(){updatedata();}, 12000); function updatedata(){ var lat=0; var long=0; for(var i=0;i<markers.length;i++){ var uri = "latdata.php"; $.ajax({ type: 'POST', async: false, dataType: "html", url: uri, data: "id="+i, success: function(data) { lat=data; } }); var uri = "longdata.php"; $.ajax({ type: 'POST', async: false, dataType: "html", url: uri, data: "id="+i, success: function(data) { long = data; } }); var myLatLng = new google.maps.LatLng(lat, long); markers[i].setPosition(myLatLng); infowindows[i].setPosition(myLatLng); } }
savecurrent.php
session_start(); include "conn.php"; $lat=$_POST['lat']; $lon=$_POST['lon']; $sql="update user set lat='$lat', lon='$lon' where id='$_SESSION[id]'"; $query=mysqli_query($connection, $sql) or die($sql);
latdata.php
$id=$_POST['id']; $sql="select * from user"; $query=mysqli_query($connection, $sql); $data=mysqli_fetch_array($query); echo $data['lat'];
longdata.php
$id=$_POST['id']; $sql="select * from user"; $query=mysqli_query($connection, $sql); $data=mysqli_fetch_array($query); echo $data['lon'];
everything is fine until I added this (https://stackoverflow.com/a/65764859/12154107) script code. Its work if i put out the php to:
function initialize () { var marker = new google.maps.Marker({ position: new google.maps.LatLng(), map: map }); markers.push(marker); var infowindow = new google.maps.InfoWindow({ content: "<img src='photo.jpg' width='100' align='left' />", size: new google.maps.Size(50, 50), position: new google.maps.LatLng(0.5098058738150961, 101.42327029544232) }); infowindow.open(map); infowindows.push(infowindow); }
thanks
https://stackoverflow.com/questions/65784167/show-and-update-an-users-current-location-live-use-phpmyadmin-database-in-map-g January 19, 2021 at 09:03AM
没有评论:
发表评论