Captain Nemo
Captain Nemo

Reputation: 346

Google Maps - Populate a map with MVC Object

I found this code at this url : http://gmaps-samples-v3.googlecode.com/svn/trunk/poly/poly_edit.html

The goal is easy : Draw a shape with markers. Markers can be added, dragged, removed.

In my case, I have multiples coordinates, and I want to inject them in the MVC object to draw the shape at page loading.

In the code, we have this function :

function addPoint(event) {
    path.insertAt(path.length, event.latLng);
    var marker = new google.maps.Marker({
        position: event.latLng,
        map: map,
        draggable: true
    });
    markers.push(marker);
    marker.setTitle("#" + path.length);

    google.maps.event.addListener(marker, 'click', function() {
        marker.setMap(null);
        for (var i = 0, I = markers.length; i < I && markers[i] != marker; ++i);
        markers.splice(i, 1);
        path.removeAt(i);
    });

    google.maps.event.addListener(marker, 'dragend', function() {
        for (var i = 0, I = markers.length; i < I && markers[i] != marker; ++i);
        path.setAt(i, marker.getPosition());
    });
}

I have a list of coordinates, like this :

var locations = ['48.87304938, 2.77083992','48.87153217, 2.77080774','48.86765779, 2.77210593']

I imagine part of code like this :

addPoint(locations[0]);
addPoint(locations[1]);

but not function..

have you got some ideas ?

(Sorry for my english... )

Upvotes: 0

Views: 901

Answers (1)

Dr.Molle
Dr.Molle

Reputation: 117354

addPoint expects as argument an object with a latLng-property set to a google.maps.LatLng() , but you provide a string.

possible approach:

var locations = ['48.87304938, 2.77083992',
                 '48.87153217, 2.77080774',
                 '48.86765779, 2.77210593'];

//iterate over the items
for(var i=0;i<locations.length;++i){
  //split the item to separate latitude and longitude
  var point=locations[i].split(',');
  //call the function
  addPoint({latLng:new google.maps.LatLng(point[0],point[1])});

}

Upvotes: 1

Related Questions