This project demonstrates how to simulate vehicle movement on a Google Map using @react-google-maps/api
and Google Maps geometry utilities. The vehicle moves along a predefined polyline path, and its marker dynamically updates its position and orientation as it moves.
- Polyline Path: Display a polyline route on the map.
- Start and End Markers: Mark the start and end of the route.
- Vehicle Simulation: The vehicle marker moves along the polyline at a specified speed.
- Dynamic Rotation: The vehicle's marker rotates to face the direction of movement.
- Custom Marker Icons: The vehicle marker is represented by a car icon.
- React
@react-google-maps/api
@mapbox/polyline
- Google Maps Geometry library
- Clone the repository
git clone https://github.com/your-username/google-maps-vehicle-simulation.git
cd google-maps-vehicle-simulation
Make sure you have Node.js installed. Run the following command to install the necessary dependencies:
npm install
You'll need to obtain a Google Maps API key to use the Google Maps services in this project.
Go to the Google Cloud Console. Create a new project (if needed). Go to the API & Services section. Enable the Maps JavaScript API and Places API. Generate an API key.
In the code, replace the placeholder with your actual Google Maps API key:
const { isLoaded } = useJsApiLoader({
googleMapsApiKey: 'YOUR_GOOGLE_MAPS_API_KEY'
});
Once you've set up the API key, start the development server:
npm start
- The map will load, displaying the polyline route.
- Click on the Start Simulation button to start moving the vehicle from the origin to the destination along the polyline path.
- The vehicle marker will rotate as it follows the path and will stop once it reaches the destination. Code Explanation
The polyline path is represented as an encoded string, which is decoded using the @mapbox/polyline
package. The decoded coordinates are then used to plot the polyline on the map.
const path = polyline.decode(encodedPolyline).map(([lat, lng]) => ({ lat, lng }));
The vehicle's position is updated at a set interval, and its movement is calculated based on the time elapsed since the simulation started. The setInterval function moves the vehicle along the polyline, and the vehicle's orientation is adjusted to match the direction of movement.
const moveObject = () => {
const distance = getDistance();
// Update position and progress
};
To rotate the vehicle marker, the computeHeading function from the Google Maps Geometry library is used to calculate the angle between the vehicle's current position and the next position. The marker is then rotated using a CSS transformation.
const angle = window.google.maps.geometry.spherical.computeHeading(lastLineLatLng, nextLineLatLng); marker.style.transform = rotate(${actualAngle}deg);
You can adjust the following parameters in the code:
- Vehicle Speed: Modify the
velocity
variable to set the speed of the vehicle. - Polyline Path: Change the
encodedPolyline
string to simulate different routes. - Marker Icon: Customize the marker by changing the
icon1
URL.
-
React: A JavaScript library for building user interfaces.
-
@react-google-maps/api: A lightweight library for integrating Google Maps into React applications.
-
@mapbox/polyline: A utility to encode and decode Google Maps polylines.
-
Google Maps Geometry Library: Provides utilities for spatial operations such as distance and heading calculation.
-
[@mapbox/polyline]: A utility to encode and decode Google Maps polylines.
-
Google Maps Geometry Library: Provides utilities for spatial operations such as distance and heading calculation.