如何使用 Google Maps API 平滑移动标记?

愤怒的B

当我的标记立即改变其位置时,它的抖动过于剧烈。transiton: 1s linearGoogle Maps API 中是否有任何等效的 CSS以便我的标记可以顺利移动?这是我的地图初始化代码:

var marker;

    function initMap() {
    // Map initialization
        var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 1,
            center: { lat: 59.325, lng: 18.070 }
        });
    // Marker settings
        marker = new google.maps.Marker({
            map: map,
            draggable: true,
            animation: google.maps.Animation.DROP,
            position: { lat: 59.327, lng: 18.067 }
        });
    // Marker moves every second
        setInterval( () => {
            moveMarker();
        }, 1000);    
    }

    var a = 1;
    function moveMarker() {
        marker.setPosition(new google.maps.LatLng(a++, a++));
    }
#map {
  width: 500px;
  height: 500px;
}
<div id="map"></div>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCJYSNYPAfcm8ZV4pi8nj6fbeOtbQQpr7Y&callback=initMap"></script>

麦克墨菲

这是非常可行的,但需要工作。重要的一点是识别 Google Maps 用于标记的 DIV(有 2 个对于触摸事件是透明的)调查已经为您完成,您实际上只需要了解一次。

一个完整的例子可以在这里找到看看 Hansel 和 Gretel 在地图上的移动有多顺畅!如果有任何延迟,转换时间会合并。

我的 Brotkrumen Ultimate Web App 的所有代码都可以在这里找到你最感兴趣的 HandleMap.js 文件,但有一个 aaa_readme.txt

这是代码的一部分:-

