0

It's seems to be very easy but i can not make it works. I have an array con 2 var inside that i get from Jquery.

var features = [long, lat ] 

i would like to get all the valores in the loop and i need

fetures[i] 

that will give me long1 and lat1 but when i try

alert( features[0] )

i get all the long values.

and when i try with [1] i get all the lat values. How can i solve this issue?

I can not make it work, maybe you can have a look at my code:

$.ajax({ 
                    url : "https://api.foursquare.com/v2/venues/search?limit=3&radius=1000&client_id=J22NHX41TJBJ2PZM4NBTLDWLDYIBWLMIF4LJCFWNAXK1WALY&client_secret=RS110L4OYLY1XFCEMV30UB2JJ1JRFQZ0E3P0USSIMSCA45RZ&v=20120101&ll=" + ui.item.y +"," + ui.item.x, 
                    dataType : "jsonp",
                    success : function(data) { 
                        $.each(data.response.venues, function( index, value ) {
                        //alert( index + ": " + value.name + " , "+ value.location.lng);

                        var Name = value.name
                        var VenueLati = value.location.lat
                        var VenueLong = value.location.lng
                        //alert( VenueLong );


                        var features = []; 
                        for(var i = 0; i < features[0].length; i++) {
                        feature[i] = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(VenueLong, VenueLati).transform('EPSG:4326', 'EPSG:3857'),
                        {some:'data'},

                            {
                                foo : 
                            }, {
                                fillColor : '#008040',
                                fillOpacity : 0.8,                    
                        strokeColor : "#ee9900",
                        strokeOpacity : 1,
                        strokeWidth : 1,
                        pointRadius : 8
                    });
        }

When i put a number intead features[0].length then i can see the points but can see only one popup :/

$.ajax({ 
                    url : "https://api.foursquare.com/v2/venues/search?limit=3&radius=1000&client_id=J22NHX41TJBJ2PZM4NBTLDWLDYIBWLMIF4LJCFWNAXK1WALY&client_secret=RS110L4OYLY1XFCEMV30UB2JJ1JRFQZ0E3P0USSIMSCA45RZ&v=20120101&ll=" + ui.item.y +"," + ui.item.x, 
                    dataType : "jsonp",
                    success : function(data) { 
                        $.each(data.response.venues, function( index, value ) {
                        //alert( index + ": " + value.name + " , "+ value.location.lng);

                var Name = value.name
                    var VenueLati = value.location.lat
                    var VenueLong = value.location.lng
                    alert( VenueLong );

                        var features = [];    
        for(var i = 0; i<50; i++) {
        features[i] = new OpenLayers.Feature.Vector(
                toMercator(new OpenLayers.Geometry.Point(VenueLong,
                                                        VenueLati)), 
                {
                    ulica : Name
                }, {
                    fillColor : '#008040',
                    fillOpacity : 0.8,                    
                    strokeColor : "#ee9900",
                    strokeOpacity : 1,
                    strokeWidth : 1,
                    pointRadius : 8
                });
    }

    // create the layer with listeners to create and destroy popups
    var vector = new OpenLayers.Layer.Vector("Points",{
        eventListeners:{
            'featureselected':function(evt){
                var feature = evt.feature;
                var popup = new OpenLayers.Popup.FramedCloud("popup",
                    OpenLayers.LonLat.fromString(feature.geometry.toShortString()),
                    null,
                    "<div style='font-size:.8em'>Name: " + feature.id +"<br>Address: " + feature.attributes.ulica+"</div>",
                    null,
                    true
                );
                feature.popup = popup;
                map.addPopup(popup);
            },
            'featureunselected':function(evt){
                var feature = evt.feature;
                map.removePopup(feature.popup);
                feature.popup.destroy();
                feature.popup = null;
            }
        }
    });
    vector.addFeatures(features);

    // create the select feature control
    var selector = new OpenLayers.Control.SelectFeature(vector,{
        hover:true,
        autoActivate:true
    }); 

    map.addLayers([vector]);
    map.addControl(selector);
    center = new OpenLayers.LonLat(lon, lat).transform('EPSG:4326', 'EPSG:3857');
    map.setCenter(center, 15);
bucek
  • 142
  • 2
  • 13

2 Answers2

1

You have 2 array in one array like this:

var features = [ [100, 200, 300, 400], ["A", "B", "C", "D"];

if you want to get the long s, you should do this:

for(var i=0;i<features[0].length;i++){
    alert(features[0][i]);
}

and for the lat s:

for(var j=0;j<features[1].length;j++){
    alert(features[1][j]);
}

but I think you are expecting something else, like an array of json objects with long and lat as its keys, although you can map these 2 array based on their order, if both lists are ordered:

var newArray = [];
for(var i=0;i<features[0].length;i++){
    newArray.push({"long":features[0][i], "lat":features[1][i]});
}
Mehran Hatami
  • 12,723
  • 6
  • 28
  • 35
  • @It seems to be easy but unfortuantely i can not make it works :( I put my code so you can have a look and maybe you will be able to point the error – bucek Feb 02 '14 at 13:37
  • @bucek: before starting to iterate using $.each, do this `console.log(JSON.stringify(data))` and copy the result here in the next comment for me to help you fix it. – Mehran Hatami Feb 02 '14 at 15:23
  • @bucek: or at least give me the right geo location for the end of your url : `ui.item.y +"," + ui.item.x` – Mehran Hatami Feb 02 '14 at 15:32
  • @ Mehran Hatami i have updated my post. the geolocation can be for example: ui.item.y= 51.508 and ui.item.x= -0.127 for london, i get it automatically from geonames – bucek Feb 02 '14 at 15:44
  • @bucek: it seems you are trying to do something based on `features` array, but at the other hand you are creating an array with the same name?? if you want come to this [chatroom](http://chat.stackoverflow.com/rooms/46211/myjs) to have a chat. – Mehran Hatami Feb 02 '14 at 16:10
  • perfect, we can chat :) – bucek Feb 03 '14 at 01:03
1

Instead of literal values in your array, you will want objects:

var features = [{long: 123, lat: 234}, {long: 545, lat: 677}];

Then,

features[0] 

will give you

{long: 123, lat: 234}

Which you can access by

features[0].long 
features[0].lat

I hope this helps. Let me know if you have further questions.

Harvey A. Ramer
  • 763
  • 7
  • 13