dpvp56187 2013-10-28 15:40
浏览 56
已采纳

从db获取所有纬度和长度以显示谷歌地图上的所有位置

I've following php page which get a single latitude and longitude from mysql db and it's editable by Mouse. I mean i can move the position by click on the marker.

Well, now, I've few lat and long in my db. I want to show all lat and long on the map. How can i do this.

Javascript code:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api
/js?sensor=false"></script>

<script type="text/javascript">

// Map Initialize function

function initialize() 
{
// Set static latitude, longitude value
var latlng = new google.mpaps.LatLng(<?php echo $lat_d; ?>, <?php echo $long_d; ?>);
// Set map options
var myOptions = {
    zoom: 16,
    center: latlng,
    panControl: true,
    zoomControl: true,
    scaleControl: true,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}
// Create map object with options
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
// Create and set the marker
marker = new google.maps.Marker({
    map: map,
    draggable:true, 
    position: latlng
});

// Register Custom "dragend" Event
google.maps.event.addListener(marker, 'dragend', function() {

    // Get the Current position, where the pointer was dropped
    var point = marker.getPosition();
    // Center the map at given point
    map.panTo(point);
    // Update the textbox
    document.getElementById('txt_latlng').value=point.lat()+", "+point.lng();
});
}

</script>

Html Code:

<div style="float:left; position:relative; width:550px; border:0px #000 solid;">
<div id="map_canvas" style="width:550px;height:400px;border:solid black 1px;"></div>
</div>                   

Php code:

$id = (int) $_GET['id'];
$sql = mysql_query("SELECT * FROM parkings WHERE LocId = '$id'");
$res =  mysql_fetch_array($sql);
$lat_d = $res['latitude'];
$long_d = $res['longitude'];

Any idea or how can i do this ?

  • 写回答

1条回答 默认 最新

  • douzhiba6873 2013-10-28 15:56
    关注

    Something like this should do the trick.

    <?php
    
    // this code block contains some test stuff
    $lat_d = 40.709;
    $long_d = -74.007;
    
    // mimic a result array from MySQL
    $result = array(array('latitude'=>$lat_d,'longitude'=>$long_d));
    
    ?>
    <!doctype html>
    <html>
        <head>
            <script type="text/javascript" src="http://maps.googleapis.com/maps/api
    /js?sensor=false"></script>
            <script type="text/javascript">
            var map;
            function initialize() {
                // Set static latitude, longitude value
                var latlng = new google.maps.LatLng(<?php echo $lat_d; ?>, <?php echo $long_d; ?>);
                // Set map options
                var myOptions = {
                    zoom: 16,
                    center: latlng,
                    panControl: true,
                    zoomControl: true,
                    scaleControl: true,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                }
                // Create map object with options
                map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
            <?php
                // uncomment the 2 lines below to get real data from the db
                // $result = mysql_query("SELECT * FROM parkings");
                // while ($row = mysql_fetch_array($result))
                foreach($result as $row) // <- remove this line
                    echo "addMarker(new google.maps.LatLng(".$row['latitude'].", ".$row['longitude']."), map);";
            ?>
            }
            function addMarker(latLng, map) {
                var marker = new google.maps.Marker({
                    position: latLng,
                    map: map,
                    draggable: true, // enables drag & drop
                    animation: google.maps.Animation.DROP
                });
    
                return marker;
            }
            </script>
        </head>
        <body onload="initialize()">
            <div style="float:left; position:relative; width:550px; border:0px #000 solid;">
                <div id="map_canvas" style="width:550px;height:400px;border:solid black 1px;"></div>
            </div>
        </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