Moving marker along path/polyline using Leaflet API?Drawing polyline in Leaflet?how to customize the marker...

How to stop co-workers from teasing me because I know Russian?

Is there a way to generate a list of distinct numbers such that no two subsets ever have an equal sum?

Does tea made with boiling water cool faster than tea made with boiled (but still hot) water?

What are the steps to solving this definite integral?

What is the most expensive material in the world that could be used to create Pun-Pun's lute?

Overlay of two functions leaves gaps

What does the integral of a function times a function of a random variable represent, conceptually?

Does a large simulator bay have standard public address announcements?

Can SQL Server create collisions in system generated constraint names?

Why didn't the Space Shuttle bounce back into space as many times as possible so as to lose a lot of kinetic energy up there?

Multiple options vs single option UI

Minor Revision with suggestion of an alternative proof by reviewer

Can't get 5V 3A DC constant

Why did C use the -> operator instead of reusing the . operator?

"Hidden" theta-term in Hamiltonian formulation of Yang-Mills theory

Is Diceware more secure than a long passphrase?

What makes accurate emulation of old systems a difficult task?

Is there really no use for MD5 anymore?

Don’t seats that recline flat defeat the purpose of having seatbelts?

How does Captain America channel this power?

Why does Mind Blank stop the Feeblemind spell?

Checks user level and limit the data before saving it to mongoDB

How exactly does Hawking radiation decrease the mass of black holes?

"You've called the wrong number" or "You called the wrong number"



Moving marker along path/polyline using Leaflet API?


Drawing polyline in Leaflet?how to customize the marker using leafletChange marker icon on click using leafletLeaflet draggable MarkerStart-Marker from GeoJSON polylineLeaflet Marker Cluster not appearingLeaflet Marker - toggle draggableWhile Dragging, created marker with connected polyline from toolbar,previous polyline between two marker deleted using leaflet?Adding a marker in leafletLeaflet Marker autoPan option






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















Trying to migrate from Googlemaps to Leaflet, some errors return in following functions.



Number.prototype.toRad = function() {
return this * Math.PI / 180;
}
//
Number.prototype.toDeg = function() {
return this * 180 / Math.PI;
}
// polyline length calc
L.Polyline.prototype.m_len = function () {
var distance = 0, coords = null, coordsArray = this._latlngs;
for (i = 0; i < coordsArray.length - 1; i++) {
coords = coordsArray[i];
distance += coords.distanceTo(coordsArray[i + 1]);
}
return distance;
}
//
function moveAlongPath(point, distance, index) {
index = index || 0; // Set index to 0 by default.
if (index < routePoints.length) {
var poly = new L.Polyline([routePoints[index], routePoints[index + 1]]);
// alert(JSON.stringify(poly, null, 4));
var distanceToNextPoint = poly.m_len();
if (distance <= distanceToNextPoint) {
return routePoints[index].moveTowards(routePoints[index + 1], distance);
// error: routePoints[index].moveTowards is not a function
} else {
var d_distance = distance - distanceToNextPoint;
return moveAlongPath(routePoints, d_distance, (index + 1));
}
} else {
return null;
}
} // end moveAlongPath
/////////////////////////////////
L.LatLng.prototype.moveTowards = function(point, distance) {
var lat1 = this.lat.toRad();
var lon1 = this.lng.toRad();
var lat2 = point.lat().toRad();
var lon2 = point.lng().toRad();
var dLon = (point.lng() - this.lng()).toRad();
// Find the bearing from this point to the next.
var brng = Math.atan2(Math.sin(dLon) * Math.cos(lat2),
Math.cos(lat1) * Math.sin(lat2) -
Math.sin(lat1) * Math.cos(lat2) *
Math.cos(dLon));
var angDist = distance / 6371000 ; // 66371000 Earth's radius.
// Calculate the destination point, given the source and bearing.
lat2 = Math.asin(Math.sin(lat1) * Math.cos(angDist) +
Math.cos(lat1) * Math.sin(angDist) *
Math.cos(brng));
lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(angDist) *
Math.cos(lat1),
Math.cos(angDist) - Math.sin(lat1) *
Math.sin(lat2));
if (isNaN(lat2) || isNaN(lon2)) {return null; }
return L.LatLng(lat2.toDeg(), lon2.toDeg());
} // end moveTowards
// LINESTRING COORDINATES KML FILE
var routePoints = <?php echo $coordstring;?>
var polycolor = <?php echo $race_polyclr;?>
var stroke_w = 5;
var polyline = L.polyline(routePoints, {color: polycolor, weight: stroke_w});
map.addLayer(polyline)









