A guide on customizing ng-map markers by assigning colors to different categories

One of the functionalities I have allows users to see nearby locations based on their position and selected radius. These locations fall into different categories, and now I want to customize the markers' colors based on those categories.

Here is a snippet of my current code:

$scope.radius = 10;
nearBaysFactory.getNearBayInRadius(data.basicData[0].latitude, data.basicData[0].longitude, $scope.radius).then(function (data) {
    $scope.nearNearBays = data.nearBays;
    NgMap.getMap().then(function(map) {
    });
});

$scope.showSites = function (evt, id) {
    angular.forEach($scope.nearNearBays, function(value){
        if (value.id_near_bay == id) {
            $scope.selectedSite =
                value.name + "<br>" +
                value.address + "<br>" +
                "Category: " + value.category;
        }
    });
    $scope.showInfoWindow.apply(this, [evt, 'bar-info-window']);
};

Here's a glimpse at my HTML setup:

<ng-map id="custom" default-style="false"
        center="46.1478781,14.4326283" zoom="9">
    <!-- Default marker in red -->
    <marker position="{{location.basicData[0].latitude}},{{location.basicData[0].longitude}}" id="{{$index}}"
            on-click="showSites(event, n.id_location)">
    </marker>
    <!-- Custom-colored markers based on near bay category -->
    <marker ng-repeat="n in nearNearBays" position="{{n.latitude}},{{n.longitude}}" id="{{$index}}"
            on-click="showSites(event, n.id_near_bay)" color="blue">
    </marker>
    <info-window id="bar-info-window">
        <div ng-non-bindable>
            <div id="siteNotice"></div>
            <h1 id="firstHeading" class="firstHeading" style="font-size: 17px;">
                <div ng-bind-html="selectedSite"></div>
            </h1>
            <div id="bodyContent">
                <p>
                    <a href="/#/nearbays/nearbay/{{selectedSite.id_near_bay}}">Open near bay</a>
                </p>
            </div>
        </div>
    </info-window>
</ng-map>

Check out the current result https://i.sstatic.net/41Ncy.png

Here is an example of one of the near bay objects:

$scope.test = [
    {
        name: 'name 1',
        address: 'Address 1',
        id_resort_category: '8',
        category : 'Towing service'
    },
    {
        name: 'name 2',
        address: 'Address 2',
        id_resort_category: '1',
        category : 'Spa'
    }
];

I am seeking suggestions or guidance on how to render these near bay markers with different colors based on their category. Any additional information needed will be provided upon request. Thank you!

Answer №1

To customize the marker in your code, you can utilize a conditional statement to define the icon property.

Explore a variety of icons by visiting this link

Answer №2

After some experimentation, I found a solution... I included the icon URL or an absolute path

$scope.test = [
    {
        name: 'name 1',
        address: 'Address 1',
        id_resort_category: '8',
        category : 'Towing service'
        img: 'http://www.iconarchive.com/download/i2262/aha-soft/food/hamburger.ico'
    },
    {
        name: 'name 2',
        address: 'Address 2',
        id_resort_category: '1',
        category : 'Spa',
        img: 'http://www.iconarchive.com/download/i2262/aha-soft/food/hamburger.ico'
    }
];

Following that, I configured the marker icon location which, in my scenario, appeared as follows

<marker ng-repeat="n in nearNearBays" position="{{n.latitude}},{{n.longitude}}" icon="dist/img/googleMapsIcons/{{n.image}}">
</marker>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Please replace the text with only letters and then submit it without any spaces, periods, commas, or other symbols

Whenever I encounter a form like this, I need to submit it and replace the text in the name field with only letters - no commas, spaces, or special characters. https://i.sstatic.net/9FQnT.png myform.html <script> function fixInput(event) { na ...

The curious case of looping and peculiar Vue actions

I've been working on a project where I am looking to dynamically add more input fields upon clicking a button. My initial attempt using jQuery.append ran into issues with detecting v-model. Switching gears, I decided to try achieving the same outcom ...

Encountering a timeout error while using selenium-webdriver and phantomJS

Currently, I am developing my automated test scripts using a combination of selenium-webdriver, phantomJS, and mocha. The script I am working on is written in JavaScript. One requirement is to wait until a specific element becomes completely visible befo ...

What is the best way to integrate the each_slice method in Rails views with React components

