0

I am working with google maps, for been specific, I am working with http://hpneo.github.io/gmaps for a gps tracker simulation, here I need to update the position on each point. At the gmaps script, markers are stored on an array, and then I was thinking on take each point and use the setPoint method for update the position, however, the gmaps scripts doesn't have implemented this method. I was thinking on implement this method, but my question is then: After having an array of markers on the gmaps script, how could I identify each one in order to update position on the correct marker O r probably I must store it on an external array, an associative array which help me to identifies each marker, but I am thinking that when I update them, the array on the gmap script also will be keeping at the same position without been updated I am attaching my code

/** Positions and map statuses **/
var isLoaded = false;
/** **/
var refreshIntervalId;
var vehicles;
var map;
function setVehicleAsCenter (registration) {
    alert("Hola");
}
function loadPoints (positions) {
    console.debug('loading markers');
    var lttd;
    var lgtd;
    for (var i=0; i < positions.length; i++) {
        lttd = positions[i].latitude;
        lgtd = positions[i].longitude;
        marker = map.addMarker({
            lat: lttd,
            lng: lgtd,
        });
        markers[positions[i].registration] = marker;
    };
    map.fitZoom();
    isLoaded = true
}
function updatePoints (positions) {
    /**
    how could be the alorithm here
    */
    console.debug('updating markers');
}
function requestPoints() {
    $.ajax({
        url:'{% url 'gpstracking.ajax.request_tracks' %}',
        type: 'get',
        dataType: 'json',
        data: {
            vehicles: vehicles
        },
        success: function (positions) {
            if (isLoaded == false) {
                loadPoints (positions);
            } else {
                updatePoints (positions);
            }
        }
    });
}
$(document).ready(function() {
    /** Buttons Click Event Set **/
    $('.map-mode').click(function(){
        vehicles = '';
        $("#jstree").jstree("get_checked",null,true).find('a[rel="vehicle"]').each(function(){
            vehicles =  vehicles + $.trim(this.text) + "|";
        });
        if (vehicles == '') {
            console.debug('No vehicles to display');
            return;
        }
        option = $(this).attr('rel');
        if (option == 'show') {
            console.debug('Ordering to show');
            clearInterval(refreshIntervalId);
            requestPoints();
        }
        if (option == 'listen') {
            console.debug('Listening');
            requestPoints();
            refreshIntervalId = setInterval("requestPoints()", 10000);
        }
        if (option == 'clear') {
            console.debug('Clearing');
            clearInterval(refreshIntervalId);
            markers = new Object();
            map.removeMarkers();
            isLoaded = false;
        }
    });
    /** Map loading **/
    map = new GMaps({
        div: '#map-canvas',
        lat: -16.4237766667,
        lng: -71.54262,
    });
});

1 Answer 1

2

Google map markers expose a setPosition method for this.

I'm guessing that the markers variable is declared somewhere and is keeping a reference to each marker on the map:

function updatePoints (positions) {
    for (var i=0; i < positions.length; i++) {
        var pos=positions[i];
        var marker=markers[pos.registration];
        if(marker){
            // this marker already exists, so reposition it
            var latlong=new google.maps.LatLng(pos.latitude, pos.longitude);
            marker.setPosition(latlong);
        }else{
            // this is a new marker so create it
            marker = map.addMarker({
                lat: pos.latitude,
                lng: pos.longitude,
            });
            markers[pos.registration] = marker;
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.