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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:08:37+00:00 2026-05-13T21:08:37+00:00

I have the following code which lets the user plot two points on a

  • 0

I have the following code which lets the user plot two points on a Google MAP. I then want to be able to catch the event for each point(marker) being dragged to a new location. I am bad at Javascript and have spent hours trying to do this so I think it’s time I get some help..

What I am trying to do is get the user to plot two points(markers) draggable on the map. I then want to be able to have my script write the positions(lat,long) to the document. I will then calculate the distance between these as part of a shipping app I am making.

I would like to have the contents of the document (lat,long) updated when a marker(point) is dragged to a new location.

Also, I fixed a schoolboy error where the point vars were being decalred inside the switch statement. My problem is fixed by moving the Add event listener statements inside the switch statement. Thanks Cannonade 🙂

The next thing now is to try and calculate the distance (crow flies) between the two points

Again, thanks for you help.. appreciated as always!!

Updated Code that works:

        var map = null;
    var geocoder = null;
    var zoom = 15;
    var first_point = false;
    var boundary =  new Array();
    var cCount = 0;


    var point1;
    var point2;

    function initialize() {
        if (GBrowserIsCompatible()) {
            first_point = false;
            map = new GMap2(document.getElementById("map_canvas"));
            var center = new GLatLng(37.4419, -122.1419);
            map.setCenter(center, zoom);

            GEvent.addListener(map, "click", function(overlay,point)
            {
                if (overlay != null)
                {}
                else
                {
                    var n = boundary.length;

                    switch (cCount)
                    {
                        case 0:
                            point1 = new GMarker(point,{draggable: true});
                            map.addOverlay(point1);
                            cCount++;
                            GEvent.addListener(point1, "dragend", function()
                                {
                                    alert('P1 Dragged');
                                });
                            break;
                        case 1:
                            point2 = new GMarker(point,{draggable: true});
                            map.addOverlay(point2);
                            cCount++;
                            GEvent.addListener(point2, "dragend", function()
                                {
                                    alert('P2 Dragged');
                                });
                            break;
                        case 2:
                            map.clearOverlays();
                            cCount=0;
                            break;
                    }
                }
            });
            map.addControl(new GSmallMapControl());
            geocoder = new GClientGeocoder();
        }
    }
  • 1 1 Answer
  • 3 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-05-13T21:08:37+00:00Added an answer on May 13, 2026 at 9:08 pm

    I have taken your code and made the following fixes:

    • Fixed the unbalanced brackets I mentioned in the comment.
    • Moved the two addListener calls into the switch statement so that the point1 and point2 variables are still in scope when you attach the events.

    You can check out the example here (source).

    Edit:

    Here is some Javascript code to get the linear distance between two points (in meters):

    /* Convert degress to radians */
    function deg2rad(deg) {
      return deg / (180 / Math.PI);
    }
    
    /* Calculate distance between two points */
    function point_distance(a, b) {
      var r = 6378700;
      var lat1 = a.y;
      var lat2 = b.y;
      var lon1 = a.x;
      var lon2 = b.x;
      var dist = r * Math.acos(Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) +
                   Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
                   Math.cos(deg2rad(lon1 - lon2)));
      return dist;
    }
    

    This is based on the approximate radius of the earth being 6378700m.

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

Sidebar

Related Questions

I want to know is below code correct ? I have following code which
I have the following code which will generate two pdf files containing plots to
I have following code which works for radio buttons but need to be changed
I have the following code which definitely returns a proper data result if I
I have the following code which is working, I was wondering if this can
I have the following code which is used to upload large files (~6MB) to
i have the following code which switches some fullscreen-background-images (fadeOut, fadeIn). setInterval(function() { var
I have the following code which is fine if I give invalid parameters (though,
I have the following code which utilises Guava's Files.readLines() method: List<String> strings = Lists.newArrayList();
I have an edit script which retrieves calendar events and lets the user edit

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.