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

  • Home
  • SEARCH
  • 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 9042567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:31:38+00:00 2026-06-16T10:31:38+00:00

I try to draw a line in google maps, i try this code and

  • 0

I try to draw a line in google maps, i try this code and nothing happen only the marker are there not the line, i already read some reference like https://developers.google.com/maps/documentation/javascript/v2/overlays#Icons_overview
and try the code but the line still doesn’t come out, anyone can help me?
below are my code, assume address are array

for (var i = 0; i < address.length; i++) {
    geocoder.geocode({ 'address': address[i] }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {

            if (i == 2) {
                var pos1 = results[0].geometry.location;
                alert(pos1);
            }

            else if (i == 2) {
                var pos2 = results[0].geometry.location;
                alert(pos2);
            }

            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker
            (
                {
                    position: results[0].geometry.location,
                    map: map,
                    title: 'click me'
                }
            );
            var infowindow = new google.maps.InfoWindow
            (
                {
                    content: '<img src="http://wallpaperscraft.com/image/child_girl_emotion_sweet_face_25641_100x100.jpg"> Welcome to origineit'
                }
            );
            var flightPlanCoordinates = [
                new google.maps.LatLng(pos1),
                new google.maps.LatLng(pos2)
              ];

            if (i == 2) {

                var polyline = new google.maps.Polyline
                ({
                    path: flightPlanCoordinates,
                    strokeColor: '#FF0000',
                    strokeOpacity: 0.8,
                    strokeWeight: 3
                });
                polyline.setMap(map);
            }

            google.maps.event.addListener(marker, 'click', function () {
                // Calling the open method of the infoWindow 
                infowindow.open(map, marker);
            });
        }
        else {
            alert('Geocode was not successful for the following reason: ' + status);
        }
    });
}
  • 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-16T10:31:39+00:00Added an answer on June 16, 2026 at 10:31 am

    The problem here is that you are using Google Maps V3 (which you should) for everything except the polyline which is from V2. You should read up on the API documentation for Google Maps V3 instead.

    For Google Maps V3, it should look something like this:

    Edit

    When testing out your code (instead of just writing an answer from the top of my head) I also noticed that you hade some scoping problems (which you’ve failed to workaround checking if(i == 2) in your updated code, I would suggest this article for you to learn more about the problem) so I changed that as well. The code below is tested to work (http://jsfiddle.net/YMyc9/) but it really should be refactored a bit to make it easier to follow.

    var pos1 = null; // These need to be declared outside of the geocode-callback
    var pos2 = null; // so we also remove 'var' from the assignment inside of that function
    for (var i = 0; i < address.length; i++) {
        geocoder.geocode({'address': address[i]}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
    
                // We can't use 'i' here since this happens in a callback and the loop will already
                // have progressed and 'i' is (with my two test addresses) always 2 here.
                if (pos1 == null) {
                    pos1 = results[0].geometry.location;
                }
                else if (pos2 == null) {
                    pos2 = results[0].geometry.location;
                }
                else {
                    /* We already got two so maybe quit? */
                    return;
                }
    
                map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                    position: results[0].geometry.location,
                    map: map,
                    title: 'click me'
                });
                var infowindow = new google.maps.InfoWindow({
                    content: '<img src="http://wallpaperscraft.com/image/child_girl_emotion_sweet_face_25641_100x100.jpg"> Welcome to origineit'
                });
    
                /* NEW CODE HERE */
                if (pos2 != null) {
                    var polyline = new google.maps.Polygon({
                        paths: [pos1, pos2],
                        strokeColor: '#FF0000',
                        strokeOpacity: 0.8,
                        strokeWeight: 3,
                        fillColor: '#FF0000',
                        fillOpacity: 0.35
                    });
    
                    polyline.setMap(map);
                } /* BACK TO THE OLD CODE HERE */
    
                google.maps.event.addListener(marker, 'click', function() {
                    // Calling the open method of the infoWindow 
                    infowindow.open(map, marker);
                });
            }
            else {
                alert('Geocode was not successful for the following reason: ' + status);
            }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to draw some lines with different colors. This code tries to draw
I already got my DrawView, but can only draw 1 line at one time.
I am trying to draw paths on google maps using android i found this
I'am trying to draw a line above mapkit. If I try to set the
Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]
Try this piece of code - public class WhitespaceTest { public static void main(String[]
I try to draw a rotated line and after rotating to draw a point
I try to draw a line with the width depending on the density. The
I have a problem with this... When I draw a line the character follows
I am trying to draw a single line with the code below and it

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.