In my Parent component Monsters, I am rendering the Monster component. for each monster in @state.monsters React.createElement Monster, key: monster.id, monster: monster, team: @state.team... Within the monster: div className: 'col-md-4' ...

Transfer sound data blob to server and store it as a file

Currently, I have 3 buttons on my webpage - one to start recording audio, one to stop the recording, and one to play the recorded audio. Using MediaRecorder makes it easy to capture audio as a blob data. However, I am facing issues when trying to upload th ...

What methods can be used to avoid regular expressions when searching for documents in MongoDB?

I am using a simple regular expression-based search in MongoDB like this: router.get('/search', function (req, res, next) { var text = req.query.text; collection.find({text: new ReqExp(text, 'ig')}, function (err, result) { ...

Having trouble with sluggish performance on openbim-components? The OrthoPerspectiveCamera tends to slow down or get stuck when zooming

While using the OrthoPerspectiveCamera in my App (from openbim-components package), I am facing an issue. Whenever I zoom into my model, it automatically switches to FirstPerson mode. This change makes it extremely slow to navigate and zoom in/out of the s ...

Unable to include a controller in AngularJS that is declared in a different file

I am new to using Angular. Currently, all of my controllers are defined in the controllers.js file following these patterns: function A($scope){.....} function B($scope){.....} function MainCtrl($http) {.....} . . . angular .module('myApp') ...

Implement a validation function in the "jQuery validation library."

Hello, I am currently using the jQuery validation plugin to validate my form. I am trying to add an additional rule to the validation, but I am struggling to get it to work. The input value should be less than or equal to the previous element's value. ...

The AngularJS ui-bootstrap modal challenge with instances

Encountering an issue with my AngularJS app (version 1.4.7) and the latest version of ui.bootstrap. The problem arises when attempting to inject $uibModalInstance, resulting in the error message Unknown provider: $uibModalInstanceProvider Below is the con ...

Transforming a JavaScript array into a JSON format

Is there a way to convert a JavaScript array into JSON format? [{ "userName": "Rodrigo", "date": "April 25, 2017", "score": 5 }] [{ "userName": "Jon", "date": "April 24, 2016", "score": 4 }] Error: There is a parser error on line ...

Issue with Stack Divider not appearing on Chakra UI card

I'm currently designing a card element using Chakra UI. However, I've noticed that the Stack Divider in the Card Body isn't displaying as expected. Is there a specific way it needs to be structured for it to show up? For example, should I se ...

What is the meaning of this CSS acronym?

div[id^=picture]:target{ /*applying various styles here*/ } I stumbled upon the snippet of code shown above on a website discussing CSS image galleries. Can anyone clarify what this code accomplishes? Appreciate it. ...

The grid items fail to refresh even after I have made changes to the useState in my React Native application

I am currently working on a react native project that includes a grid component. My goal is to update an array in the state called selectedHomeTypes whenever a user clicks on an item within the grid. Initially, the array is set to contain only one element: ...

JavaScript code can be used to access data from a JSON file and present the flower and color information in separate unordered lists

How can I retrieve data from a JSON file in JavaScript and display flowers and colors in separate unordered lists? I am having trouble adding it. fetch('dataFiles/midterm.json', { method: 'GET', mode: 'no-cors' ...

Tips on sending information from Express to my HBS template

var Ticket = require('./models/ticket.js'); module.exports = function (app) { app.get('/',function (req, res) { Ticket.find({},function (err, tickets) { if(err) { res.sen ...

refreshing a seraph or seraph model with new values

I am looking to modify an object in a Neo4j database using Seraph. The code I have been using seems to always add a new object instead of updating the selected one. app.put('/contactlist/:name',function (req,res){ db.find({name: req.params.name ...

Adjust the visual presentation based on the chosen option at the top of a column JQchart

Can anyone offer assistance with creating three radio button fields that will display yearly, monthly, or weekly data on a column chart? I have searched through numerous examples in JQchart but haven't found one for this specific scenario. Are there o ...

A more efficient method for creating a nested array of distinct values using JavaScript

The scenario: My website has a complex html table structure to showcase hierarchical data, with the ability for users to toggle visibility of individual rows. Each row is identified by a dom id consisting of a level number and primary key specific to that ...

How to submit form data with a POST request in Flask using fetch without having to reload

Despite reading numerous similar questions, I am still unable to determine how to achieve my goal. I have multiple forms on a single page and I am trying to submit data from each form without refreshing the page. Below is an example of one of the five form ...