[英]Reading latitude/longitude from DB and putting markers on the map
Below is given my sample code. 下面给出了我的示例代码。 In
index.php
I define jquery tabs. 在
index.php
我定义了jquery选项卡。 One of the tabs should open a map (openlayers) and put markers on this map. 其中一个标签应打开一个地图(openlayers)并在该地图上放置标记。 Latitude and longitude of each marker is taken from MySQL DB.
每个标记的纬度和经度均取自MySQL DB。 The problem is that I don't know how and where to execute the function
put_marker
reading data from DB. 问题是我不知道如何以及在哪里执行功能
put_marker
从数据库读取数据。 I know it should be a basic question. 我知道这应该是一个基本问题。
index.php index.php
<script type="text/javascript">
$(document).ready(function() {
$("#tabs").tabs({
ajaxOptions: {
success: function( html ) {
$("#content").html(html);
page_init();
}
}
});
});
</script>
<div id="tabs">
<ul>
<li><a href="administration.php"><span>Administration</span></a></li>
<li><a href="map.php"><span>Map</span></a></li>
</ul>
</div>
map.php map.php
<?php
include_once 'include/DatabaseConnector.php';
$query4="SELECT r.resLatitude, r.resLongitude FROM resources r;";
$result4=DatabaseConnector::ExecuteQueryArray($query4);
foreach ($result4 as $row):
// HERE I HAVE A PROBLEM
//putMarker($row['resLatitude'],$row['resLongitude']);
endforeach;
?>
<script src="http://www.openlayers.org/api/OpenLayers.js"></script>
<script type="text/javascript">
var map, layer;
function page_init(){
map = new OpenLayers.Map("basicMap");
var mapnik = new OpenLayers.Layer.OSM();
var fromProjection = new OpenLayers.Projection("EPSG:4326"); // Transform from WGS 1984
var toProjection = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection
var position = new OpenLayers.LonLat(2.07833,41.2969).transform( fromProjection, toProjection);
var zoom = 15;
map.addLayer(mapnik);
map.setCenter(position, zoom );
}
function putMarker(latMarca, lonMarca)
{
var lonLat = new OpenLayers.LonLat(lonMarca ,latMarca ).transform(new OpenLayers.Projection("EPSG:4326"),map.getProjectionObject());
var zoom=16;
var markers = new OpenLayers.Layer.Markers( "Markers" );
map.addLayer(markers);
markers.addMarker(new OpenLayers.Marker(lonLat));
map.setCenter (lonLat, zoom);
}
</script>
<div id="basicMap" style="width: 100%; height: 100%;">
</div>
Well, you're getting your markers in PHP on the server side. 好吧,您正在服务器端使用PHP获得标记。 And you're passing them to Javascript on the client side.
您将它们传递给客户端的Javascript。 Two different places.
两个不同的地方。
Actually, there's no need even in JSON manipulations in the simplest case. 实际上,在最简单的情况下,甚至不需要进行JSON操作。 In your map.php you can do:
在您的map.php中,您可以执行以下操作:
..
echo '<script type="text/javascript">';
echo '$(document).ready(function() {';
foreach ($result4 as $row){
echo 'putMarker('.$row['resLatitude'].','.$row['resLongitude'].');';
}
echo '});';
echo '</script>';
...
These code would be interpreted on the client side as pure JS, but with values being taken from the PHP. 这些代码在客户端将被解释为纯JS,但其值取自PHP。
By the way, it's not a good approach to write your code the same way. 顺便说一句,用相同的方式编写代码不是一个好的方法。 Look toward MVC frameworks , where the code and look are separated from each other.
将目光投向MVC框架 ,在其中代码和外观相互分离。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.