BloodRayne Blood
BloodRayne Blood

Reputation: 63

Google maps v3 array marker

Hy i am unable to display multiple markers in Google Maps V3, i am getting the coordinates correctly but not displays on map. Also no errors in console

  1. map_items[0] = title
  2. map_items[1] = 55.153766, 11.909180
  3. map_items[2] = link
  4. map_items[3] = text

all of them appear correctly if i do an alert. example

"Title","51.00150763193481, -2.5659284999999272", "link", "text"

 function initialize() {
            var map = new google.maps.Map(document.getElementById('map'), {
                zoom: 7,
                center: new google.maps.LatLng(55.153766, 11.909180),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });
            for (var x = 0; x < temp.length; x++) {
                if(temp[x][1]){
                    var map_items = temp[x];
                    var myLatlng = new google.maps.LatLng(map_items[1]);
                    var marker = new google.maps.Marker({
                        map: map,
                        position: myLatlng,
                        title: map_items[0]
                    });
                    google.maps.event.addListener(marker, 'click', function() {
                        infowindow.setContent('<div class="google_marker"><a href="'+map_items[2]+'">'+map_items[0]+'</a><br /><p>'+map_items[3]+'</p></div>');
                        infowindow.open(map, marker);
                    });
                }
            }
        }

Upvotes: 0

Views: 1749

Answers (3)

Anoop
Anoop

Reputation: 23208

I think only last map_items would be available when you click on any marker. Creating a different context may solve your problem.

function initialize() {
                var map = new google.maps.Map(document.getElementById('map'), {
                    zoom: 7,
                    center: new google.maps.LatLng(55.153766, 11.909180),
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                });
                function AttachEventToMarker(marker, map_items){

                    google.maps.event.addListener(marker, 'click', function() {
                       infowindow.setContent('<div class="google_marker"><a href="'+map_items[2]+'">'+map_items[0]+'</a><br /><p>'+map_items[3]+'</p></div>');
                       infowindow.open(map, marker);
                    });
                }

                for (var x = 0; x < temp.length; x++) {
                    if(temp[x][1]){
                        var map_items = temp[x];
                        var myLatlng = new google.maps.LatLng(map_items[1]);
                        var marker = new google.maps.Marker({
                            map: map,
                            position: myLatlng,
                            title: map_items[0]
                        });
                         AttachEventToMarker(marker, map_items);
                    }
                }
            }

Upvotes: 0

Marcelo
Marcelo

Reputation: 9407

google.maps.LatLng() takes two parameters, not one, so:

var latlon = map_items[1].split(',');
var myLatlng = new google.maps.LatLng(parseFloat(latlon[0]), parseFloat(latlon[1]));

though in fact, it would be better to use objects rather than an array in which each item contains different data types, for example:

marker_data[0] = {
  "lat": 55.153766,
  "lon": 11.909180,
  "title": "My Title",
  "link": "http://stackoverflow.com"
}
//etc...

and then you'd access it like this:

var myLatlng = new google.maps.LatLng(marker_data[0].lat, marker_data[0].lon);

Upvotes: 7

Diode
Diode

Reputation: 25135

Assuming map_items[1] is string

if (temp[x][1]) {
    var map_items = temp[x];
    var latlng = map_items[1].split(",");
    var myLatlng = new google.maps.LatLng(parseFloat(latlng[0]), parseFloat(latlng[1]));
    var marker = new google.maps.Marker({
        map: map,
        position: myLatlng,
        title: map_items[0]
    });
    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent('<div class="google_marker"><a href="' + map_items[2] + '">' + map_items[0] + '</a><br /><p>' + map_items[3] + '</p></div>');
        infowindow.open(map, marker);
    });
}

Upvotes: 1

Related Questions