share|improve this question
















bumped to the homepage by Community 23 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.






















    0















    Trying to migrate from Googlemaps to Leaflet, some errors return in following functions.



    Number.prototype.toRad = function() {
    return this * Math.PI / 180;
    }
    //
    Number.prototype.toDeg = function() {
    return this * 180 / Math.PI;
    }
    // polyline length calc
    L.Polyline.prototype.m_len = function () {
    var distance = 0, coords = null, coordsArray = this._latlngs;
    for (i = 0; i < coordsArray.length - 1; i++) {
    coords = coordsArray[i];
    distance += coords.distanceTo(coordsArray[i + 1]);
    }
    return distance;
    }
    //
    function moveAlongPath(point, distance, index) {
    index = index || 0; // Set index to 0 by default.
    if (index < routePoints.length) {
    var poly = new L.Polyline([routePoints[index], routePoints[index + 1]]);
    // alert(JSON.stringify(poly, null, 4));
    var distanceToNextPoint = poly.m_len();
    if (distance <= distanceToNextPoint) {
    return routePoints[index].moveTowards(routePoints[index + 1], distance);
    // error: routePoints[index].moveTowards is not a function
    } else {
    var d_distance = distance - distanceToNextPoint;
    return moveAlongPath(routePoints, d_distance, (index + 1));
    }
    } else {
    return null;
    }
    } // end moveAlongPath
    /////////////////////////////////
    L.LatLng.prototype.moveTowards = function(point, distance) {
    var lat1 = this.lat.toRad();
    var lon1 = this.lng.toRad();
    var lat2 = point.lat().toRad();
    var lon2 = point.lng().toRad();
    var dLon = (point.lng() - this.lng()).toRad();
    // Find the bearing from this point to the next.
    var brng = Math.atan2(Math.sin(dLon) * Math.cos(lat2),
    Math.cos(lat1) * Math.sin(lat2) -
    Math.sin(lat1) * Math.cos(lat2) *
    Math.cos(dLon));
    var angDist = distance / 6371000 ; // 66371000 Earth's radius.
    // Calculate the destination point, given the source and bearing.
    lat2 = Math.asin(Math.sin(lat1) * Math.cos(angDist) +
    Math.cos(lat1) * Math.sin(angDist) *
    Math.cos(brng));
    lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(angDist) *
    Math.cos(lat1),
    Math.cos(angDist) - Math.sin(lat1) *
    Math.sin(lat2));
    if (isNaN(lat2) || isNaN(lon2)) {return null; }
    return L.LatLng(lat2.toDeg(), lon2.toDeg());
    } // end moveTowards
    // LINESTRING COORDINATES KML FILE
    var routePoints = <?php echo $coordstring;?>
    var polycolor = <?php echo $race_polyclr;?>
    var stroke_w = 5;
    var polyline = L.polyline(routePoints, {color: polycolor, weight: stroke_w});
    map.addLayer(polyline)









    share|improve this question
















    bumped to the homepage by Community 23 mins ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.


















      0












      0








      0








      Trying to migrate from Googlemaps to Leaflet, some errors return in following functions.



      Number.prototype.toRad = function() {
      return this * Math.PI / 180;
      }
      //
      Number.prototype.toDeg = function() {
      return this * 180 / Math.PI;
      }
      // polyline length calc
      L.Polyline.prototype.m_len = function () {
      var distance = 0, coords = null, coordsArray = this._latlngs;
      for (i = 0; i < coordsArray.length - 1; i++) {
      coords = coordsArray[i];
      distance += coords.distanceTo(coordsArray[i + 1]);
      }
      return distance;
      }
      //
      function moveAlongPath(point, distance, index) {
      index = index || 0; // Set index to 0 by default.
      if (index < routePoints.length) {
      var poly = new L.Polyline([routePoints[index], routePoints[index + 1]]);
      // alert(JSON.stringify(poly, null, 4));
      var distanceToNextPoint = poly.m_len();
      if (distance <= distanceToNextPoint) {
      return routePoints[index].moveTowards(routePoints[index + 1], distance);
      // error: routePoints[index].moveTowards is not a function
      } else {
      var d_distance = distance - distanceToNextPoint;
      return moveAlongPath(routePoints, d_distance, (index + 1));
      }
      } else {
      return null;
      }
      } // end moveAlongPath
      /////////////////////////////////
      L.LatLng.prototype.moveTowards = function(point, distance) {
      var lat1 = this.lat.toRad();
      var lon1 = this.lng.toRad();
      var lat2 = point.lat().toRad();
      var lon2 = point.lng().toRad();
      var dLon = (point.lng() - this.lng()).toRad();
      // Find the bearing from this point to the next.
      var brng = Math.atan2(Math.sin(dLon) * Math.cos(lat2),
      Math.cos(lat1) * Math.sin(lat2) -
      Math.sin(lat1) * Math.cos(lat2) *
      Math.cos(dLon));
      var angDist = distance / 6371000 ; // 66371000 Earth's radius.
      // Calculate the destination point, given the source and bearing.
      lat2 = Math.asin(Math.sin(lat1) * Math.cos(angDist) +
      Math.cos(lat1) * Math.sin(angDist) *
      Math.cos(brng));
      lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(angDist) *
      Math.cos(lat1),
      Math.cos(angDist) - Math.sin(lat1) *
      Math.sin(lat2));
      if (isNaN(lat2) || isNaN(lon2)) {return null; }
      return L.LatLng(lat2.toDeg(), lon2.toDeg());
      } // end moveTowards
      // LINESTRING COORDINATES KML FILE
      var routePoints = <?php echo $coordstring;?>
      var polycolor = <?php echo $race_polyclr;?>
      var stroke_w = 5;
      var polyline = L.polyline(routePoints, {color: polycolor, weight: stroke_w});
      map.addLayer(polyline)









      share|improve this question
















      Trying to migrate from Googlemaps to Leaflet, some errors return in following functions.



      Number.prototype.toRad = function() {
      return this * Math.PI / 180;
      }
      //
      Number.prototype.toDeg = function() {
      return this * 180 / Math.PI;
      }
      // polyline length calc
      L.Polyline.prototype.m_len = function () {
      var distance = 0, coords = null, coordsArray = this._latlngs;
      for (i = 0; i < coordsArray.length - 1; i++) {
      coords = coordsArray[i];
      distance += coords.distanceTo(coordsArray[i + 1]);
      }
      return distance;
      }
      //
      function moveAlongPath(point, distance, index) {
      index = index || 0; // Set index to 0 by default.
      if (index < routePoints.length) {
      var poly = new L.Polyline([routePoints[index], routePoints[index + 1]]);
      // alert(JSON.stringify(poly, null, 4));
      var distanceToNextPoint = poly.m_len();
      if (distance <= distanceToNextPoint) {
      return routePoints[index].moveTowards(routePoints[index + 1], distance);
      // error: routePoints[index].moveTowards is not a function
      } else {
      var d_distance = distance - distanceToNextPoint;
      return moveAlongPath(routePoints, d_distance, (index + 1));
      }
      } else {
      return null;
      }
      } // end moveAlongPath
      /////////////////////////////////
      L.LatLng.prototype.moveTowards = function(point, distance) {
      var lat1 = this.lat.toRad();
      var lon1 = this.lng.toRad();
      var lat2 = point.lat().toRad();
      var lon2 = point.lng().toRad();
      var dLon = (point.lng() - this.lng()).toRad();
      // Find the bearing from this point to the next.
      var brng = Math.atan2(Math.sin(dLon) * Math.cos(lat2),
      Math.cos(lat1) * Math.sin(lat2) -
      Math.sin(lat1) * Math.cos(lat2) *
      Math.cos(dLon));
      var angDist = distance / 6371000 ; // 66371000 Earth's radius.
      // Calculate the destination point, given the source and bearing.
      lat2 = Math.asin(Math.sin(lat1) * Math.cos(angDist) +
      Math.cos(lat1) * Math.sin(angDist) *
      Math.cos(brng));
      lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(angDist) *
      Math.cos(lat1),
      Math.cos(angDist) - Math.sin(lat1) *
      Math.sin(lat2));
      if (isNaN(lat2) || isNaN(lon2)) {return null; }
      return L.LatLng(lat2.toDeg(), lon2.toDeg());
      } // end moveTowards
      // LINESTRING COORDINATES KML FILE
      var routePoints = <?php echo $coordstring;?>
      var polycolor = <?php echo $race_polyclr;?>
      var stroke_w = 5;
      var polyline = L.polyline(routePoints, {color: polycolor, weight: stroke_w});
      map.addLayer(polyline)






      leaflet






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 24 '18 at 9:41









      PolyGeo

      54.1k1782247




      54.1k1782247










      asked May 22 '18 at 19:35









      StefanoStefano

      11




      11





      bumped to the homepage by Community 23 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 23 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          1 Answer
          1






          active

          oldest

          votes


















          0














          If you're migrating from GMaps to Leaflet, then you should check the Leaflet plugin list before implementing any behaviour not included in the default Leaflet build.



          In your particular case, you should try using Leaflet.AnimatedMarker instead of implementing your own animation routines. Let me quote from the README file of that plugin:



          var line = L.polyline([[40.68510, -73.94136],[40.68576, -73.94149],[40.68649, -73.94165]]),
          animatedMarker = L.animatedMarker(line.getLatLngs());

          map.addLayer(animatedMarker);





          share|improve this answer
























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "79"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f283788%2fmoving-marker-along-path-polyline-using-leaflet-api%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            If you're migrating from GMaps to Leaflet, then you should check the Leaflet plugin list before implementing any behaviour not included in the default Leaflet build.



            In your particular case, you should try using Leaflet.AnimatedMarker instead of implementing your own animation routines. Let me quote from the README file of that plugin:



            var line = L.polyline([[40.68510, -73.94136],[40.68576, -73.94149],[40.68649, -73.94165]]),
            animatedMarker = L.animatedMarker(line.getLatLngs());

            map.addLayer(animatedMarker);





            share|improve this answer




























              0














              If you're migrating from GMaps to Leaflet, then you should check the Leaflet plugin list before implementing any behaviour not included in the default Leaflet build.



              In your particular case, you should try using Leaflet.AnimatedMarker instead of implementing your own animation routines. Let me quote from the README file of that plugin:



              var line = L.polyline([[40.68510, -73.94136],[40.68576, -73.94149],[40.68649, -73.94165]]),
              animatedMarker = L.animatedMarker(line.getLatLngs());

              map.addLayer(animatedMarker);





              share|improve this answer


























                0












                0








                0







                If you're migrating from GMaps to Leaflet, then you should check the Leaflet plugin list before implementing any behaviour not included in the default Leaflet build.



                In your particular case, you should try using Leaflet.AnimatedMarker instead of implementing your own animation routines. Let me quote from the README file of that plugin:



                var line = L.polyline([[40.68510, -73.94136],[40.68576, -73.94149],[40.68649, -73.94165]]),
                animatedMarker = L.animatedMarker(line.getLatLngs());

                map.addLayer(animatedMarker);





                share|improve this answer













                If you're migrating from GMaps to Leaflet, then you should check the Leaflet plugin list before implementing any behaviour not included in the default Leaflet build.



                In your particular case, you should try using Leaflet.AnimatedMarker instead of implementing your own animation routines. Let me quote from the README file of that plugin:



                var line = L.polyline([[40.68510, -73.94136],[40.68576, -73.94149],[40.68649, -73.94165]]),
                animatedMarker = L.animatedMarker(line.getLatLngs());

                map.addLayer(animatedMarker);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered May 23 '18 at 8:41









                IvanSanchezIvanSanchez

                6,3531619




                6,3531619






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f283788%2fmoving-marker-along-path-polyline-using-leaflet-api%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    (145452) 2005 RN43 Классификация | Примечания | Ссылки |...

                    Щит и меч (фильм) Содержание Названия серий | Сюжет |...

                    Энтрерриос (город) Содержание История | Географическое...