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 7966251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:22:34+00:00 2026-06-04T06:22:34+00:00

First I’m pretty new to Javascript, so sorry if my question comes across poorly.

  • 0

First I’m pretty new to Javascript, so sorry if my question comes across poorly.

I’m creating an application in Flash to help users calculate their electrical costs. Then I’m taking this figure and write it to an xml file.

Now I’m looking to open a webpage and show a google map, and there is a rectangle drawn over the map which is generated dynamically from the number generated earlier and stored in the xml file.

I’m completely lost as to places to turn on how to achieve this. I’ve gotten my map on to my page, and it scales 100% as I want it to, but I can’t figure out the dynamic rectangle part at all. Any ideas or pointers in the right direction greatly 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-04T06:22:35+00:00Added an answer on June 4, 2026 at 6:22 am

    In this latest version, the XML file

    <countries>
      <country name="USA" lat="40.0" lng="-100.0" width="30.0"/>
      <country name="France" lat="46.6" lng="2.7" width="10"/>
      <country name="Germany" lat="51.1" lng="10.1" width="20"/>
    </countries>
    

    is loaded as soon as the map tiles finish loading. I could not get the getProjection to be called correctly if I did not wait for tile loading to finish. The docs state that getting the projection needs the map to be initialized, and recommends listening for projection_changed. Both ways work yet I still feel listening to tiles_loaded is safer and if something goes wrong with the xml loading it will get called again if the map is zoomed or panned a noticeable amount.

      var map;
      var xmlLoaded = false;
    
      function initialize() {
        var mapOptions = { center: new google.maps.LatLng(30.0, 0.0), zoom: 2,
          mapTypeId: google.maps.MapTypeId.ROADMAP };
    
        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
        google.maps.event.addListener(map, 'tilesloaded', loadData);
      }
    
      function loadData() {
        if(!xmlLoaded) {
         $.ajax({
          type: "GET",
          url: "co2data.xml",
          dataType: "xml",
          success: function(xml) {
            var countries = xml.documentElement.getElementsByTagName("country");
            for(var i = 0, country; country = countries[i]; i++) {
              var name = country.getAttribute("name");
    
              var lat = parseFloat(country.getAttribute("lat"));
              var lng = parseFloat(country.getAttribute("lng"));
              var point = map.getProjection().fromLatLngToPoint(new google.maps.LatLng(lat,lng));
    
              // width is really an arbitrary unit, relative to CO2 tonnage.
              // equals the side of the drawn square.
              // it is measured in google maps points units.
              var width = parseFloat(country.getAttribute("width"));
    
              makeCO2Rect(name, point, width);
            }
            xmlLoaded = true;
          }
         });
        }
      }
    

    The rectangle is defined by width in points (the whole world is 256×256 points), so some conversion is needed when assigning their centers to the more conventional LatLng.

      function rectParamsToBounds(point, width) {
        var ctrX = point.x;
        var ctrY = point.y;
    
        var swX = ctrX - (width/2);
        var swY = ctrY - (width/2);
    
        var neX = ctrX + (width/2);
        var neY = ctrY + (width/2);
        return new google.maps.LatLngBounds(
            map.getProjection().fromPointToLatLng(new google.maps.Point(swX, swY)),
            map.getProjection().fromPointToLatLng(new google.maps.Point(neX, neY)));
      }
    

    Finally, a rectangle is created with a country name that goes into a MarkerWithLabel (using v1.1.5 here, you can hotlink to http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerwithlabel/1.1.5/src/markerwithlabel_packed.js though I prefer saving a local copy)

    Since dragging a rectangle appears impossible, a marker in its center works as a handle. When it’s dragged, the associated rectangle moves with it.

      function makeCO2Rect(name, point, width) {
        var rect = new google.maps.Rectangle({
          map: map,
          bounds: rectParamsToBounds(point, width)
        });
    
        var marker = new MarkerWithLabel({
          map: map,
          position: map.getProjection().fromPointToLatLng(new google.maps.Point(point.x, point.y)),
          draggable: true,
          raiseOnDrag: false,
          labelContent: name,
          labelAnchor: new google.maps.Point(30, 0),
          labelClass: "labels", // the CSS class for the label
          labelStyle: {opacity: 1.0}
        });
    
        google.maps.event.addListener(marker, 'drag', function(event) {
          var newLatLng = event.latLng;
          var newPoint = map.getProjection().fromLatLngToPoint(newLatLng);
          rect.setBounds(rectParamsToBounds(newPoint, width));
        });
      }
    
      google.maps.event.addDomListener(window, 'load', initialize);
    

    Styling the labels need to be done both in the .labels CSS class and the constructor, and rectangles have options like stroke color, thickness, opacity, and fill color.

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

Sidebar

Related Questions

First let me preface this question by saying that I'm fairly new to Javascript.
first of all, sorry if that question is dumb but I´m a total newbie
First of all, I am sorry if this question doesn't belong to SO since
First off, I'm pretty new so I hope I hadn't missed anything too trivial.
First i have declare that i am new to javascript. I have created a
First of all, I'm really new to the MVC Asp.Net ideology. I would like
First some background: I'm working on an application and I'm trying to follow MVVM
First I will ask users How many names you want to enter?. Once they
first question asked, so I'll get straight to it. I've got some C code
First let me phrase the proper question: Q: There is a file containing more

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.