Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Create Moving Marker Functionality on Google Map with jQuery and HTML

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.33k
    Comment on it

    How to move markers on a google map api without clicking on any object ?

    The above can be achieved with simple logic . ie update the marker position after a particular time ( Let say 1 second in our example )

    The following HTML and jquery code will help to achieve the moving marker functionality.

    Note : You will require a google map API key to pass in the script.

    1. <hmtl>
    2. <head>
    3. <script src="http://code.jquery.com/jquery-1.9.0.js"></script>
    4. <!-- Plz Your unique google map api key -->
    5. <script src="https://maps.googleapis.com/maps/api/js?v=3&&key=API-KEY" type="text/javascript"></script>
    6.  
    7. <style type="text/css">
    8. #map{
    9. width: 100%;
    10. height: 900px;
    11. border: 0px;
    12. padding: 0px;
    13. }
    14. </style>
    15. </head>
    16.  
    17. <body>
    18.  
    19. <section class="mappageList">
    20. <div>
    21.  
    22. <div class="google-mapsPage">
    23.  
    24. <div class="map-responsive">
    25. <!-- Define the div area on which the map will be loaded. -->
    26. <div id="map"></div>
    27.  
    28. </div>
    29.  
    30. </div>
    31.  
    32. </div>
    33. </section>
    34.  
    35. <script type="text/javascript">
    36.  
    37. // Set initial latitude and longitude for map and starting point for marker.
    38.  
    39. var lat_initial = 30.56562;
    40. var long_initial = 1.8805050;
    41.  
    42. // Load initial map with initial latitude and longitude on page load.
    43.  
    44. initMap();
    45.  
    46. function initMap()
    47. {
    48. map = new google.maps.Map(document.getElementById("map"), {
    49.  
    50. center: new google.maps.LatLng(lat_initial,long_initial),
    51. zoom: 5,
    52. mapTypeId: google.maps.MapTypeId.ROADMAP,
    53. mapTypeControl: true,
    54. mapTypeControlOptions:
    55. {
    56. style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
    57. },
    58. scrollwheel: true,
    59. navigationControl: true,
    60. scaleControl: false,
    61. disableDoubleClickZoom: true,
    62. zoomControl: true,
    63. zoomControlOptions: {
    64. position: google.maps.ControlPosition.RIGHT_TOP,
    65. },
    66.  
    67. });
    68. }
    69.  
    70. var INTERVAL = 1000;
    71.  
    72. var markers_array = [];
    73.  
    74. // Call function to update marker position after every second.
    75.  
    76. getMarkers();
    77.  
    78. function getMarkers() {
    79.  
    80. // Stop calling functional if markers are reached at certain constant value.
    81.  
    82. if ( lat_initial == '35.735619999999976' && long_initial== '7.990504999999995' ) {
    83. return false; // exit the loop
    84.  
    85. } else {
    86.  
    87. // Change latitude and longitude values by adding some amount to previous values. You can put your own logic as desired according to requirement.
    88.  
    89. lat_initial = parseFloat(lat_initial)+0.11;
    90.  
    91. long_initial = parseFloat(long_initial)+0.13;
    92.  
    93. // remove old marker
    94. for(i=0; i<markers_array.length; i++){
    95. markers_array[i].setMap(null);
    96. }
    97.  
    98. // add new marker
    99. addMarkers(lat_initial, long_initial);
    100. // Call the function recursively after every second.
    101.  
    102. window.setTimeout(getMarkers,INTERVAL);
    103. }
    104.  
    105. }
    106. // Function called to add markers on the map.
    107. function addMarkers(lats, longs) {
    108.  
    109. var marker = new google.maps.Marker({
    110. position: new google.maps.LatLng(lats, longs),
    111. map:map
    112. });
    113.  
    114. markers_array.push(marker);
    115.  
    116. }
    117.  
    118.  
    119. </script>
    120.  
    121. </body>
    122. </html>

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: