In this article, you will learn about how to create moving Google map marker smoothly using Google maps JavaScript API. You can easily display maps on the website using Google Maps API and point the location using marker on map and show the html/text on info popup window.
You can implement Google map functionality in various way on the web page as per your requirement. If you need to extend Google Map functionality and the animated marker feature than you can do it easily in a user friendly way. Now, we will show you how to move google map location marker smoothly on google map using Google Maps JavaScript API.
In this below example script, you will see a map with a location marker using Google Maps JavaScript API. On clinking on the Google Map different location the marker moves smoothly and changed marker position.
Google Maps API is used to generate google map, so load the JavaScript API First under your <head></head> tags. You need to change API Key in API URL for using the Google Maps JavaScript API.
2 3 4 5 6 |
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script> |
If you don’t know, How you will get Google Maps JavaScript API Key. Please follow our step by step instruction to get your own API key:- How to create Google Maps JavaScript API Key.
You need to copy and paste below JavaScript Code under <head> or under <body> tags.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<script> var position = [28.4594965, 77.0266383]; function initialize() { var latlng = new google.maps.LatLng(position[0], position[1]); var myOptions = { zoom: 16, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById("mapCanvas"), myOptions); marker = new google.maps.Marker({ position: latlng, map: map, title: "Latitude:"+position[0]+" | Longitude:"+position[1] }); google.maps.event.addListener(map, 'click', function(event) { var result = [event.latLng.lat(), event.latLng.lng()]; transition(result); }); } //Load google map google.maps.event.addDomListener(window, 'load', initialize); var numDeltas = 100; var delay = 10; //milliseconds var i = 0; var deltaLat; var deltaLng; function transition(result){ i = 0; deltaLat = (result[0] - position[0])/numDeltas; deltaLng = (result[1] - position[1])/numDeltas; moveMarker(); } function moveMarker(){ position[0] += deltaLat; position[1] += deltaLng; var latlng = new google.maps.LatLng(position[0], position[1]); marker.setTitle("Latitude:"+position[0]+" | Longitude:"+position[1]); marker.setPosition(latlng); if(i!=numDeltas){ i++; setTimeout(moveMarker, delay); } } </script> |
initialize() function creates a google Map with location Marker. transition() & moveMarker() are used to move location marker smoothly on Google Map.
HTML Code:
2 3 4 5 6 |
<div id="mapCanvas"></div> |
The above HTML code define the Google Map display area. The selector ID (mapCanvas) need to be specified in the initialize() function.
2 3 4 5 6 7 8 9 |
#mapCanvas{ width:100%; height:500px; } |
The above CSS Code is used to define Google Map width and height. You need to put under <head> tags.
Google Map animated marker Complete Code:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
<html> <head> <title>How to move animated marker smoothly using Google Map Javascript API- Tutorialswebsite </title> <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script> <script> var position = [28.4594965, 77.0266383]; function initialize() { var latlng = new google.maps.LatLng(position[0], position[1]); var myOptions = { zoom: 16, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById("mapCanvas"), myOptions); marker = new google.maps.Marker({ position: latlng, map: map, title: "Latitude:"+position[0]+" | Longitude:"+position[1] }); google.maps.event.addListener(map, 'click', function(event) { var result = [event.latLng.lat(), event.latLng.lng()]; transition(result); }); } //Load google map google.maps.event.addDomListener(window, 'load', initialize); var numDeltas = 100; var delay = 10; //milliseconds var i = 0; var deltaLat; var deltaLng; function transition(result){ i = 0; deltaLat = (result[0] - position[0])/numDeltas; deltaLng = (result[1] - position[1])/numDeltas; moveMarker(); } function moveMarker(){ position[0] += deltaLat; position[1] += deltaLng; var latlng = new google.maps.LatLng(position[0], position[1]); marker.setTitle("Latitude:"+position[0]+" | Longitude:"+position[1]); marker.setPosition(latlng); if(i!=numDeltas){ i++; setTimeout(moveMarker, delay); } } </script> <style> #mapCanvas{ width: 100%; height: 400px; } </style> </head> <body> <div id="mapCanvas"></div> </body> </html> |
Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co