Markers from database with different color

  backend, dictionary, javascript, markers, php

This is my map function with all the markers from database fetched and displayed. That works. Now i want to change markers color depending on database color. For example, I have for one marker color set to blue in database, I want to display marker as ‘blue-dot.png’, same for yellow, purple, and so on. Thanks.

function initMap() {
var map;

// Map center coordinates
const europe = {
            lat: 47.881400, 
            lng: 15.374455
        };

// Display map
var map = new google.maps.Map(document.getElementById('mapCanvas'), {
            zoom: 4,
            center: europe
        });
    
// Multiple markers location ( latitude, longitude )
var markers = [
    <?php if($result->num_rows > 0){
        while($row = $result->fetch_assoc()){
            echo '['.$row['latitude'].', '.$row['longitude'].', '.$row['color'].'],';
        }
    }
    ?>
];

// Place markers on the map  
for(i = 0; i < markers.length; i++) {
    var position = new google.maps.LatLng(markers[i][0], markers[i][1], markers[i][2]);
    bounds.extend(position);
    marker = new google.maps.Marker({
        position: position,
        map: map,
  });
}

}

Source: Ask Javascript Questions

LEAVE A COMMENT