How to highlight regions of dropdown selected area on Google Map php javascript

How to Highlight Regions of Dropdown Selected Area on Google Map in PHP and JavaScript?

If you are using Google Maps on your website, you might want to allow users to select a region from a dropdown menu and highlight that region on the map. This is particularly useful if you are showing data specific to certain regions, such as population density or crime rates.

Step 1: Set Up Your Google Map

The first step is to set up your Google Map. This involves creating a new map object, setting the center and zoom level, and adding any markers or polygons that you want to display.

function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 8,
    center: {lat: -34.397, lng: 150.644}
  // Add markers or polygons here

Step 2: Create Your Dropdown Menu

Next, you need to create your dropdown menu. This can be done using HTML and JavaScript. Here is an example:

<select id="region">
  <option value="sydney">Sydney</option>
  <option value="melbourne">Melbourne</option>
  <option value="brisbane">Brisbane</option>

<button onclick="highlightRegion()">Highlight Region</button>

In this example, we have created a simple dropdown menu with three options: Sydney, Melbourne, and Brisbane. When the user clicks the "Highlight Region" button, we will call a JavaScript function that will highlight the selected region on the map.

Step 3: Highlight the Selected Region

Finally, we need to write the JavaScript code that will highlight the selected region on the map. We can do this by creating a new polygon object and setting its path to the coordinates of the region we want to highlight.

function highlightRegion() {
  var region = document.getElementById('region').value;
  // Define coordinates for each region
  var sydneyCoords = [
    {lat: -33.890, lng: 151.274},
    {lat: -33.890, lng: 151.310},
    {lat: -33.880, lng: 151.310},
    {lat: -33.880, lng: 151.274}
  var melbourneCoords = [
    {lat: -37.820, lng: 144.960},
    {lat: -37.820, lng: 145.000},
    {lat: -37.780, lng: 145.000},
    {lat: -37.780, lng: 144.960}
  var brisbaneCoords = [
    {lat: -27.470, lng: 153.020},
    {lat: -27.470, lng: 153.080},
    {lat: -27.430, lng: 153.080},
    {lat: -27.430, lng: 153.020}
  // Create a new polygon object and set its path
  var polygon = new google.maps.Polygon({
    map: map,
    paths: eval(region + 'Coords'),
    strokeColor: '#FF0000',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#FF0000',
    fillOpacity: 0.35

In this code, we first get the value of the selected region from the dropdown menu. We then define the coordinates for each region as an array of objects. Finally, we create a new polygon object and set its path to the coordinates of the selected region. We also set the stroke color, opacity, weight, fill color, and fill opacity to customize the look of the polygon.

Now, when the user selects a region from the dropdown menu and clicks the "Highlight Region" button, the selected region will be highlighted on the map.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]