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

The Archive Base Latest Questions

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

I’m working on a project using Google Maps v3 that will allow users to

  • 0

I’m working on a project using Google Maps v3 that will allow users to drag and drop randomly placed markers (quantity, coordinates, and labels generated with php).

I would like the latitude and longitude of moved markers to update html input fields on the page.
Unfortunately, I do not know enough about js to give each marker a unique identity in an efficient way by using arrays and/or ‘variable variables’.

Here is my code thus far:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
  html { height: 100% }
  body { height: 100%; margin: 0px; padding: 0px }
  #map_canvas { height: 100% }
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
function initialize() 
{
    var latlng = new google.maps.LatLng(39.3939,-119.861);
    var myOptions = 
    {
      zoom: 17,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.SATELLITE
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);

    // Set Border Coords
    var BorderCoords = 
    [
        new google.maps.LatLng(39.3952726888,-119.85922848),
        new google.maps.LatLng(39.3925273112,-119.85922848),
        new google.maps.LatLng(39.3925273112,-119.86277152),
        new google.maps.LatLng(39.3952726888,-119.86277152)   
    ];      

    // Define Border
    Border = new google.maps.Polygon
    ({
        map: map,
        paths: BorderCoords,
        strokeColor: "#FF0000",
        strokeOpacity: 0.8,
        strokeWeight: 2
    });

    // Set marker coords
    var MarkerCoords =
    [
        [39.3930761,-119.8612324],
        [39.3947606,-119.8617452],
        [39.3948516,-119.8619689],
        [39.3929412,-119.859542],
        [39.3947902,-119.8601571],
        [39.3940501,-119.8593369]
    ];

    // Set marker properties
    var MarkerProp = 
    [
        ["Red","A"],
        ["Red","B"],
        ["Red","C"],
        ["Blue","A"],
        ["Blue","B"],
        ["Blue","C"]
    ];

    // Define marker coords
    for (var i = 0; i < MarkerCoords.length; i++)
    {
        var markers = new google.maps.Marker
        ({
            map: map,
            position: new google.maps.LatLng(MarkerCoords[i][0],MarkerCoords[i][1]),
            clickable: true,
            draggable: true,
            icon: 'img/icons/'+MarkerProp[i][0]+'/letter_'+MarkerProp[i][1]+'.png'
        });

        google.maps.event.addListener(markers, "dragend", function() 
        {
            //These variables will not work
            //var latstr = eval('lat_'+MarkerProp[i][0]+'_'+MarkerProp[i][1]);
            //var lngstr = eval('lng_'+MarkerProp[i][0]+'_'+MarkerProp[i][1]);
            //var lat = document.getElementById(latstr);
            //var lng = document.getElementById(lngstr);

            //these lat/lng variables work for only one set of input fields
            var lat = document.getElementById('lat_Blue_C');
            var lng = document.getElementById('lng_Blue_C');
            var coords = this.getPosition()
            lat.value = coords.lat();
            lng.value = coords.lng();
        });
    }
}
</script>

</head>
<body onload="initialize()">
<div id="map_canvas" style="width:700px; height:400px"></div>
<br/>
<input id="lat_Red_A" type="text">
<input id="lng_Red_A" type="text">
<br/>
<input id="lat_Red_B" type="text">
<input id="lng_Red_B" type="text">
<br/>
<input id="lat_Red_C" type="text">
<input id="lng_Red_C" type="text">
<br/>
<input id="lat_Blue_A" type="text">
<input id="lng_Blue_A" type="text">
<br/>
<input id="lat_Blue_B" type="text">
<input id="lng_Blue_B" type="text">
<br/>
<input id="lat_Blue_C" type="text">
<input id="lng_Blue_C" type="text">
<br/>
</body>

</html>
  • 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-23T12:32:21+00:00Added an answer on May 23, 2026 at 12:32 pm

    Here is the answer. Define a custom id based on your marker properties and based on that access yout input field ids:

    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0px; padding: 0px }
      #map_canvas { height: 100% }
    </style>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
    <script type="text/javascript">
    function initialize() 
    {
        var latlng = new google.maps.LatLng(39.3939,-119.861);
        var myOptions = 
        {
          zoom: 17,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.SATELLITE
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"),
            myOptions);
    
        // Set Border Coords
        var BorderCoords = 
        [
            new google.maps.LatLng(39.3952726888,-119.85922848),
            new google.maps.LatLng(39.3925273112,-119.85922848),
            new google.maps.LatLng(39.3925273112,-119.86277152),
            new google.maps.LatLng(39.3952726888,-119.86277152)   
        ];      
    
        // Define Border
        Border = new google.maps.Polygon
        ({
            map: map,
            paths: BorderCoords,
            strokeColor: "#FF0000",
            strokeOpacity: 0.8,
            strokeWeight: 2
        });
    
        // Set marker coords
        var MarkerCoords =
        [
            [39.3930761,-119.8612324],
            [39.3947606,-119.8617452],
            [39.3948516,-119.8619689],
            [39.3929412,-119.859542],
            [39.3947902,-119.8601571],
            [39.3940501,-119.8593369]
        ];
    
        // Set marker properties
        var MarkerProp = 
        [
            ["Red","A",],
            ["Red","B"],
            ["Red","C"],
            ["Blue","A"],
            ["Blue","B"],
            ["Blue","C"]
        ];
    
        // Define marker coords
        for (var i = 0; i < MarkerCoords.length; i++)
        {
            var markers = new google.maps.Marker
            ({
                map: map,
                position: new google.maps.LatLng(MarkerCoords[i][0],MarkerCoords[i][1]),
                clickable: true,
                draggable: true,
    
                //define a custom id based on marker properties
    
                my_id: MarkerProp[i][0] +"_"+MarkerProp[i][1],
                icon: 'img/icons/'+MarkerProp[i][0]+'/letter_'+MarkerProp[i][1]+'.png'
            });
    
            google.maps.event.addListener(markers, "dragend", function() 
            {
                //These variables will not work
                //var latstr = eval('lat_'+MarkerProp[i][0]+'_'+MarkerProp[i][1]);
                //var lngstr = eval('lng_'+MarkerProp[i][0]+'_'+MarkerProp[i][1]);
                //var lat = document.getElementById(latstr);
                //var lng = document.getElementById(lngstr);
    
    //get the id of the marker
    var marker_id = this.my_id;
    
    
    
                //match the fields to update
                var lat = document.getElementById('lat_' + marker_id);
                var lng = document.getElementById('lng_' + marker_id);
                var coords = this.getPosition()
                lat.value = coords.lat();
                lng.value = coords.lng();
            });
        }
    }
    </script>
    
    </head>
    <body onload="initialize()">
    <div id="map_canvas" style="width:700px; height:400px"></div>
    <br/>
    <input id="lat_Red_A" type="text">
    <input id="lng_Red_A" type="text">
    <br/>
    <input id="lat_Red_B" type="text">
    <input id="lng_Red_B" type="text">
    <br/>
    <input id="lat_Red_C" type="text">
    <input id="lng_Red_C" type="text">
    <br/>
    <input id="lat_Blue_A" type="text">
    <input id="lng_Blue_A" type="text">
    <br/>
    <input id="lat_Blue_B" type="text">
    <input id="lng_Blue_B" type="text">
    <br/>
    <input id="lat_Blue_C" type="text">
    <input id="lng_Blue_C" type="text">
    <br/>
    </body>
    
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am using Paperclip to handle profile photo uploads in my app. They upload
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.