在Google地图上移动标记

时间:2014-07-09 17:53:39

标签: javascript google-maps google-maps-api-3

我正在做跟踪系统。 我正在使用codeigniter框架和mongodb连接。 另外我使用谷歌地图javascript api v3来加载地图。 我能够在地图上加载标记。但是我不知道在从数据库加载值时如何移动标记。我已经读过,我必须使用AJAX来做,但不知道。

我跟着this link加载了标记。

任何帮助都将受到高度赞赏。

2 个答案:

答案 0 :(得分:0)

Google地图的代码不会有任何变化,让我举一个简单的例子。

function initialize() {
    var map;
    var bounds = new google.maps.LatLngBounds();
    var mapOptions = {
        mapTypeId: 'roadmap'
    };

    // Display a map on the page
    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

    // Multiple Markers
    var markers = [
        ['London Eye, London', 51.503454,-0.119562],
        ['Palace of Westminster, London', 51.499633,-0.124755]
    ];

    // Info Window Content
    var infoWindowContent = [
        ['<div class="info_content">' +
        '<h3>London Eye</h3>' +
        '<p>The London Eye is a giant Ferris wheel situated on the banks of the River Thames. The entire structure is 135 metres (443 ft) tall and the wheel has a diameter of 120 metres (394 ft).</p>' +        '</div>'],
        ['<div class="info_content">' +
        '<h3>Palace of Westminster</h3>' +
        '<p>The Palace of Westminster is the meeting place of the House of Commons and the House of Lords, the two houses of the Parliament of the United Kingdom. Commonly known as the Houses of Parliament after its tenants.</p>' +
        '</div>']
    ];

    // Display multiple markers on a map
    var infoWindow = new google.maps.InfoWindow(), marker, i;

    // Loop through our array of markers & place each one on the map  
    for( i = 0; i < markers.length; i++ ) {
        var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
        bounds.extend(position);
        marker = new google.maps.Marker({
            position: position,
            map: map,
            title: markers[i][0]
        });

        // Allow each marker to have an info window    
        google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
                infoWindow.setContent(infoWindowContent[i][0]);
                infoWindow.open(map, marker);
            }
        })(marker, i));

        // Automatically center the map fitting all markers on the screen
        map.fitBounds(bounds);
    }

    // Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
    var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) {
        this.setZoom(14);
        google.maps.event.removeListener(boundsListener);
    });

}

因此,您只需要使用从数据库中获取的值替换markers数组中的值,并绘制所有标记。

您需要做的是,在PHP中从数据库中获取数据。

<?php

        #code to fetch data from database
        #store results in array

        $lat_long_values = array
                               (
                                  array('London Eye, London', 51.503454,-0.119562),
                                  array('Palace of Westminster, London', 51.499633,-0.124755),
                              );
?>

假设您在Array中获得了类似的值。现在在JavaScript中使用此数组中的值。因此,标记现在将成为,

var markers = <?php echo $lat_long_values; ?>

修改

然后在那种情况下,我建议你使用组合 Ajax Get Method

JavaScript setInterval method

setInterval(function(){


    $.get( "YOUR_PHP_PAGE", function( data ) {
        //Do what you want with data
        alert(data);
    });


}, 3000);

你可以看到,我在3秒(3000毫秒)后调用JS函数,并且我调用PHP函数从数据库中获取最新值。

你可以运用这个技巧来完成它。

答案 1 :(得分:0)

这是您想要的代码..

$query = "select * from  table";
$result= mysql_query($query);
$lat[] = array();
$lng[]=array();
$x=0;
while($rows=mysql_fetch_array($result))
{
    $lat[$x]=$rows['X'];
    $lng[$x]=$rows['Y'];
    $x++;

}//here is your connection in db

var markersdata=[];
function dd()
{

    latLocation =<?php echo json_encode($lat); ?>;
    lngLocation =<?php echo json_encode($lng); ?>;

            for (var i = 0; i < latLocation.length; i++) {
        //

                    makeMarker(latLoc[i],lngLoc[i]);


            }

}
funtion makeMarker(lati, longi)
{

   var markerOptions = {map: map, position: new google.maps.LatLng(lati, longi)};`
   var marker = new google.maps.Marker(markerOptions);`

}
//this code is working. leave comment if you have any question