Peršokti į turinį
  • ŽAIDIMAI
  • , ŽAIDIMAI
  • ŽAIDIMAI

[HELP] Google Maps marker & SQL coordinates


Ši tema yra neaktyvi. Paskutinis pranešimas šioje temoje buvo prieš 2681 dienas (-ų). Patariame sukurti naują temą, o ne rašyti naują pranešimą.

Už neaktyvių temų prikėlimą galite sulaukti įspėjimo ir pranešimo pašalinimo!

Recommended Posts

Sveiki, susiradau kaip ant google maps pridėliot "marker'iu" pagal ištrauktas koordinates iš duomenų bazės.

Iškilo problema su tų "marker'ių" pozicijos atnaujinimu.

Man į duomenų bazę kas 2min, sukrenta naujos koordinates, ir aš kiekvieno vilkiko koordinates paimu naujausią datą.

Kaip padaryt, kad nerefrešinus puslapio kas 2min automatiškai atsinaujintu tų "marker'ių" pozicijos?

<html>
	<head>
	<script type='text/javascript' src='http://www.macrostash.com/wp-content/plugins/jquery-1.6.2.min.js'></script>
	<script type='text/javascript' src='http://www.macrostash.com/wp-content/plugins/jquery-ui-1.8.14.custom.min.js'></script>
	<style>
		BODY {font-family : Verdana,Arial,Helvetica,sans-serif; color: #000000; font-size : 13px ; }
		#map_canvas { width:100%; height: 100%; z-index: 0; }
	</style>
	<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyCBc6dm99GfvijLGr604Ejju8aXd4Pk9uQ&sensor=false" /></script>
	<script type='text/javascript'>
		//This javascript will load when the page loads.
		jQuery(document).ready( function($)
		{
			//Initialize the Google Maps
			var geocoder;
			var map;
			var markersArray = [];
			var infos = []; 
			geocoder = new google.maps.Geocoder();
			var myOptions = {
				zoom: 9,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			}
			//Load the Map into the map_canvas div
			var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
			map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
			//Initialize a variable that the auto-size the map to whatever you are plotting
			var bounds = new google.maps.LatLngBounds();
			//Initialize the encoded string       
			var encodedString;
			//Initialize the array that will hold the contents of the split string
			var stringArray = [];
			//Get the value of the encoded string from the hidden input
			encodedString = document.getElementById("encodedString").value;
			//Split the encoded string into an array the separates each location
			stringArray = encodedString.split("****"); 
			var x;
			for (x = 0; x < stringArray.length; x = x + 1)
			{
				var addressDetails = [];
				var marker;
				//Separate each field
				addressDetails = stringArray[x].split("&&&");
				//Load the lat, long data
				var lat = new google.maps.LatLng(addressDetails[1], addressDetails[2]);
				//Create a new marker and info window
				marker = new google.maps.Marker({
					map: map,
					position: lat,
					//Content is what will show up in the info window
					content: addressDetails[0]
				});
				//Pushing the markers into an array so that it's easier to manage them
				markersArray.push(marker);
				google.maps.event.addListener( marker, 'click', function () {
					closeInfos();
					var info = new google.maps.InfoWindow({content: this.content});
					//On click the map will load the info window
					info.open(map,this);
					infos[0]=info;
				});
				//Extends the boundaries of the map to include this new location
				bounds.extend(lat);
			}
			//Takes all the lat, longs in the bounds variable and autosizes the map
			map.fitBounds(bounds); 
			//Manages the info windows
			function closeInfos()
			{
				if(infos.length > 0)
				{
					infos[0].set("marker",null);
					infos[0].close();
					infos.length = 0;
				}
			}
		});
	</script>
	</head>
	<body>
	<div id='input'> 
		<?php
			//Connect to the MySQL database that is holding your data, replace the x's with your data
			$host        = "host=localhost";
			$port        = "port=5432";
			$dbname      = "dbname=test";
			$credentials = "user=postgres password=test";
			$db = pg_connect( "$host $port $dbname $credentials"  );
			if(!$db)
			{
				echo "Error : Unable to open database <br><br>";
			} else {
			} 
			//Initialize your first couple variables
			$encodedString = ""; //This is the string that will hold all your location data
			$x = 0; //This is a trigger to keep the string tidy 
			//Now we do a simple query to the database        
			$sql = pg_query("SELECT * FROM dev_list");						
			while($row = pg_fetch_row($sql))
			{
				$sql2 = pg_query("SELECT * FROM position_list WHERE dev_id = '".$row[0]."' order by date desc limit 1");							
				while($row2 = pg_fetch_row($sql2))
				{
					//This is to keep an empty first or last line from forming, when the string is split
					if ( $x == 0 )
					{
						$separator = "";
					}
					else
					{
						//Each row in the database is separated in the string by four *'s
						$separator = "****";
					}
					//Saving to the String, each variable is separated by three &'s
					$encodedString = $encodedString.$separator.
					"<p class='content'><b>Lat:</b> ".$row2[5].
					"<br><b>Long:</b> ".$row2[6].
					"<br><b>Valstybinis: </b>".$row[1].
					"<br><b>Date: </b>".$row2[1].
					"<br><b>Save date: </b>".$row2[9].
					"</p>&&&".$row2[5]."&&&".$row2[6];
					$x = $x + 1;
				}
			}
			pg_close($db);
		?> 
		<input type="hidden" id="encodedString" name="encodedString" value="<?php echo $encodedString; ?>" />
	</div>
	<div id="map_canvas"></div>
	</body>
</html>
Nuoroda į komentarą
Dalintis per kitą puslapį

Nelabai draugauju su frontend'u ir neskaičiau tavo kodo. Tačiau tu pasidarei/moki pasidaryt, jog refreshinant atsinaujintų tos koordinatės? Jei taip, labai paprastas, bet žinoma ne geriausias sprendimas būtų tiesiog sukurt naują failą, į jį įkišt iframe kuriame užkrautum tą psl kuris rodo koordinates ir tada tiesiog su JS tą iframe kas norimą laiko tarpą refreshint... 

Nuoroda į komentarą
Dalintis per kitą puslapį

(redaguota)

Kažką susiradau, bet čia tikrai kažko trūksta.....

 

index.html

<!DOCTYPE html>
<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
  <title>Google Maps Multiple Markers</title> 
  <link href="https://google-developers.appspot.com/maps/documentation/javascript/examples/default.css" rel="stylesheet">
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCBc6dm99GfvijLGr604Ejju8aXd4Pk9uQ">
    </script>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>


  <script type="text/javascript">

    var map;

    // Cretes the map
    function initialize() {
      map = new google.maps.Map(document.getElementById('map'), {
        zoom: 4,
        center: new google.maps.LatLng(46.62395343041488, 8.041563034057617),
        mapTypeId: google.maps.MapTypeId.ROADMAP
      });
    }

    // This function takes an array argument containing a list of marker data
    function generateMarkers(locations) {

      for (var i = 0; i < locations.length; i++) {  

        new google.maps.Marker({
          position: new google.maps.LatLng(locations[i][1], locations[i][2]),
          map: map,
          title: locations[i][0]
        });
      }
    }
  </script>

</head> 
<body> 
  <div id="map" style="width: 1000px; height: 700px;"></div>
   <script type="text/javascript">

      window.onload = function () {
      initialize();
      setInterval(function()
      {
        $.ajax({
          url: "http://localhost/opop/location.php",
          type: "POST",
          data: {func: 'getLocation'},
          success: function(data) {
            generateMarkers(data);
          }
        });
      }, 1000);

    };
  </script>
</body>
</html>

location.php

<?php
    $host        = "host=localhost";
    $port        = "port=5432";
    $dbname      = "dbname=dataimport";
    $credentials = "user=test password=test";

    $db = pg_connect( "$host $port $dbname $credentials"  );
    if(!$db)
    {
      echo "Error : Unable to open database <br><br>";
    } else {
    }

    if (isset($_POST['func']) && $_POST['func'] === 'getLocation')
    {
        echo getLocation();

        function getLocation()
        {
            $json= array();

            $sql = pg_query("SELECT * FROM dev_list");                      
            while($row = pg_fetch_row($sql))
            {
                
                $sql2 = pg_query("SELECT * FROM position_list WHERE dev_id = '".$row[0]."' order by date desc limit 1");                            
                while($row2 = pg_fetch_row($sql2))
                {   
                    $koordinates = "";
                    $koordinates = "".$row2[5].",".$row2[6]."";
                    
                    $XY = explode(",",$koordinates);

                    $json[] = array($row[1],$XY[0],$XY[1]);                

                }
            }
            return $json;           
            
        }
    }
    pg_close($db);
?>
Redaguota , nario mindux9
Nuoroda į komentarą
Dalintis per kitą puslapį

Ši tema yra neaktyvi. Paskutinis pranešimas šioje temoje buvo prieš 2681 dienas (-ų). Patariame sukurti naują temą, o ne rašyti naują pranešimą.

Už neaktyvių temų prikėlimą galite sulaukti įspėjimo ir pranešimo pašalinimo!

Svečias
Ši tema yra užrakinta.
  • Šiame puslapyje naršo:   0 nariai

    • Nėra registruotų narių peržiūrinčių šį forumą.

Skelbimai


×
×
  • Sukurti naują...