简体   繁体   中英

google maps api v3 not showing markers

So here's my code...

First I check the database...

function buscarCoords(callback){
      var result = result;
      connection.query('SELECT * FROM monitoreo_actual', function(err, result){
        if(err){
          console.log(err);
        }
        callback({result:result});
      });
}

Then I have a callback function that basically calls yet another function that will convert the data into a google maps latLng format (too many functions i know, i will fix that later)

database.buscarCoords(function(resultsObject){
    convert.ConvertFromGrdToGoogle(resultsObject.result);
});

inside the convert function i call another function that will save the array to a txt file

function ConvertFromGrdToGoogle(grd){
........
files.saveMonActual(JSON.stringify(arrCoords));
}

this is the function that saves to the file

var saveMonActual = function(grd){
  fs.writeFile("grd2google.txt", grd, 'UTF-8', function(err) {
    if(err) {
      console.log(err);
    } else {
      console.log("=> grd2google.txt actualizado con las coordenadas convertidas a Google");
    }
  });
}

an example of the output

"'2','10.1850','-68.334','4','10.1850','68.334','3','10.1853','-68.334','5','10.1853','-68.334'"

and last but not least i have a function to read the data and strip it from the unnecesary quotes and single quotes.

var readMonActual = function(){
  var coordsArray=fs.readFileSync("grd2google.txt", "utf8");
  var coordsArray=coordsArray.substring(1,coordsArray.length-1);
  var myCoords=[];
  data=coordsArray.split(',');
  for (var i=0;i<data.length;i+=3){
    myCoords.push([
                    Number(String(data[i]).replace(/\'/g, "")),
                    Number(String(data[i+1]).replace(/\'/g, "")),
                    Number(String(data[i+2]).replace(/\'/g, ""))
                  ]);
  }
  return myCoords;
}

this would be the console.log of that function

[ [ 2, 10.185, -68.334 ],
  [ 4, 10.185, 68.334 ],
  [ 3, 10.1853, -68.334 ],
  [ 5, 10.1853, -68.334 ] ]

So i call that function from the initialize() in the google maps html file's tag

function initialize() {
  var mapOptions = {
    center: new google.maps.LatLng(10.191, -68.191),
    zoom: 7,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("map-canvas"),mapOptions);
  var markers = readMonActual().myCoords;
  for(i=0; i<markers.length; i++ ) {
    var position = new google.maps.LatLng(Number(markers[i][1]), Number(markers[i][2]));
    //bounds.extend(position);
    marker = new google.maps.Marker({
             position: position,
             map: map,
             title: String(markers[i][0])
             });
  }
}
 google.maps.event.addDomListener(window, 'load', initialize);

this is one of the many ways i've tried to display the map markers but they still won't show. Some help would be greatly appreciated

The main problem is call

var markers = readMonActual().myCoords;

It should be

var markers = readMonActual();

You are returning an array from function.

And definition of marker array is missing in front of for loop:

var marker = [];

Check example at jsBin . Function readMonActual() is changed to just return proper result. I cannot test database part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM