简体   繁体   English

从数据库中读取经度/纬度并将标记放在地图上

[英]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.

相关问题 从Google Map Link提取经度和纬度 - Extract Latitude and Longitude from Google Map Link 移动谷歌地图标记点时,使用纬度和经度更新数据库 - Update db with latitude and longitude when google map marker point is move 在给定经度和纬度的情况下,如何从数据库获取到地点的距离 - How to get distance to places from DB given longitude and latitude 从MySQL获取纬度和经度以显示Google Map - Fetch Latitude and Longitude from MySQL for show Google Map 将经度和纬度传递给javascript函数,并从Google地图获取视图 - Pass Latitude and Longitude to a javascript function and get a view from the google map 使用标记从Google地图获取纬度/经度坐标 - Get latitude/longitude coordinates from a Google map using a marker 使用数据库中的多个纬度和经度值在Google地图上标记路线 - Marking route on Google map with multiple latitude and longitude values from database 从本地数据库检索纬度和经度数据,然后在Google地图中搜索 - Retrieve the latitude and longitude data from the local database and search it in the google map Google地图从经度和纬度值获取总地址 - Google map get the total address from longitude and latitude values 如何在php中谷歌地图转换纬度和经度的任何地址? - how to convert any address in latitude and longitude from google map in php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM