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

  • SEARCH
  • Home
  • 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 8215905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:56:39+00:00 2026-06-07T11:56:39+00:00

I have this code, that basically creates a D3.js map and creates a ‘circle’

  • 0

I have this code, that basically creates a D3.js map and creates a ‘circle’ when someone ‘hits’ the page based on their IP location, this was originally done in Raphael and i am trying to just use the D3.js library, but I am stuck on how to place the ‘circle’ directly on the map with the right co-ordinates:-

function ZmgcClient() {
    var personPath = "M255.968,166.154c34.206,0,61.936-27.727,61.936-61.934c0-34.208-27.729-61.936-61.936-61.936s-61.936,27.728-61.936,61.936 C194.032,138.428,221.762,166.154,255.968,166.154z M339.435,194.188c-13.082-13.088-28.625-20.924-84.83-20.924 c-56.214,0-71.38,8.505-83.796,20.924c-12.422,12.416-8.23,144.883-8.23,144.883l27.485-65.304l17.28,194.554l49.856-99.57 l46.521,99.57l16.456-194.554l27.487,65.304C347.664,339.07,352.521,207.271,339.435,194.188z";
    if (! (this instanceof arguments.callee)) {
        return new arguments.callee(arguments);
    }
    var self = this,
    width = $('#map').width(),
        mapCanvasHeight = (width * 0.45);

    this.init = function() {
        self.drawMap();
        self.setupBayeuxHandlers();
    };

    this.setupBayeuxHandlers = function() {
        $.getJSON("/config.json", function (config) {
            self.client = new Faye.Client("http://" + window.location.hostname + ':' + config.port + '/faye', {
                timeout: 120
            });

            self.client.subscribe('/stat', function (message) {
                // console.log("MESSAGE", message);
                self.drawMarker(message);
            });
        });
    };

    this.drawMap = function () {
        var data;
        // Most parts of D3 don't know anything about SVG—only DOM.
        self.map = d3.geo.equirectangular().scale(width);
        self.path = d3.geo.path().projection(self.map);
        self.svg = d3.select('#map').append('svg:svg')
            .attr("width", "100%")
            .attr("height", "100%")
            .attr("viewBox", "0 0 " + width + " " + mapCanvasHeight);

        self.countries = self.svg.append('svg:g').attr('id', 'countries');
        // Load data from .json file
        d3.json("/ui/data/world-countries.json", function(json) {
            self.countries.selectAll("path")    // select all the current path nodes
            .data(json.features)                // bind these to the features array in json
            .enter().append("path")             // if not enough elements create a new path
            .attr("d", self.path)               // transform the supplied jason geo path to svg
            .on("mouseover", function(d) {
                d3.select(this).style("fill","#6C0")
                    .append("svg:title")
                    .text(d.properties.name);})
            .on("mouseout", function(d) {
                d3.select(this).style("fill","#000000");})
        });
    }

    this.geoCoordsToMapCoords = function (latitude, longitude) {
        latitude = parseFloat(latitude);
        longitude = parseFloat(longitude);

        var mapWidth = width,
            mapHeight = mapCanvasHeight,
            x, y, mapOffsetX, mapOffsetY;

        x = (mapWidth * (180 + longitude) / 360) % mapWidth;

        latitude = latitude * Math.PI / 180;
        y = Math.log(Math.tan((latitude / 2) + (Math.PI / 4)));
        y = (mapHeight / 2) - (mapWidth * y / (2 * Math.PI));

        mapOffsetX = mapWidth * 0.026;
        mapOffsetY = mapHeight * 0.141;

        return {
            x: (x - mapOffsetX) * 0.97,
            y: (y + mapOffsetY + 15),
            xRaw: x,
            yRaw: y
        };
    }

    this.drawMarker = function (message) {
        var lon = message.longitude,
            lat = message.latitude,
            city = message.city;

        self.countries.append('svg:circle')
            .attr("cy", lon, lat) 
            .attr("cx", lon, lat)
            .attr('r', 5);
    }
    // Initialise
    this.init();
};

var ZmgcClient;

jQuery(function() {
  ZmgcClient = new ZmgcClient();
});

I am trying to pass lon/lat data and draw a circle at that point on the map, this code:

this.drawMarker = function (message) {
    var latitude = message.latitude,
        longitude = message.longitude,
        text = message.title,
        city = message.city,
        x, y;

    var mapCoords = this.geoCoordsToMapCoords(latitude, longitude);
        x = mapCoords.x;
        y = mapCoords.y;

    console.log(x,y);
    self.countries.append('svg:circle')
        .attr("r", 40.5)
        .attr("cx", longitude)
        .attr("cy", latitude);
        console.log(latitude, longitude);

The circle is created, but it is not in the correct position.

What am I missing?

Any advice much appreciated.

  • 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-07T11:56:42+00:00Added an answer on June 7, 2026 at 11:56 am

    it must have been late, it should be

        var mapCoords = this.geoCoordsToMapCoords(latitude, longitude);
            x = mapCoords.x;
            y = mapCoords.y;
    
        self.countries.append('svg:circle')
            .attr("r", 5)
            .style("fill", "steelblue")
            .attr("cx", x)
            .attr("cy", y);
    

    and not the latitude, longitude values.

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

Sidebar

Related Questions

I have this horribly stripped delphi code that basically login to server, save cookie
I have this small piece of code that basically takes a list and runs
So i have been creating this framework thing that basically puts together source code
I have this code that I've edited: http://pastebin.com/vrqHek6S I've put in comments where I
I have this code that runs but never stops. class A { public static
I have this code that reads from XML file. It gets five strings (groupId,
I have this code that suppose to place the marker by the latlng public
I have this code that where I would normally use one line: if (tableView
I have this code that works in a unit test but doesn't work when
I have this code that I want to make point-free; (\k t -> chr

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.