Exploring NgMap and Heatmap functionality with dynamically loaded data

I'm struggling to integrate NgMaps with Heatmaps from the Google Maps API.

When I load the data from a script tag, everything works smoothly, similar to the NgMap example.

However, if I attempt to create the data array locally, it fails and throws an error stating,

invalid heatmap data <heatmap-layer id="foo" data="pointArray">
.

The goal is to update the array dynamically and refresh both the map and heatmap as needed.

This is my current approach:

HTML:

<ng-map center="21.088513,92.197204" zoom="16" map-type-id="SATELLITE" >
   <heatmap-layer id="foo" data="pointArray"></heatmap-layer>
</ng-map>

AngularJS:

$scope.outbreak = [
  [21.088, 92.19862],
  // other coordinates here
  [21.09083, 92.19574]
];

$scope.renderHeatMap = function() {

    var heatmap, vm = this;

    NgMap.getMap().then(function(map) {
      var outbreakfin = [];
      for (var i in $scope.outbreak) {
        outbreakfin.push(new google.maps.LatLng($scope.outbreak[i][0], $scope.outbreak[i][1]));
      };
      var pointArray = new google.maps.MVCArray(outbreakfin);

      heatmap = map.heatmapLayers.foo;

      heatmap = new google.maps.visualization.HeatmapLayer({
          data: pointArray
      });

      heatmap.setMap(map);
    });

};

I suspect that the heatmap layer isn't loading when expected.

What am I overlooking?

Answer №1

Here's the error you are encountering:

The heatmap data is invalid

This error occurs because the pointArray array needs to be defined in the controller's scope:

$scope.pointArray = [];

Additionally, there is no need to create a new instance of HeatmapLayer:

heatmap = new google.maps.visualization.HeatmapLayer({
      data: pointArray
  });
heatmap.setMap(map);

The HeatmapLayer directive already handles this creation and can be accessed from the controller like so in your case:

heatmap = map.heatmapLayers.foo;

Check out the modified example below:

angular.module('mapApp', ['ngMap'])
    .controller("MapCtrl", function ($scope, $http, NgMap) {

       
        $scope.pointArray = [];

        $scope.outbreak = [
            [21.088, 92.19862],
            // Add your outbreak coordinates here
        ];

        $scope.renderHeatMap = function () {
            
            NgMap.getMap().then(function (map) {
                var outbreakfin = [];
                for (var i in $scope.outbreak) {
                    outbreakfin.push(new google.maps.LatLng($scope.outbreak[i][0], $scope.outbreak[i][1]));
                };
                $scope.pointArray = new google.maps.MVCArray(outbreakfin);

                heatmap = map.heatmapLayers.foo;
                heatmap.setData($scope.pointArray);
            });
        };

        $scope.renderHeatMap();
    });
<script src="https://maps.google.com/maps/api/js?libraries=visualization"></script>
<script src="https://code.angularjs.org/1.3.15/angular.js"></script>
<script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script>

<div ng-app="mapApp" ng-controller="MapCtrl">
  
    <ng-map center="21.088513,92.197204" zoom="16" map-type-id="SATELLITE" >
        <heatmap-layer id="foo" data="pointArray"></heatmap-layer>
     </ng-map>
</div>

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

Determine the size of an SVG while taking into consideration any strokes applied

Is there a way to seamlessly position an SVG in the corner of a div, despite having a dynamically generated stroke? Calculating the distance to the outermost part of the border proves difficult when dealing with irregular shapes like stars. Here's my ...

Ensuring the accuracy of input data in all input fields with the help of dojo

1)In my Dojo widget, I have two additional widgets loaded inside. If I need to clear all text boxes within the widget, I currently use this method: this.myAttachPoint.value="". Is there an alternative code that can achieve the same result without adding ...

Extract source, medium, etc. data from __utmz GA with the help of JavaScript

Is it possible to retrieve the campaign, source, and medium that Google Analytics records all at once when the page loads? If so, could I use jQuery to look up the values from __utmz? ...

Tips for refreshing a GET request within a Vue.js application

I am receiving data from my Rails API backend and I would like to automatically refresh that GET request every 15 seconds. This way, if there are any changes on the backend (for example, if a POST request is made to another route), it will reload and retri ...

