0

I am using this JSON link to access data now i want to access the place name if i hover my mouse over particular marker !

$.getJSON('http://anyorigin.com/get?url=https%3A//maps.googleapis.com/maps/api/place/nearbysearch/json%3Flocation%3D-33.8670522%2C151.1957362%26radius%3D5000%26types%3Dfood%26name%3Dharbour%26sensor%3Dfalse%26key%3DAIzaSyAYMqH361IS1S9SA4atjBPCDlLpJ4mr6Sw&callback=?', function (data) {
    var responseData = data.contents;
});

events: {
    mouseover: function (marker, event, context) {
        console.log(responseData.results[this].name)
    }
},
Tushar Gupta - curioustushar
  • 54,013
  • 22
  • 95
  • 103
user123
  • 31
  • 1
  • 10
  • 3
    Declare your "var responseData" globaly in your script and then try – SoftwareNerd Nov 07 '13 at 10:59
  • yeah i did still not working :( ! if i gove responseData.results[0].name then i am getting my results ! there is something wrong with passing data inside the results array – user123 Nov 07 '13 at 11:01
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – adeneo Nov 07 '13 at 11:02
  • use an each loop on the response data – SoftwareNerd Nov 07 '13 at 11:04
  • @SoftwareNerd : Could you help me to how to use that in my code ! i.e in this segment console.log(responseData.results[this].name) – user123 Nov 07 '13 at 11:05

3 Answers3

0

Here in you code this refers the moverover event. So the problem is there also make responseData as global variable.

Updates:

Here is a simple approach

var responseData = data.contents;
    var res = responseData.results;
    for (var i = 0; i < res.length; i++) {
        console.log(res[i].name)
    }

JSFiddle

Praveen
  • 50,562
  • 29
  • 125
  • 152
  • Could you help me to how to use that in my code ! i.e in this segment console.log(responseData.results[this].name) – user123 Nov 07 '13 at 11:04
  • Yea i can get name if i pass it through an for loop but my problem is if i mouseover on any marker the name of particular marker stored in JSON must be displayed ! – user123 Nov 07 '13 at 11:20
  • @user2889942 check this http://jsfiddle.net/Gmr4p/1/ what you need is some where the index numbers. you can even use data attribute – Praveen Nov 07 '13 at 11:29
  • in my console,am getting typeerror:res[parseInt($(this).text())].name is undefined – user123 Nov 07 '13 at 11:36
  • @user2889942 definitely you will get undefined. the reason is you're not using as I mentioned in fiddle. You have marker generated by script. Do not possible, try using data attributes – Praveen Nov 07 '13 at 12:04
0

here you can use loop like this

      $.each(responseData , function (index, value) {
            console.log(value.name)
      });
SoftwareNerd
  • 1,825
  • 8
  • 28
  • 58
0

Assuming the return type of response data is something valid, I doubt using results[this] will lead to coherent data.

You should inspect what responseData.results contains to see what data you want to display (add 'debugger;' and inspect responseData.results).

var responseData;
$.getJSON('http://...', function (data) 
{
    responseData = data.contents;
});

events: 
{
    mouseover: function (marker, event, context) 
    {
        debugger; // remove after inspect responseData and extract the worthy data
        console.log(responseData.results)
    }
},
Baart
  • 512
  • 5
  • 14