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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:16:32+00:00 2026-06-05T03:16:32+00:00

I have this function that after a click on a marker draws a line

  • 0

I have this function that after a click on a marker draws a line between points that are related to an item.

function showDetails(itemId)
{   
    var newlatlng = itemId.position;
    var xmlhttp;

    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET", "index.php?r=items/ajaxdetails&id="+itemId.indice, 
                 false);
    xmlhttp.send();




    var checkins = JSON.parse(xmlhttp.responseText);
    var numeroCheckins = checkins.length;

    var polylineCheckins = [];

    var bounds = new google.maps.LatLngBounds();

    for (counter = 0; counter< numeroCheckins; counter++)
    {
        var posizione = new google.maps.LatLng(checkins[counter].lat,
                                               checkins[counter].long);
        polylineCheckins[counter] = posizione;
        bounds.extend(posizione);
    }

    var polyline = new google.maps.Polyline({
        path: polylineCheckins,
        strokeColor: "#FF0000",
        strokeOpacity: 0.5,
        strokeWeight: 5
        });

    polyline.setMap(map);

    map.fitBounds(bounds);
}

Everything is working, but if a call several time this function the previous line is always showed. I tried to use the method setMap(null) but without success, to try to reset the polyline.

I would like to achieve te result of deleting the previous polyline, before drawing a new one.

Thanks for your support

  • 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-05T03:16:35+00:00Added an answer on June 5, 2026 at 3:16 am

    The easiest way to keep only one Polyline for showDetails on the map is to make a global Polyline variable. That way, each time showDetails is called the global variable is modified.

    Right now, each time showDetails runs a new Polyline is created and no reference to it is returned, so I don’t see a way to set the previous line’s map to null.

      // GLOBAL 
      var detailsPolyline = new google.maps.Polyline({
        strokeColor: "#FF0000",
        strokeOpacity: 0.5,
        strokeWeight: 5
      });
    

    inside showDetails:

    detailsPolyline.setPath(polylineCheckins);
    detailsPolyline.setMap(map);
    
    map.fitBounds(bounds);
    

    Here’s the whole test case I used, because I don’t have the php file I created my own objects

      var map;
      var mapOptions = { center: new google.maps.LatLng(0.0, 0.0), zoom: 2,
        mapTypeId: google.maps.MapTypeId.ROADMAP };
    
      function initialize() {
        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
        showDetails([ {lat: 20, long: 0},
                      {lat: 20, long: 10},
                      {lat: 30, long: 20}]);
    
        showDetails([ {lat: 10, long: 0},
                      {lat: 10, long: 10},
                      {lat: 20, long: 20}]);
      }
    
      var detailsPolyline = new google.maps.Polyline({
        strokeColor: "#FF0000",
        strokeOpacity: 0.5,
        strokeWeight: 5
      });
    
    function showDetails(checkins)
    {   
        var numeroCheckins = checkins.length;
        var polylineCheckins = [];
        var bounds = new google.maps.LatLngBounds();
    
        for (counter = 0; counter< numeroCheckins; counter++)
            {
            var posizione = new google.maps.LatLng(checkins[counter].lat, checkins[counter].long);
            polylineCheckins[counter] = posizione;
            bounds.extend(posizione);
            }
    
        detailsPolyline.setPath(polylineCheckins);
        detailsPolyline.setMap(map);
    
        map.fitBounds(bounds);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function that is storing the contents of an array in three
I have this function that I wrote that is supposed to display notifications: function
I have this function that prints the name of all the files in a
I have this function that converts all special chars to uppercase: function uc_latin1($str) {
I have this function that takes the input of a, runs a calculation and
I have this function that switches the HTML contents from one element on a
So I have this function that forks N number of child processes. However it
I have this php function that has to perform some processing on a given
I have this function called ProperCase that takes a string, then converts the first
I have this function in jQuery that gets data from a page with POST,

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.