Is there a way to stop the execution of myFunc after it has been performed 5 times using clearInterval?

This code is designed to notify online shoppers that they need to choose a color from a dropdown menu before adding an item to their shopping cart. If no color is selected, the text will blink to alert them. How can I modify this code so that the blinking ...

Choose an image and save the selection information for the following page (Tarot card)

I'm in the process of creating a website that showcases multiple tarot cards. The goal is for users to select the cards they're interested in and have their chosen card displayed on the next page. I've implemented some code for selecting the ...

Troubles with Angular elements not aligning correctly when using the "display: block" property

When using an angular element for a directive with "display: block", it may not automatically take up 100% of the width of the parent container. In order to ensure that it does, the width must be explicitly set to "100%" in the CSS. Despite setting "width: ...

What is the best way to showcase the information of each object on a click event in Vue.js?

My goal with this code is to display each day's class schedule when it is clicked on. However, the issue I'm facing is that the entire week's schedule is being displayed instead of just the selected day. What adjustments should I make in ord ...

Update a roster with AJAX following the addition or removal of a user

My goal is to implement two functions: Adding and Deleting a User from the database using Mongoose. However, when I run the code, I receive a 200 OK response but with an empty username. I suspect there might be an issue with the ajax calls. Can anyone hel ...

Performing addition and subtraction calculations with HTML and Javascript

I need help creating a simple HTML table for adding and subtracting values, with a limit of 10 and a minimum of 0. I have two functions set up, additionalAdd and additionalSub, triggered by the onclick event, but I keep getting an error saying that totalAd ...

Using AJAX and PHP to redirect a PHP page once specific conditions are satisfied

Hello, I am currently facing an issue and I could use some guidance with Ajax as I am still learning how to work with it. The situation is this: I have a webpage that undergoes a refresh every 7 seconds using Ajax and Javascript. During each refresh, one ...

What is the best way to manage asynchronous data within AngularJS directives?

Having encountered similar challenges to this specific scenario, I am currently facing issues with handling asynchronous data within my directives. The main issue arises when trying to pass asynchronously fetched data into these directives. Initially, I at ...

Massive Memory Drain Due to XMLHttp POST Request

Is there a way to prevent XHR POST Memory leak in my web app? I have searched extensively for solutions but have not found any satisfactory answers. My issue is similar to the one described in this blog post that outlines the problem without offering any f ...

Dealing with 'ECONNREFUSED' error in React using the Fetch API

In my React code, I am interacting with a third party API. The issue arises when the Avaya One-X client is not running on the target PC, resulting in an "Error connection refused" message being logged continuously in the console due to the code running eve ...

Saving Information Using Express-Session

Imagine you need a user to input their email and then save it to be accessible across multiple web pages. Would it be considered a poor practice to store this email in the session object of express-session? For example: req.session.email = '<user ...

Struggling with the navbar-toggler in Bootstrap 4 Beta 2?

As part of my Bootstrap practice, I have implemented a navbar on my webpage. However, I am facing issues with the nav-bar toggler not working for small screens and the icon navbar-toggler-icon not appearing. Below is my current navbar code: <nav class ...

What is the best way to tally the frequency of words in a collection of URLs using JavaScript?

I have a JSON object in WordPress that contains a list of URLs. I would like to determine the frequency of occurrence of the second part of each URL. Currently, the code snippet below is able to extract the remaining part of the URL after the prefix https ...

Bootstrap3: Issue with Firefox - Unable to define height for div

Firstly, I'd like to share the link with you: While everything looks good in Chrome, the slider in Firefox seems to be pushed to the right side. This issue arises because the height of 'nav.navbar.navigation-9' is set to 50px. Even though I ...

Populate my HTML text box with content retrieved from JSON data

Here is the custom HTML text for the text box: <div class="row"> <div class="col-md-2"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button> <div class="alert alert ...

Attempting to insert items into a storage array and subsequently outputting them using a loop

I am attempting to add "contacts" to local storage, and every time I add a new contact I want to refresh it in a jQuery list. I have successfully achieved this without using local storage. However, now I am facing a problem that I can't seem to solve. ...