-1
私は2つのオブジェクトの任務とトラックのJSON座標を取得するRoRプロジェクトを持っています。原点マーカー(GM API DirectionService)を更新した位置にゆっくり移動させるにはどうすればよいですか?
Google Maps API DirectionServiceを使用して、それらの座標を使用してそれらの間のルートを描画します。
ミッションの座標は、トラックの静的ですがので、私は彼らに10秒ごとに要求しています、AJAXを使用して、ではありません。
Atm、トラックのマーカーが新しい位置に「ジャンプ」して、醜い効果を作り出します。
私が達成したいのは、トラックの座標が更新されるたびに、トラックのマーカーが新しい位置にゆっくりと移動することです。
これを行う方法に関するアイデアは、本当に感謝しています!私の問題を解決するために
$(document).on('page:change', function() {
//gets new truck position every 10 seconds
var intervalId = setInterval(getNewTruckPosition, 10000);
// initial truck coordinates
newTruckLatLng = {lat: 41.85, lng: -87.65};
// mission coordinates(directionService destination -> static)
missionLatLng = {lat: 40.771, lng: -73.974};
drawMapData();
function getNewTruckPosition() {
$.ajax({
....
success: function(data) {
//gets new coordinates for truck
newTruckLat = "new-value-from-json";
newTruckLong = "new-value-from-json";
newTruckLatLng = {lat: newTruckLat, lng: newTruckLong};
//calculates and draws new route every time truck coordinates are updated
drawMapData();
}
});
}
initMap();
});
function drawMapData() {
##########ISSUE I'M TRYING TO SOLVE#############
//draws a new marker for the truck, deleting the last one --->
//this results in a truck marker jumping effect
//what I would like to achieve is the truck marker
//sliding to its new position, not jumping
if (typeof newTruckLat != 'undefined') {
function makeMarker() {
if (typeof newMarker != 'undefined') {
newMarker.setMap(null);
}
newMarker = new google.maps.Marker({
//value received from JSON
position: newTruckLatLng,
map: map,
title: "Truck's Position"
});
}
}
//***********ROUTING****************//
directionsService = new google.maps.DirectionsService;
directionsDisplay = new google.maps.DirectionsRenderer();
directionsDisplay.setOptions(
{
suppressInfoWindow: true,
suppressMarkers: true,
polylineOptions: {
strokeColor: "#1A239A",
strokeOpacity: 0.9 ,
strokeWeight: 5
}
});
directionsDisplay.setMap(map);
calculateAndDisplayRoute(directionsService, directionsDisplay);
function calculateAndDisplayRoute(directionsService, directionsDisplay) {
directionsService.route({
//value received from JSON
origin: newTruckLatLng,
destination: missionLatLng,
travelMode: google.maps.TravelMode.DRIVING
}, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
makeMarker();
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
//***********Finished ROUTING****************//
}
function initMap() {
isDraggable = $(document).width() > 480 ? true : false;
if (typeof truckLat != 'undefined') {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
draggable: isDraggable,
scrollwheel: false,
disableDefaultUI: true
});
}
marker = new google.maps.Marker({
position: missionLatLng,
map: map,
title: "Mission's Position"
});
newMarker = new google.maps.Marker({
//initial value {newTruckLatLng = {lat: 41.85, lng: -87.65}}
position: newTruckLatLng,
map: map,
title: "Truck's Position"
});
markers = [marker, newMarker];
bounds = new google.maps.LatLngBounds();
for (var i = 0; i < markers.length; i++) {
bounds.extend(markers[i].getPosition());
}
map.fitBounds(bounds);
}
[、最小コンプリート、テストおよび読み取り可能な例]を提供してください(http://stackoverflow.com/help/mcve)を示していあなたが解決しようとしている問題。 – geocodezip
私の問題を私が持っている問題に関するコメントとともに編集しました。それが十分明確である場合や追加情報を追加する必要がある場合はお知らせください。ありがとうございました! –