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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:20:29+00:00 2026-05-12T12:20:29+00:00

After some advice on how to clear markers on googlemaps, I have a map

  • 0

After some advice on how to clear markers on googlemaps, I have a map which I would like to have only one marker showing (basically the last marker clicked). I would like the user to be able to change thier mind and click multiple times, but not have confusing map of previous clicks etc.

I have tried the map.clearOverlay(); function, but it seems to permantly clear the whole map.

function initialize() {

    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("googlemap"));

        map.setCenter(new GLatLng(50.401515,-4.866943), 8);

        GEvent.addListener(map,"click", function(overlay,latlng) {     
            if (latlng) {   
                var myHtml = "" + latlng ;
                split=myHtml.indexOf(",");       
                x=Math.round(myHtml.slice(1,split)*1000000)/1000000;
                y=Math.round(myHtml.slice(split+1,myHtml.length-1)*1000000)/1000000;
                document.collector.latitude.value=x;
                document.collector.longitude.value=y;
                lat="<br />Latitude: " + x;
                lon="<br />Longitude: " + y;
                latlon=""+lat+lon;

                //map.openInfoWindow(latlng, latlon);
                map.clearOverlay();
                map.addOverlay(new GMarker(latlng));
            }
        });
        map.addControl(new GLargeMapControl3D());
        map.addControl(new GMapTypeControl());
    }
}
  • 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-05-12T12:20:30+00:00Added an answer on May 12, 2026 at 12:20 pm

    Untested, but should do what you want – note that the setLatLng method of GMarker was introduced in API version 2.88:

    function initialize() {
    
        if (GBrowserIsCompatible()) {
            var marker;
    
            function showNewMarker(latlng) {
                marker = new GMarker(latlng);
                map.addOverlay(marker);
                showMarker = updateExistingMarker;
            }
    
            function updateExistingMarker(latlng) {
                marker.setLatLng(latlng);
            }
    
            var showMarker = showNewMarker;
    
            var map = new GMap2(document.getElementById("googlemap"));
    
            map.setCenter(new GLatLng(50.401515,-4.866943), 8);
    
            GEvent.addListener(map,"click", function(overlay,latlng) {     
                if (latlng) {   
                    var myHtml = "" + latlng ;
                    split=myHtml.indexOf(",");       
                    x=Math.round(myHtml.slice(1,split)*1000000)/1000000;
                    y=Math.round(myHtml.slice(split+1,myHtml.length-1)*1000000)/1000000;
                    document.collector.latitude.value=x;
                    document.collector.longitude.value=y;
                    lat="<br />Latitude: " + x;
                    lon="<br />Longitude: " + y;
                    latlon=""+lat+lon;
    
                    //map.openInfoWindow(latlng, latlon);
                    map.clearOverlay();
                    showMarker(latlng);
                }
            });
            map.addControl(new GLargeMapControl3D());
            map.addControl(new GMapTypeControl());
        }
    }
    

    It works by using a variable, showMarker, containing a reference to a function. This starts out pointing to a function, showNewMarker, which creates a marker, adds it to the map, and then changes showMarker to point to the function updateExistingMarker, which simply uses setLatLng to move the marker to a new position. This means that, on subsequent clicks, the existing marker will be moved to the location of the click.

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

Sidebar

Related Questions

After some advice. I have a client wishing to have an app which lets
Just after some advice regarding this database design situation. So I have two tables
after some advice regarding a problem i am getting using a linux based piece
Summary I am after some advice on the easiest way to analyze simple data
I'm after some design advice. I'm working on an application with a fellow developer.
I'm a newbie to Unit Testing and I'm after some best practice advice. I'm
Basically I'm after advice on producing dynamic PDFs with charts, images and some styling
After some recursive function I have an array: first iteration: Array ( [category_id] =>
I have a script which allows me to lookup for a hostname after inputting
I'm after some advice on the most efficient approach to stepping through a list

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.