在Google地图中用sql& amp; amp; amp; PHP的? [英] Array multiple points in google maps with sql & php?

查看:93
本文介绍了在Google地图中用sql& amp; amp; amp; PHP的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP中的新手。上面的代码工作正常,但只有一个标记。如果我使用top 1搜索一个点就没关系。



当我更改顶部nubmer像10仍然显示一个或最后一个标记。我如何显示多个标记?
我不想使用json。
我认为这有点像var数组。
谢谢

< html> < HEAD> < link rel =stylesheettype =text / cssmedia =screenhref =css.css> < meta name =viewportcontent =initial-scale = 1.0,user-scalable = no/> < style type =text / css> html {height:100%} body {height:100%;保证金:0;填充:0;颜色:白色}#map-canvas {height:100%}< / style> < script type =text / javascriptsrc =https://maps.googleapis.com/maps/api/js?key=<?php echo $ apikey;?>& sensor = false> < /脚本> < script type =text / javascript>函数initialize(){<?php $ con = mssql_connect(DB_HOST2,DB_USER2,DB_PASS2,DB_NAME2)或死亡('连接到MSSQ时出现错误'); mssql_select_db(MDTcan,$ con); $ query =(select top 10 Cabno,lat,long,ttime from gps_history where ttime =(从gps_history选择min(ttime)作为f,其中f.Cabno = gps_history.Cabno)order by cabno); $ result = mssql_query($ query); while($ row = mssql_fetch_assoc($ result)){echo'var mapOptions = {center:new google.maps.LatLng('。$ row ['lat']。','。$ row ['long']。' ),zoom:13}; var map = new google.maps.Map(document.getElementById(map-canvas),mapOptions); var myLatlng1 = new google.maps.LatLng('。$ row ['lat']。','。$ row ['long']。'); var marker = new google.maps.Marker({position:myLatlng1,map:map,animation:google.maps.Animation.DROP,title:''。$ row ['Cabno']。','。$ row [' Hora']。'});'; ?}> } google.maps.event.addDomListener(window,'load',initialize); < /脚本> < /头> <身体GT; <中心],[ < /中心],[ < div id =map-canvas/> < / body>< / html>

解决方案

只有创建一次地图(目前您正在为数据库中的每一行创建一个带有一个标记的新地图,当查询结束时,最后一个仍然存在):

  function initialize(){
var bounds = new google.maps.LatLngBounds();
//初始化地图
var mapOptions = {
center:new google.maps.LatLng(0,0),
zoom:13
};
var map = new google.maps.Map(document.getElementById(map-canvas),
mapOptions);

<?php

...然后向它添加标记

$ result = mssql_query($ query);

while($ row = mssql_fetch_assoc($ result))
{
echo'var myLatlng1 = new google.maps.LatLng('。$ row ['lat']。 ','。$ row ['long']。');
var marker = new google.maps.Marker({
position:myLatlng1,
map:map,
animation:google.maps.Animation.DROP,
title :''。$ row ['Cabno']。','。$ row ['Hora']。'
});
bounds.extend(marker.getPosition());';
}
?>
//居中放大地图以显示所有标记。
map.fitBounds(bounds);
}
google.maps.event.addDomListener(window,'load',initialize);


I a newbie in PHP. The code above works fine, but only with one marker. If I use top 1 to search one point it's ok.

When I change the top nubmer like 10 still shows one or last marker. How can I show multiple markers? I don't want to use json. I think there's something like var array. Thank you

<html>
  <head>
  <link rel="stylesheet" type="text/css" media="screen" href="css.css">
    <meta name="viewport"
        content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0; color: white }
      #map-canvas { height: 100% }
    </style>
    <script type="text/javascript"
      src="https://maps.googleapis.com/maps/api/js?key=
          <?php echo $apikey; ?>&sensor=false">
    </script>
    <script type="text/javascript">
      function initialize() {
        <?php
				$con =  mssql_connect(DB_HOST2, DB_USER2, DB_PASS2,DB_NAME2)
				or die('Something went wrong while connecting to MSSQ');
				mssql_select_db("MDTcan", $con);
				$query = ("select top 10 Cabno, lat, long, ttime
								from gps_history
								where ttime = (select min(ttime) from gps_history as f where f.Cabno = gps_history.Cabno)
								order by cabno"); 
				
	
				

				$result = mssql_query($query);

			    while($row = mssql_fetch_assoc($result))
				{
			 
				 echo ' var mapOptions = {
						  center: new google.maps.LatLng(  
									  '. $row['lat'].', '.$row['long'].'),
						  zoom: 13
						};
						var map = new google.maps.Map(document.getElementById("map-canvas"),
							mapOptions);
							var myLatlng1 = new google.maps.LatLng('.
									  $row['lat'].', '.$row['long'].'); 
									  
							var marker = new google.maps.Marker({ 
								position: myLatlng1, 
								map: map, 
								animation: google.maps.Animation.DROP,
								title:"'.$row['Cabno'].','.$row['Hora'].'"
							  });';
								}
			?>
	      }
      google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
  <center>
	</center>
    <div id="map-canvas"/>
  </body>
</html>

解决方案

Only create the map once (currently you are creating a new map with one marker on it for each row in your database, when the query ends, the last one remains):

function initialize() {
  var bounds = new google.maps.LatLngBounds();
  // initialize the map
  var mapOptions = {
         center: new google.maps.LatLng(0,0),
         zoom: 13
  };
  var map = new google.maps.Map(document.getElementById("map-canvas"),
                        mapOptions);

<?php

... then add markers to it

  $result = mssql_query($query);

  while($row = mssql_fetch_assoc($result))
  {  
    echo 'var myLatlng1 = new google.maps.LatLng('.$row['lat'].', '.$row['long'].');                                  
    var marker = new google.maps.Marker({ 
                            position: myLatlng1, 
                            map: map, 
                            animation: google.maps.Animation.DROP,
                            title:"'.$row['Cabno'].','.$row['Hora'].'"
                          });
     bounds.extend(marker.getPosition());';
   }
 ?>
   // center and zoom the map to show all the markers.
   map.fitBounds(bounds);
 }
 google.maps.event.addDomListener(window, 'load', initialize);

这篇关于在Google地图中用sql&amp; amp; amp; amp; PHP的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