admin管理员组

文章数量:1394065

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="
/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 ?

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./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 ?

Share Improve this question asked Oct 28, 2013 at 15:40 CreativeartbdCreativeartbd 312 silver badges8 bronze badges 2
  • 2 Loop through your lat/lng coordinates in your PHP. In your javascript create markers for each of them and extend the bounds of the map to fit them all in. – Jonathon Commented Oct 28, 2013 at 15:46
  • 1 Well, I can loop all the lat and long from db with php but how can i use these to javascript ? – Creativeartbd Commented Oct 28, 2013 at 15:48
Add a ment  | 

1 Answer 1

Reset to default 4

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./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
            // unment 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>

本文标签: javascriptGet all lat and long from db to show all position on the google mapStack Overflow