How to show multiple bookmarks in Google Map API v3?

Asked

Viewed 2,673 times

5

I cannot mark several points on the map. They could check if there is something wrong with the code?

var map;
var idInfoBoxAberto;
var infoBox = [];
var marker ;
var geocoder;

function initialize() {
    var latlng = new google.maps.LatLng(latitudes, longitudes);

    var options = {
        zoom: 14,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("mapa"), options);
    geocoder = new google.maps.Geocoder();

    for (var i = 0; i < coord.length; i++) {
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(coord[i]),
            map: map,
        });
    }
}

initialize();

1 answer

2


Being his array coord one string, the section you set the position of each marker is incorrect, since the class constructor google.maps.LatLng there must be at least two arguments, which is latitude and longitude (in this order), as defined in the variable latlng at the beginning of the function initialize().

Therefore, you first need to separate the values to define the values of latitude and longitude to then play in the property position in class options google.maps.Marker:

for (var i = 0; i < coord.length; i++) {
    var location = coord[i].split(",");

    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(location[0], location[1]),
        map: map
    });
}
  • in the variable coord mi returns the following data ex:-03.734084,-038.576155

  • Is a string at this value?

  • that’s right! maybe because it is in this format is not working!!

  • 1

    Right, so I adjusted my answer. See if now goes.

  • you have devised how to draw a line between the dots?

  • 1

    You refer to navigation routes or straight lines?

  • draw a straight line from one point to the other!

  • 1

    Here has an example of how to accomplish this. If you prefer, you can open a new question for this question. Do not forget to mark this as accepted if you have answered your question.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.