How to add multiple pin on Google Maps JavaScript dynamically?
Add multiple pin on Google Maps (marker) is a powerful feature if you’re building a location-based application. This tutorial will explain how to use the Google Maps JavaScript API, including retrieving location data from the database.
Create database and PHP backend
Use MySQLI database to store location details like (name, latitude, longitude and state). The following PHP code will fetch these details dynamically:
$conn=mysqli_connect("localhost","root","","location_db");
if(!$conn)
{
die("Connection Failed"). mysqli_connect_error()
}
$sql = "SELECT id, name, latitude, longitude, state FROM googlemap WHERE name != ''";
$result = mysqli_query($conn, $sql);
$franchiselocated = [];
while ($row = mysqli_fetch_array($result)) {
$franchiselocated[] = [
'name' => $row['name'],
'latitude' => $row['latitude'],
'longitude' => $row['longitude'],
'state' => $row['state']
];
}
mysqli_close($conn);
Database Connection and Data Fetching
- The script connects to a MySQL database where database name is (
location_db
) and retrieves location data from thegooglemap
table. - Only records with non-blank names are included in the map display if blank is found, so it will show a warning message please select state.
Dynamic Google Maps Display
- The
Google Maps JavaScript API
powers interactive maps embedded in web pages. - By default, the map is centered on India’s geographical coordinates (
lat: 20.5937
,lng: 78.9629
) and uses a zoom level of 5.
State-Based Filtering
- A dropdown menu allows users to select any specific state.
- Upon selection, the map dynamically filters and displays markers only for coding tutorial franchises located in the selected state.
Add Google map Script with Api
Add the Google Maps API script to your HTML file:
Dynamically add markers to the map
Here’s how to create a map and dynamically add multiple pins:
$row['name'],
'latitude' => $row['latitude'],
'longitude' => $row['longitude'],
'state' => $row['state']
];
}
$conn->close();
?>
Franchise Locations
Coding Tutorial Locations
Features of this implementation
- Dynamically Data Fetching: Locations are fetch from the database and added dynamically.
- State-Based Filtering: Users can select a state from a selection box to a filter pin.
- Interactive Map: Clicking on a pin displays additional information about the location like location name.
- Responsive Zoom: Map automatically adjusts zoom based on visible pins.
Benefits of This Google Map pin Application
- Interactive User Experience: Users can visually locate franchise locations on a map.
- Dynamic Data Filtering: State-based data filtering allows users to quickly narrow down locations of interest.
- Easy expandability: Developers can easily add more features, such as searching by city and additional filtering criteria.
- The
Google Maps JavaScript API
powers interactive maps embedded in web pages. - By default, the map is centered on India’s geographical coordinates (
lat: 20.5937
,lng: 78.9629
) and uses a zoom level of 5.
Google map multi pin locator Source code
FREE DOWNLOAD
Send download link to: