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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:57:55+00:00 2026-06-04T01:57:55+00:00

I have to create several markers on Google Maps, in run-time. Their initial position

  • 0

I have to create several markers on Google Maps, in run-time.

Their initial position is randomly defined.

When they are created, how can I change position of some of them? New position is also randomly defined.

I did tried with

marker1.setPosition(pt);

… but, I’m getting error

marker1 is not defined

I guess that problem is that marker1 is not defined in moment when map is created… Something like that.

Can you help me how can I solve this one?

p.s. There is no limit of how many markers will be created.

UPDATE Markers are created with:

function addNewMarker( locationsTotal ) {

if (document.getElementById("lon1").value == '') document.getElementById("lon1").value = '19';
if (document.getElementById("lat1").value == '') document.getElementById("lat1").value = '45';

var parliament = (map.getCenter());

var newMarker = 'marker' + locationsTotal;
newMarker = new google.maps.Marker({
  name:newMarker,
  id:newMarker,
  map:map,
  draggable:true,
  animation: google.maps.Animation.DROP,
  position: parliament, 
  icon: 'img/pin.png'
});

google.maps.event.addListener(newMarker, "dragend", function() {
  var center = newMarker.getPosition();
  var latitude = center.lat();
  var longitude = center.lng();
  var newLon = 'lon' + locationsTotal;
  var newLat = 'lat' + locationsTotal;
  document.getElementById(newLon).value = longitude;
  document.getElementById(newLat).value = latitude;
});

}
  • 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-04T01:57:58+00:00Added an answer on June 4, 2026 at 1:57 am

    As You can see newMarker is visible only in the scope of addNewMarker function.
    What you need is to store your markers in array visible in global scope.
    For example:
    Modify your function:

    var allMarkers = [];
    function addNewMarker( locationsTotal ) {
       //.... skip
      allMarkers.push(newMarker);
    }
    

    All your markers are now stored in an array so you can manipulate them.

    To access marker by name add function:

    function getMarker(name) {
      for (k in allMarkers)
         if (allMarkers[k].name == name) return allMarkers[k];
      return null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create several markers along a route from google maps directions.
I have dynamically created WrapPanel (_wp) with several Borders. And I need create handler
I'd like to create several instances of a class in CodeIgniter. I have created
I have a short loop I used to create several .csv files. The loop
I have a domain name: TestSite.com. I create several subdomains for this site and
I have several tables: CREATE TABLE [dbo].[Tracks]( [Id] [uniqueidentifier] NOT NULL, [Artist_Id] [uniqueidentifier] NOT
I have some code to create a basic dialog box with several options -
We have build several web service based on .net. Now we want to create
I have a data frame with several variables. What I want is create a
I have created several UI elements in the viewDidLoad method. I want to change

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.