Maps API Cordova - Add multiple markers

Asked

Viewed 176 times

1

I am using the code below to add several markers to the map. Each click a marker is added. In Chrome the code works perfectly, however, when generating code using Cordova the map is displayed but no marker is added.

        function initialize() {
          var inicial = { lat: -22.4086028, lng: -43.6624047 };
          var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 17,
            center: inicial
          });

        google.maps.event.addListener(map, 'mousedown', function(event) {
          var initPoint = {x: event.pageX, y: event.pageY},
          toBeCanceled = false,
          latLong = event.latLong;

          google.maps.event.addListener(map, 'mousemove', function(event) {
            var newPoint = {x: event.pageX, y: event.pageY};
            toBeCanceled = true
          });

          google.maps.event.addListener(map, 'mouseup', function(event) {
            if (toBeCanceled) {
              event.stop()
            } else {
              addMarker(event.latLng, map);
              console.log(event.latLng.lat() +' _ ' + event.latLng.lng());
              salvaPonto(event.latLng.lat(), event.latLng.lng());
              event.latLng.empty();
            }
          });

        });

        }

        function addMarker(location, map) {
            var marker = new google.maps.Marker({
              position: location,
              label: "D",
              map: map
            });

        }


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

1 answer

0

Try it this way:

addMarkers(data, function(markers) {
  markers[markers.length - 1].showInfoWindow();
});

function addMarkers(data, callback) {
  var markers = [];
  function onMarkerAdded(marker) {
    markers.push(marker);
    if (markers.length === data.length) {
      callback(markers);
    }
  }
  data.forEach(function(markerOptions) {
    map.addMarker(markerOptions, onMarkerAdded);
  });
}

Info: https://github.com/mapsplugin/cordova-plugin-googlemaps/wiki/marker

  • in my function, every click on the maps a Marker is added. From what I understand of yours, loads markers already "ready" I am correct?

Browser other questions tagged

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