How to load data obtained from an Ajax in the "description" attribute in Openlayers 2?

1

is the first time that the OSM API with OpenLayers and I have the problem that when I want to load the values returned with an Ajax, it does not load them. this happens in the "description" attribute of the new OpenLayers.Feature.Vector function, this is for dynamic bookmarks. I leave the code that I have, if someone has any ideas or has worked with this API and have more experience, I would greatly appreciate it.

This is my code:

function initOpenStreetMap() {

  epsg4326 = new OpenLayers.Projection("EPSG:4326"); //WGS 1984 projection
  projectTo = new OpenLayers.Projection("EPSG:900913"); //The map projection (Spherical Mercator)

  /* div content map and controls of map */
  map = new OpenLayers.Map("map_canvas", {
    controls: [
      new OpenLayers.Control.Navigation(), // Navegar por el mapa.
      //new OpenLayers.Control.ScaleLine(),                             // La escala utilizada en el mapa.
      new OpenLayers.Control.KeyboardDefaults(), // Mover el mapa con el teclado.
      new OpenLayers.Control.Zoom()
    ]
  });
  map.addLayer(new OpenLayers.Layer.OSM());

  var vectorLayer = new OpenLayers.Layer.Vector('StreetMap', {
    styleMap: new OpenLayers.StyleMap({
      externalGraphic: 'img/icon-hotel.png',
      graphicWidth: 32,
      graphicHeight: 46,
      graphicYOffset: -24
    })
  });


  var feature = [];
  if (locations.length != 0) {
    for (var i = 0; i < locations.length; i++) {

      var datainfo = $('#search_params').val();
      datainfo += '/byuid/' + locations[i][1];

      var LonLati = new OpenLayers.Geometry.Point(locations[i][3], locations[i][2]).transform(epsg4326, projectTo);
      var feature2 = new OpenLayers.Feature.Vector(LonLati);
      feature.push(feature2);
      feature2.attributes.description = '<h3>' + locations[i][0] + '</h3><br><a href="' + datainfo + '"> Mas Informacion</a><div id="info_show"></div>';

      $(function() {
        $.ajax({
          type: 'get',
          dataType: 'html',
          url: datainfo,
          success: function(data) {
            $('#info_show').append('"' + data + '"');
            //description: data
          }
        });

      })



      /*map.events.register("click", map , function(event){
      	//createInfo(map, event.LonLati, datainfo);
      });*/

    }
  }

  vectorLayer.addFeatures(feature);
  map.addLayer(vectorLayer);

  //Add a selector control to the vectorLayer with popup functions
  var controls = {
    selector: new OpenLayers.Control.SelectFeature(vectorLayer, {
      onSelect: createPopup,
      onUnselect: destroyPopup
    })
  };

  function createPopup(feature) {
    feature.popup = new OpenLayers.Popup.FramedCloud("pop",
      feature.geometry.getBounds().getCenterLonLat(),
      null,
      '<div class="markerContent">' + feature.attributes.description + '</div>',
      null,
      true,
      function() {
        controls['selector'].unselectAll();
      }
    );
    map.addPopup(feature.popup);
  }

  function destroyPopup(feature) {
    feature.popup.destroy();
    feature.popup = null;
  }

  map.addControl(controls['selector']);
  controls['selector'].activate();

  map.setCenter(new OpenLayers.LonLat(locations[0][3], locations[0][2]).transform(epsg4326, projectTo), 12);
}

The data that is obtained, comes from an array, locations [i] [1] is a token or works as an identifier, I am trying to execute the ajax and load what comes back, but it is not working Try the map.events.register function but there were no results.

greetings and I would appreciate the support, contributions and ideas.

    
asked by MNolasco_L 27.06.2017 в 18:31
source

0 answers