function showJourney(){
    map.setZoom(map.getZoom());
    map.setOptions({gestureHandling: "none"});
    zoomOut.style.display = "none";
    zoomIn.style.display  = "none";

    hat.setPosition(
        new google.maps.LatLng(
                lastPos.coords.latitude,
                lastPos.coords.longitude)); 
    hat.setVisible(true);
    hat.setAnimation(bounce);

    HandG.setPosition(
        new google.maps.LatLng(
                firstPos.coords.latitude,
                firstPos.coords.longitude)); 
    HandG.setVisible(true);

    map.panTo(path[0]); 
    google.maps.event.trigger(map, 'resize');

    if (document.querySelectorAll(MARKER_SELECTOR).length == 0){
        observer.observe(mapDiv, {
                        childList     : true,
                        subtree       : true ,
                        attributes    : true ,
                        characterData : false
                        })
    } else {
        setTimeout(plotTrip,0);
    }
}
function plotTrip(){
    nextFunc = plotStep;
    hat.setAnimation(bounce);
    HandG.setPosition(path[0]);
    dirPoly.setVisible(true);       
    progressPath = [];
    progressPath.push(path[0]);
    dirPoly.setPath(path);
    stepPoly.setPath(progressPath);
    stepPoly.setVisible(true);
    currStep = 1;
    markerDivs = [];
    var markerImgs = document.querySelectorAll(MARKER_SELECTOR);
    for (var i=0; i<markerImgs.length; i++){
        console.log(markerImgs[i].src);
        markerDivs[i] = markerImgs[i].parentNode;
        markerDivs[i].style.transitionDuration = "0s";
        markerDivs[i].style.transitionProperty = "left, top";
        markerDivs[i].style.transitionTimingFunction = "linear";
    }

    setTimeout(plotStep,0);
    abort = false;
    btn.value = "Cancel";
    btn.disabled = false;
}
function plotStep(){
    if (abort) return;

    if (legs[currStep].didLoiter){
        countDown = legs[currStep].restTime;
        infoWindow.setContent(
            "<div id='waitDiv'><span>Waiting</span></div>");
        infoWindow.open(map,HandG);
        showInterval();
    } else {
        plotIt();
    }
}
function showInterval(){
    if (abort) return;

    infoWindow.setContent(
        "<div id='waitDiv'><span>Waiting "+deltaDate(countDown)+"</span></div>");
    countDown -= (ONE_SEC * multiSpeed);
    if (countDown < 1){
        infoWindow.close(); 
        plotIt();
    } else {
        setTimeout(showInterval, ONE_SEC);
    }
}
function plotIt(){
    if (abort) return;

    progressPath.push(path[currStep]);
    stepPoly.setPath(progressPath);
    map.panTo(path[currStep]);
    var transitionMS = legs[currStep].duration / multiSpeed;
    for (var i=0; i<markerDivs.length; i++){
        markerDivs[i].style.transitionDuration = transitionMS + "ms";
    }
    HandG.setPosition(path[currStep])

    if (++currStep >= path.length)
        nextFunc = cleanUp;

    plotTimer = setTimeout(nextFunc,transitionMS);
}
function cleanUp(){
    infoWindow.close();
    hat.setAnimation();
    btn.value = "Replay";
    btn.disabled = false;
    clearTimeout(plotTimer);
    for (var i=0; i<markerDivs.length; i++){
        markerDivs[i].style.transitionDuration = "0s";
    }
    HandG.setPosition(
        new google.maps.LatLng(
                lastPos.coords.latitude,
                lastPos.coords.longitude)); 
    HandG.setVisible(false);
    map.setOptions({gestureHandling: "cooperative"});
    zoomIn.style.display  = "";
    zoomOut.style.display = "";
    if (canTalk && !abort)
        speechSynthesis.speak(finish);
}
function waitForMarker(mutations, myInstance) {
    outer:
    for (var i=0; i<mutations.length; i++){
        if (mutations[i].type           == "attributes" && 
            mutations[i].target.tagName == "IMG"        &&
            mutations[i].target.src.toLowerCase().indexOf(MARKER_SRC) != -1){
            console.log("result")
            myInstance.disconnect();
            setTimeout(plotTrip,0)
            break outer;
        }
        if (mutations[i].type != "childList" ||
            mutations[i].addedNodes.length   == 0) 
            continue;
        for (var j=0; j<mutations[i].addedNodes.length; j++) {
            var node = mutations[i].addedNodes[j];
            if (node.tagName == "DIV" && node.firstChild && node.firstChild.tagName == "IMG" &&
                node.firstChild.src.toLowerCase().indexOf(MARKER_SRC) != -1){
                console.log(node.firstChild.src);
                myInstance.disconnect();
                setTimeout(plotTrip,0)
                break outer;
            }
        }
    }
}       

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何纠正在Google Maps v2中平滑移动标记的问题?

google maps API:如何使标记可点击?

使用Google Maps API进行标记的说明

使用Google Maps API查找最近的标记

使用Google Maps API显示标记

如何使用Google Maps API在缩小时收集标记?

群组标记-Google Maps API

如何使用javascript平滑移动图像?

如何在Google Maps API上的标记周围添加圆圈

Google Maps API v2:如何使标记可点击?

最终如何在Google Maps API中更改标记位置?

使用Google Maps JS API的键盘可访问标记

Google Maps API使用markerclusterer检测标记点击

Google Maps API:尝试使用数组删除多个标记

使用php的Google Maps Api

如何平滑移动UIImageViews数组?

如何使用JavaScript在Google Maps API中仅获取一个标记

如何更改Google Maps API图形图层控件中使用的默认标记

Flutter:如何使用新的Marker API将标记添加到Google Maps?

如何在Google Maps API v3中使用SVG标记

如何使用新的Google Maps官方地图标记(JavaScript API)12月17日

如何在Android上使用Google Maps API v2很好地管理标记

如何使用Google Maps API显示自定义标记以生成静态地图图像

如何使用Firebase Google Maps API Android中的数据更新标记位置

Google Maps v3 API - 自动平滑放大到标记

在Google Maps Android中使用GPS移动标记

使用Google Maps Javascript API添加标记,使其外观与在maps.google.com中添加的标记完全相同

Google Maps API 不再显示路线或标记

其他标记信息Google Maps Api