Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8850825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:59:23+00:00 2026-06-14T12:59:23+00:00

I’m using websockets to send data from the server to the client. This works

  • 0

I’m using websockets to send data from the server to the client. This works fine so for example I can display messages the server is sending to the client as they are received.
I’m able to convert this into longitude and latitudes.

So as data is recieved from the server it should plot onto the map in my html, but it doesn’t seem to be plotting anything at all.

I’m completely new to javascript so am unsure if my code is correct. What I would like to ideally do is has soon data is received it is plotted straight onto the map and thus generate the heat map. This is the javascript side of the code:

<script>
    $(document).ready(function() {
        var ws = new WebSocket("ws://" + "localhost" + ":" + "8888" + "/ws");
        var London = new google.maps.LatLng(51.507335, -0.127683);

    var map = new google.maps.Map(document.getElementById('map_canvas'), {
        center: London,
        zoom: 3,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var pointArray = new google.map.MVCArray([]);           
    ws.onmessage = function(evt){  
            //$("#display").append(evt.data + "<br />");

            var msg = JSON.parse(evt.data);
            var coordinates = msg.coordinates.coordinates;
            var latLng = new google.maps.LatLng(coordinates[1], coordinates[0]);
            console.log('received');
            pointArray.push(latLng);
            var heatmap = new google.maps.visualization.HeatmapLayer({
                data: pointArray
            });
            heatmap.setMap(map);

            //$("#display").append(latLng + "<br />");

            //console.log(evt.data);

    };

    ws.onclose = function(evt) {alert("Server connection terminated");};
   });      
    $("#open").click(function(evt){
    evt.preventDefault();
    $.post("/", $("#eventForm").serialize());   

});

</script>

Would there be a way so that as data is received it’s stored into an array, BUT then I’m not sure how then I could update the map with element in the array has it is received.
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:59:25+00:00Added an answer on June 14, 2026 at 12:59 pm

    I can’t say for sure if this is the only issue, but you appear to have a scoping issue here –

    ws.onmessage = function (evt) {
        var msg = JSON.parse(evt.data);
        var coordinates = msg.coordinates.coordinates;
        var latLng = new google.maps.LatLng(coordinates[1], coordinates[0]);
    };
    ...
    var heatmap = new google.maps.visualization.HeatmapLayer({
        data: latLng
    });
    

    The way scopes work in javascript is that latLng is only available to code within the function it is declared. An easy way to fix this would be to just declare it in the scope they share –

    var latLng;
    ws.onmessage = function (evt) {
        var msg = JSON.parse(evt.data);
        var coordinates = msg.coordinates.coordinates;
        latLng = new google.maps.LatLng(coordinates[1], coordinates[0]);
    };
    ...
    var heatmap = new google.maps.visualization.HeatmapLayer({
        data: latLng
    });
    

    This will make latLng accessible to other parts of the code in the click function.

    A few other possible issues:

    If you want to progressively add data to a map, your structure is in general a bit off. What you’d want to do is create a single map that’s inside the webpage, then continually add data to it – there’s no need to create a new map each time. What you probably should be doing is creating the map inside of the ready function, not the click.

    Also, there is a single heatmap layer in a google map; if you create new heatmaps for each bit of received data, you’ll be overriding everything that you have put into prior heatmaps. Given your most recent edit it looks like you’ve already reached the same conclusion though.

    I’m also not quite sure your websocket code is quite as good as it needs to be. The websocket “onmessage” function will not immediately return results: rather, it provides instructions on how to handle any data that comes in at any point in the future. So, if you want to update the map every time that data comes in, you do not need to do anything with websockets on a click event.

    This will lead to code that is loosely structured like this –

    $(document).ready(function () {
        var ws = new WebSocket("ws://" + "localhost" + ":" + "8888" + "/ws");
        var London = new google.maps.LatLng(51.507335, -0.127683);
    
        var map = new google.maps.Map(document.getElementById('map_canvas'), {
            center: London,
            zoom: 3,
            mapTypeId: google.maps.MapTypeId.SATELLITE
        });
    
        var pointArray = new google.map.MVCArray([]);
        ws.onmessage = function (evt) {
            var msg = JSON.parse(evt.data);
            var coordinates = msg.coordinates.coordinates;
            pointArray.push(new google.maps.LatLng(coordinates[1], coordinates[0]));
            var heatmap = new google.maps.visualization.HeatmapLayer({
                data: pointArray
            });
            heatmap.setMap(map);
        };
    
        $("#open").click(function (evt) {
            // whatever code you want click to do
        });
    });
    

    I cannot test any of this code as I do not have your specific websocket setup, but hopefully this gives you a better grounding to go from here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.