im can't figure out how to solve this.
I'm getting data from json file with ajax, this json file contains 3 points to be markers called "pickup", "dropoff" and "motomarker", this file is getting new data every 5 seconds, and the only changes the location coordinates is "Motomaker".
When this location changes the "motomarker" is not getting new position, it's created a new one whit the new coordinates.
The json looks like this
{"origin": {"lat": 19.4292187, "lon": -99.156495199999995}, "dropoff": {"lat": 19.4290716, "lon": -99.156278299999997, }, "driver": {"lat": 19.414372254556561, "lon": -99.154668594441588, "driver_name": "User_test"}}
My code is this
<script type="text/javascript">
var mapOptions = {
zoom: 14,
center: new google.maps.LatLng(19.420823, -99.186716)
};
// Creating the map
map = new google.maps.Map(document.getElementById('map-container'), mapOptions);
minutes = 30;
seconds = 0;
hours = 0;
function asyncRequest(url, data, method, successHandler, errorHandler) {
$.ajax({
url: url,
type: method,
dataType: "json",
success: successHandler,
error: errorHandler,
data: data,
xhrFields: {
withCredentials: true
}
});
}
function refreshGodViewJSON() {
var date = new Date(2013, 09, 22, 16, minutes, seconds)
//seconds = (seconds + 30) % 60;
//if (seconds == 0) {
minutes = (minutes + 3) % 60
//}
if (minutes == 0) {
hours = (hours + 1) % 60
}
url = "/2/delivery/track?tracking_id={{tracking_id}}&f=json"
var successHandler = function (response) {
if (response.status == "Error") {
}
if (response.length == 0) {
return;
}
var json = response;
SlidingMarker.initializeGlobally();
if (json.driver.driver_name != "No_driver") {
var driver_lat = json.driver.lat;
var driver_lon = json.driver.lon;
var driver_icon = new google.maps.MarkerImage(
"/static/img/moto99.png",
null, /* size is determined at runtime */
null, /* origin is 0,0 */
null, /* anchor is bottom center of the scaled image */
new google.maps.Size(50, 50)
);
var pickup_lat = json.origin.lat;
var pickup_lon = json.origin.lon;
var pickup_icon = new google.maps.MarkerImage(
"/static/img/pin_delivery_origin.png",
null, /* size is determined at runtime */
null, /* origin is 0,0 */
null, /* anchor is bottom center of the scaled image */
new google.maps.Size(80, 68)
);
var dropoff_lat = json.dropoff.lat;
var dropoff_lon = json.dropoff.lon;
var dropoff_icon = new google.maps.MarkerImage(
"/static/img/pin_delivery_destination.png",
null, /* size is determined at runtime */
null, /* origin is 0,0 */
null, /* anchor is bottom center of the scaled image */
new google.maps.Size(80, 68)
);
// Adding a new marker for the object
var marker1 = new google.maps.Marker({
position: new google.maps.LatLng(pickup_lat, pickup_lon),
map: map,
icon: pickup_icon,
});
var marker2 = new google.maps.Marker({
position: new google.maps.LatLng(dropoff_lat, dropoff_lon),
map: map,
icon: dropoff_icon,
});
//if ("driver_lat" in sessionStorage) {
//console.log(old_lat);
//console.log(old_lon);
// var latLng = new google.maps.LatLng(driver_lat, driver_lon)
// markermoto.setPosition(latLng);
//} else {
sessionStorage["driver_lat"] = driver_lat;
sessionStorage["driver_lon"] = driver_lon;
var old_lat = localStorage.driver_lat;
var old_lon = localStorage.driver_lon;
var markermoto = new google.maps.Marker({
map: map,
icon: driver_icon,
});
if (old_lat != driver_lat && old_lon != driver_lon) {
var latLng = new google.maps.LatLng(driver_lat, driver_lon)
markermoto.setPosition(latLng);
}
} else {
var pickup_lat = json.origin.lat;
var pickup_lon = json.origin.lon;
var pickup_icon = new google.maps.MarkerImage(
"/static/img/pin_delivery_origin.png",
null, /* size is determined at runtime */
null, /* origin is 0,0 */
null, /* anchor is bottom center of the scaled image */
new google.maps.Size(80, 68)
);
var dropoff_lat = json.dropoff.lat;
var dropoff_lon = json.dropoff.lon;
var dropoff_icon = new google.maps.MarkerImage(
"/static/img/pin_delivery_destination.png",
null, /* size is determined at runtime */
null, /* origin is 0,0 */
null, /* anchor is bottom center of the scaled image */
new google.maps.Size(80, 68)
);
// Adding a new marker for the object
var marker1 = new google.maps.Marker({
position: new google.maps.LatLng(pickup_lat, pickup_lon),
map: map,
icon: pickup_icon,
});
var marker2 = new google.maps.Marker({
position: new google.maps.LatLng(dropoff_lat, dropoff_lon),
map: map,
icon: dropoff_icon,
});
}
};
var errorHandler = function () {
};
asyncRequest(url, {}, "GET", successHandler, errorHandler)
}
$(document).ready(function () {
refreshGodViewJSON();
setInterval('refreshGodViewJSON()', 5000);
});