Mark several points in Google Maps?

1

I have a problem with the markers in the map of google maps, I have been trying to implement a google map with javascript api, I work with ionic 1 and angularjs and this is the code I have so far. The problem is that it only marks me where I am and does not show me the markers of the stores that I get with the json.

Javascript and Cordova geolocalization

var options = {
  timeout: 10000,
  enableHighAccuracy: true
 };

 $cordovaGeolocation.getCurrentPosition(options).then(function(position) {

 var latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

 var mapOptions = {
    center: latLng,
    zoom: 17,
    disableDefaultUI: true
 };

 var map = new google.maps.Map(document.getElementById("map"), mapOptions);

 var marker = new google.maps.Marker({
   position: latLng,
   map: map,
 });
 // To add the marker to the map, call setMap();
 marker.setMap(map);

}, function(error) {
  console.log("Could not get location");
});

$.getJSON("http://www.kupomcity.com/gamma/api_v2.php?_opt=comercios&_act=json", function(json1) {
  $.each(json1, function(key, data) {
  //console.log (data);
    if (data.lat != '' && data.lng != '') {
      console.log("json lat: " + data.lat + "json lng: " + data.lng);
      var latLng = new google.maps.LatLng(data.lat, data.lng);

      var marker = new google.maps.Marker({
        position: latLng,
      });

      marker.setMap($scope.map);
    };
  });
})    
    
asked by Nelsón J. Torres 15.06.2017 в 19:35
source

1 answer

1

how are you? The correct way to place multiple markers on the map is explained in the Google Maps documentation. I leave the official JSFiddle of Google where it explains how to add several markers to the map using an array (which could be the data of your JSON file) - link

Basically uses an iterator, in each iteration a new marker is added using the map instance already initialized.

I hope it has helped you.

Greetings!

    
answered by 21.06.2017 в 20:28