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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:48:40+00:00 2026-05-11T07:48:40+00:00

My javascript is very weak. Is it possible to modify the same code below

  • 0

My javascript is very weak. Is it possible to modify the same code below to do the following:

  • Make the objects loaded draggable
  • When the object is dropped, an ajax request to something like:

    http://…/moveTo?lat=new_lat&long=new_long&id=some_way_to_uniquely_id_the_object

Any advice to offer?

Sample Code:

var ge;  // store the object loaded for the given file... initially none of the objects // are loaded, so initialize these to null var currentKmlObjects = {   'red': null,   'yellow': null,   'green': null };  google.load('earth', '1');  function init() {   // Create checkboxes   var content = document.getElementById('content');   var inputHTML = 'Placemarks:<br/>';   inputHTML += '<input type='checkbox' id='kml-red-check' onclick='toggleKml(\'red\');'/>' +                '<label for='kml-red-check'>Red</label>' +                '<input type='checkbox' id='kml-yellow-check' onclick='toggleKml(\'yellow\');'/>' +                '<label for='kml-yellow-check'>Yellow</label>' +                '<input type='checkbox' id='kml-green-check' onclick='toggleKml(\'green\');'/>' +                '<label for='kml-green-check'>Green</label>';   content.innerHTML = inputHTML;    google.earth.createInstance('content', initCB, failureCB); }  function initCB(instance) {   ge = instance;   ge.getWindow().setVisibility(true);     // add a navigation control   ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);    // add some layers   ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);   ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);    // fly to Santa Cruz   var la = ge.createLookAt('');   la.set(37, -122,           0, // altitude           ge.ALTITUDE_RELATIVE_TO_GROUND,           0, // heading           0, // straight-down tilt           5000 // range (inverse of zoom)   );   ge.getView().setAbstractView(la);    // if the page loaded with checkboxes checked, load the appropriate   // KML files   if (document.getElementById('kml-red-check').checked)     loadKml('red');    if (document.getElementById('kml-yellow-check').checked)     loadKml('yellow');    if (document.getElementById('kml-green-check').checked)     loadKml('green');    document.getElementById('installed-plugin-version').innerHTML =       ge.getPluginVersion().toString(); }  function failureCB(errorCode) { }  function toggleKml(file) {   // remove the old KML object if it exists   if (currentKmlObjects[file]) {     ge.getFeatures().removeChild(currentKmlObjects[file]);     currentKmlObject = null;   }    // if the checkbox is checked, fetch the KML and show it on Earth   var kmlCheckbox = document.getElementById('kml-' + file + '-check');   if (kmlCheckbox.checked)     loadKml(file); }  function loadKml(file) {   var kmlUrl = 'http://earth-api-samples.googlecode.com/svn/trunk/' +                'examples/static/' + file + '.kml';    // fetch the KML   google.earth.fetchKml(ge, kmlUrl, function(kmlObject) {     // NOTE: we still have access to the 'file' variable (via JS closures)      if (kmlObject) {       // show it on Earth       currentKmlObjects[file] = kmlObject;       ge.getFeatures().appendChild(kmlObject);     } else {       // bad KML       currentKmlObjects[file] = null;       alert('Bad KML');        // uncheck the box       document.getElementById('kml-' + file + '-check').checked = '';     }   }); }  google.setOnLoadCallback(init); 

For bonus points, can I get the kml to be reloaded afterwards?

  • 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. 2026-05-11T07:48:41+00:00Added an answer on May 11, 2026 at 7:48 am

    To make the objects draggable you need to set up some event listeners and handle the movement in the callback function. I presume the objects you wish to drag are place-marks (KmlPlacemark) if so you need something like this…(NB: This is untested and written here so there could be some typos.)

    var dragging = false; // the object being dragged  var url = 'http://.../moveTo?'; // path to your cgi script  function init() {  // Rest of your method body...  google.earth.addEventListener(ge.getGlobe(), 'mousedown', function(e)    {      // if it is a place mark     if(e.getTarget().getType() == 'KmlPlacemark')      {        // set it as the dragging target       dragging = e.getTarget();       }    });    google.earth.addEventListener(ge.getGlobe(), 'mouseup', function(e)   {      // drop on mouse up (if we have a target)      if(dragging) {        // build the query string        // ...you could use getName or getSnippet rather than getId        var query = 'lat=' + dragging.getGeometry().getLatitude() +        '&long=' + dragging.getGeometry().getLongitude() +         '&id=' + dragging.getId();         // send the query to the url        httpPost(url, query);         // clear the dragging target        dragging = false;       }   });    google.earth.addEventListener(ge.getGlobe(), 'mousemove', function(e)    {      // when the mouse moves (if we have a dragging target)     if(dragging) {        // stop any balloon opening       e.preventDefault();        // drag the object       // i.e. set the placemark location to cursor the location       dragging.getGeometry().setLatLng(e.getLatitude(), e.getLongitude());     }    });   }  // send a HTTP POST request // could use jQuery, etc.... function httpPost(url, query) {     var httpReq = false;     var self = this;     // Mozilla/Safari     if (window.XMLHttpRequest) {         self.httpReq = new XMLHttpRequest();     }     // IE     else if (window.ActiveXObject) {         self.httpReq = new ActiveXObject('Microsoft.XMLHTTP');     }     self.httpReq .open('POST', url, true);     self.httpReq .setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');     self.httpReq .onreadystatechange = function() {         if (self.httpReq .readyState == 4) {             // do something...             alert(self.httpReq.responseText);          }     }     self.httpReq.send(query); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 214k
  • Answers 214k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you take a look at the documentation at http://fancybox.net/api… May 12, 2026 at 10:43 pm
  • Editorial Team
    Editorial Team added an answer It works fine but is it the "right" approach and… May 12, 2026 at 10:43 pm
  • Editorial Team
    Editorial Team added an answer According to the is there a Ice Maven Integration thread… May 12, 2026 at 10:43 pm

Related Questions

If you don't want any context or an example of why I need this,
My JavaScript is pretty nominal, so when I saw this construction, I was kind
So I have been exploring different methods to clean up and test my JavaScript.
Short version: What is the cleanest and most maintainable technique for consistant presentation and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.