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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:41:01+00:00 2026-06-15T05:41:01+00:00

how to draw line between marker and fixed point – Google Maps API v3

  • 0

how to draw line between marker and fixed point – Google Maps API v3

I used that code to draw a line between 2 fixed points
and put a marker
using google api v3

how can i replace the marker position instead of one of the 2 fixed points ?

<!DOCTYPE html>
<html>
<head>
<script
src="http://maps.googleapis.com/maps/api/js?key=AIzaSyCwID2UsBJvwVKEMx_U53brmIC8EOLsBFo&sensor=false">
</script>

<script>


function initialize()
{

var x=new google.maps.LatLng(52.395715,4.888916);


var mapProp = {
  center:x,
  zoom:4,
  mapTypeId:google.maps.MapTypeId.ROADMAP
  };

var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);


 google.maps.event.addListener(map, "mousemove", function(pt) { 
      document.getElementById("latlgn").innerHTML = pt.latLng; 
    });

      var marker = new google.maps.Marker({
  draggable: true,
  position: x,
  map: map,
  title: "Your location"
  });


  google.maps.event.addListener(marker, 'dragend', function (event) {
    document.getElementById("latbox").value = this.getPosition().lat();
    document.getElementById("lngbox").value = this.getPosition().lng();
});

var lat = "80";
var lng = "80"; 
var stavanger=new google.maps.LatLng(lat,lng);
var london=new google.maps.LatLng(51.508742,-0.120850);

var myTrip=[stavanger,london];
var flightPath=new google.maps.Polyline({
  path:myTrip,
  strokeColor:"#0000FF",
  strokeOpacity:0.9,
  strokeWeight:2
  });

flightPath.setMap(map);

google.maps.event.addDomListener(window, 'load', initialize);
}







</script>
</head>
 <div id="latlong">
    <p>Latitude: <input size="20" type="text" id="latbox" name="lat" ></p>
    <p>Longitude: <input size="20" type="text" id="lngbox" name="lng" ></p>
  </div>
<body onLoad="initialize()">

<div id="googleMap" style="width:500px;height:380px;"></div>
<div id="latlgn"></div>
</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-06-15T05:41:02+00:00Added an answer on June 15, 2026 at 5:41 am

    set the myTrip-variable to

    [stavanger,marker.getPosition()]
    

    This will initially draw a line between stavanger and the markers position.

    To keep the line updated when the marker has been moved, use the setPath-method of the PolyLine(flightPath) inside the callback of marker.dragend

      google.maps.event.addListener(marker, 'dragend', function (event) {
        document.getElementById("latbox").value = this.getPosition().lat();
        document.getElementById("lngbox").value = this.getPosition().lng();
        flightPath.setPath([flightPath.getPath().getAt(0),
                            new google.maps.LatLng(this.getPosition().lat(),
                                                   this.getPosition().lng())]);
    });
    

    As requested, modifications to set an end of the line to the center:

    var myTrip=[stavanger,map.getCenter()];
    
    google.maps.event.addListener(map, 'center_changed', function (event) {
        flightPath.setPath([flightPath.getPath().getAt(0),this.getCenter()])
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use the Google maps JavaScript API to draw a line between
I have to draw a line between two points using quartz. At some point
I want to draw a line between two points: https://i.stack.imgur.com/Yuhsd.gif Lets say that I
How to draw a line between two points (Latitude and Longitude) with the Google
I have a program where I want to draw a line between two points.
I want to understand how I would draw a line between two shapes that
I have Latitude and Longitude of two points and Want to Draw line between
How can i draw a line between two particular longitude and latitude points in
I want draw a line between to specify point in java 3d. how can
In flash, I use lineTo to draw a line between two points. This is

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.