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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:10:41+00:00 2026-06-04T10:10:41+00:00

How to each time when click Create New Poly button, store last object in

  • 0

How to each time when click “Create New Poly” button, store last object in array and create a new clean object to draw new separated polyline on the map. I’d like to keep the functionality of the old polylines. Now it is not possible to clean the object. Simplified example presented bellow.

HTML:

<button onclick="create()">Create New Poly</button>
<div id="map" style="width: 500px; height: 400px;"></div>

JS:

var map;
var listener;

var polys = [],
    poly = {};

create = function() {
    poly = new Poly();

    if ( !! listener) google.maps.event.removeListener(listener);
    listener = google.maps.event.addListener(map, 'click', function(e) {
        poly.addPoint(e.latLng);
    });
}

function Poly() {
    this.markers = [];
    this.setMap(map);
    polys.push(this);
}
Poly.prototype = new google.maps.Polyline();

Poly.prototype.addPoint = function(p) {
    var m = poly.createMarker(p);
    poly.markers.push(m);
    poly.getPath().push(p);
}

Poly.prototype.createMarker = function(p) {
    var marker = new google.maps.Marker({
        position: p
    });
    marker.setMap(this.getMap());
    return marker;
}

$(function() {
    map = new google.maps.Map(document.getElementById('map'), {
        center: new google.maps.LatLng(48.864715, 10.546875),
        zoom: 4,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });
});

Demo: JSFIDDLE

  • 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-04T10:10:42+00:00Added an answer on June 4, 2026 at 10:10 am

    Even after doing what Ben Davis suggested http://jsfiddle.net/LxGmR/ it still exhibits the problem.

    Which I think is due to all the Ploy objects sharing the same prototype: Poly.prototype = new google.maps.Polyline();. I think the fix is that each new Ploy object needs its own prototype google.maps.Polyline instance.

    Which after testing, I have found to be true: http://jsfiddle.net/uhZFE/

    Then I looked up how to do this without the wrapper function, I used the method described in the SO answer https://stackoverflow.com/a/6519265/388787 (http://javascript.crockford.com/prototypal.html was also helpful) and produced http://jsfiddle.net/YgSwF/ which is the following; it works as you requested:

    var map;
    var listener;
    
    var polys = [];
    
    create = function () {
      var poly = new Poly();
    
      if ( !! listener) google.maps.event.removeListener(listener);
      listener = google.maps.event.addListener(map, 'click', function (e) {
        poly.addPoint(e.latLng);
      });
      polys.push(poly);
    }
    
    function Poly() {
      google.maps.Polyline.call(this);
      this.markers = [];
      this.setMap(map);
    }
    Poly.prototype = Object.create(google.maps.Polyline.prototype);
    
    Poly.prototype.addPoint = function (p) {
      var m = this.createMarker(p);
      this.markers.push(m);
      this.getPath().push(p);
    }
    
    Poly.prototype.createMarker = function (p) {
      var marker = new google.maps.Marker({
        position: p
      });
      marker.setMap(this.getMap());
      return marker;
    }
    
    $(function () {
      map = new google.maps.Map(document.getElementById('map'), {
        center: new google.maps.LatLng(48.864715, 10.546875),
        zoom: 4,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      });
    });​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just trying to run a new thread each time a button click even
Each time I click on the map a new marker is placed on the
Each time I click the Run as button, I get the following message. The
To create Button and its click event in run time I use: Button b
Each time I make changes to XML file and press run button, It generates
The webViewDidFinishLoad message seems to be sent each time any object in the page
i have this crazy label for each time i create a migration that use
I developed one swing application but each time you run application new window is
I have a directory tree structure. Each time I click a folder, jQuery.ajax fires
Each time my webpage is loaded it runs a routine in the page_load event

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.