How to fetch category name of a venue from result object of fouresquare venue search? - ios

I am developing an ios app using phonegap in which I am using foursquare venue search api for listing all the near by venues.
This is the code I have used to list the name of all the near by venues.
$.getJSON('https://api.foursquare.com/v2/venues/search?ll='+pos+'&radius=10000&client_id=2POUFAUU4ZBJ2MTDOY3S2YHR2NIT52FYW0LUTPHBMNTJFJNQ&client_secret=YFDZI1YWV3ZI5S5SPM2DZJEQIEBPIDJ5XFZBWTIKIQZVQNYM&v=20120101&limit=60',
function(data) {
console.log(pos);
$.each(data.response.venues, function(i,venues){
content = '<li id="list-item"> <p><a href="#reviewPage" onClick="reviewPageAction(this)">' + venues.name + '</li>';
/*$(content).appendTo("#names");*/
$(content).appendTo("#mer");
});
});
And now my problem is , I want to display the category name with name of each venue. I have tried the following code
venues.categories.name
but it didn't work.
Is it possible to fetch the category name of each venue.?? Please help me!!

The Venues endpoint will return all the categories assigned to a venue. There can be multiple categories assigned to a venue, one of the categories will have a field "primary" that is set to true to indicate that it is the primary category.
Look for something like this in the result set:
categories: [
{
id: "4bf58dd8d48988d143941735",
name: "Breakfast Spot",
pluralName: "Breakfast Spots",
shortName: "Breakfast",
icon: {
prefix: "https://foursquare.com/img/categories_v2/food/breakfast_",
suffix: ".png"
},
primary: true
},
{
id: "4bf58dd8d48988d16a941735",
name: "Bakery",
pluralName: "Bakeries",
shortName: "Bakery",
icon: {
prefix: "https://foursquare.com/img/categories_v2/food/bakery_",
suffix: ".png"
},
}
}
]

Related

Exporting CSV from ui-grid with cell display rather than the source value

I have a table done with ui-grid and this table has a column with cellFilter definition like this:
{ field: 'company', cellFilter: 'mapCompany:row.grid.appScope.companyCatalog' }
My gridMenu custom item is configured like this:
gridMenuCustomItems: [
{
title:'Custom Export',
action: function ($event) {
var exportData = uiGridExporterService.getData(this.grid, uiGridExporterConstants.ALL, uiGridExporterConstants.ALL, true);
var csvContent = uiGridExporterService.formatAsCsv([], exportData, this.grid.options.exporterCsvColumnSeparator);
uiGridExporterService.downloadFile (this.grid.options.exporterCsvFilename, csvContent, this.grid.options.exporterOlderExcelCompatibility);
},
order:0
}
]
The table is displayed correctly, but when I try to export to CSV, the Company column is exported with empty value. Here is my Plunkr.
Any suggestion will be appretiated
References
I did some research and according to ui-grid Issue #4948 it works good when the Company column is filled with an static catalog. Here is a Plunkr demostrating that.
As you mentioned, there is an export issue when the values in the column are not static, so I forked your plunker with a solution that creates the company mapping for each data object and adds it as a key-value once you get the company catalog back:
$http.get('https://api.github.com/users/hadley/orgs')
.success(function(data) {
$scope.companyCatalog = data;
angular.forEach($scope.gridOptions.data, function(item) {
var compMap = uiGridFactory.getMapCompany(item.company, $scope.companyCatalog);
item.mappedCo = compMap;
});
});
I've kept the original company column but made it invisible (rather than overwriting it), so it exports with the rest of the data in the csv. New column defs:
columnDefs: [
{ field: 'name' },
{ field: 'mappedCo', name: 'Company'},
{ field: 'company', visible: false }
]
Also, in the case that you were getting your data from a server, you would have to ensure that the data and the catalog returned before you run the mapping function. This solution may not work in your particular use case; I don't know.
I found a solution to the CSV export to be able to display the cell display value.
My mistake was using row.grid instead of this.grid at the mapCompany cell filter:
columnDefs: [
{ field: 'name' },
{ field: 'company', cellFilter: 'mapCompany:row.grid.appScope.companyCatalog' }
],
The right way to use it is using this:
columnDefs: [
{ field: 'name' },
{ field: 'company', cellFilter: 'mapCompany:this.grid.appScope.companyCatalog' }
],
Here is my Plunkr.

angular using ui-select2 to assign an object as a model property

Hypothetical example to illustrate a problem I am having using angular-UI select2. Let's say I have a screen where I want to edit a "game" model. A game, among other things has players. I want to be able to set the players via a select2 drop down menu. Here's some example code:
app.js
$scope.getGamePromise().then(function(results) {
$scope.game = results;
console.log(game.players); //prints [{name:'Joe',age: 15},{name:'Sally',age:16}]
});
$scope.players = [
{
name: 'Joe',
age: 15
},
{
name: 'Fred',
age: 14
},
{
name: 'Sally',
age: 16
},
{
name: 'Lucy',
age: 13
}
]
view.html
<select ngModel="game.players" ui-select2 multiple>
<option ng-repeat="player in players" value="player">{{ player.name }}</option>
</select>
When I want to save this 'game' object, I send the game object up to the server. The server is expecting game.players to be an array of objects. However, what is being sent up is a string. I am moderately familiar with angular, and completely new to select2. How can I get my select2 to set game.players as an array of objects instead of strings?
I guess you find another solution or you don't have the problem anymore. Anyway I post it here:
Use
<input>
instead of
<select>
Example:
<input type="hidden" ui-select2="playersCfg" ng-model="players"/>
And following configuration:
$scope.playersCfg = {
multiple: true,
allowClear: true,
data: { results: Player.query(), text: 'name' },
formatResult: function(item) {
return "<div class='select2-user-result'>" + item.name + "</div>";
},
formatSelection: function(item) {
return item.name;
}
};
Player.query()
is a resource which returns a list of player containing a name (name) and an identifier (id)
Hope it would help somebody!

Grouping results in Select2

Is it possible somehow to group results in a Select2 component when it's not using <select> tag, but <input type="hidden">, and results are provided as "data" option in configuration object?
var select2Options = {
data: {
results: myArrayOfResults
}
};
Yes, the results objects support a children attribute...
so for example:
var select2Options = {
data: {
results: [
{text: "My shiny group", children: [
{id: 1, text: "My shiny item"},
{id: 2, text: "My shiny item2"}
]}
]
}
};
For ajax data loading with group and data work for me using,
$arrFinal = array(array("name"=>"My shiny group 1",
"children"=>array(array("id"=>1,"name"=>"My shiny item 11"),array("id"=>2,"name"=>"My shiny item 12"))
),array("name"=>"My shiny group 2",
"children"=>array(array("id"=>1,"name"=>"My shiny item 21"),array("id"=>2,"name"=>"My shiny item 22"))
)
);
die(json_encode(array("result" => $arrFinal)));
if formatResult: ratioFormatResult then,
function ratioFormatResult(row) {
// Here, you will get both group ("My shiny group 1") as well as data("My shiny item11") as row .
}
To make group selectable use id field along with name in group.

ExtJs 4 POST array of records from store

I have a simple rest store and after filling it with several records I am trying to send post request to server with array of records by calling create function.
The POST request is sent but only with one record instead of array of records.
UPDATE - I forgot to mention that this record has fields with empty values, and that is strange too, because I am filling store with values.
I stalled. Please give me a light where did I go wrong. Thanks.
My code samples:
Ext.define('my.store.testStore',{
extend: 'Ext.data.Store',
storeId: 'teststore',
model: 'my.model.testModel',
proxy: {
type: 'rest',
url: 'http://someurl.ru',
reader: 'json'
}
});
Ext.define('my.model.testModel',{
extend: 'Ext.data.Model',
fields: [
{name: 'name', type: 'string'},
{name: 'phone', type: 'string'},
{name: 'email', type: 'string'}
]
});
var namesList = [Ext.create('my.model.testModel',{
'name':'test name1',
'phone':'343-343',
'email':'test#test.com'
}),
Ext.create('my.model.testModel',{
'name':'test name2',
'phone':'6345',
'email':'test#test.com'
}),
Ext.create('my.model.testModel',{
'name':'test name2',
'phone':'24324',
'email':'test#test.com'
})
];
var testStore = Ext.create('my.store.testStore');
testStore.loadData(namesList);
testStore.create();
You should use testStore.sync() insted of testStore.create()
Another things:
testStore.create() - this function takes an object as first parameter and creates instance of the model then sends it to the server.
In case if "create" is called without parameter then instance of the model is created based on values from fields defaultValue which by default is set to "", that is way you got response with empty values.
About namesList. You forgot to add ")" to the 3-th model.
var namesList = [Ext.create('my.model.testModel',{
'name':'test name1',
'phone':'343-343',
'email':'test#test.com'
}),
Ext.create('my.model.testModel',{
'name':'test name2',
'phone':'6345',
'email':'test#test.com'
}),
Ext.create('my.model.testModel',{
'name':'test name2',
'phone':'24324',
'email':'test#test.com'
})//Here was missed ")"
];
After a great time, this issue appeared again so I had to solve it anyway.
And finally I succeeded to find working solution. The point is was just to add batchActions: true to store. Like this:
Ext.define('my.store.testStore',{
extend: 'Ext.data.Store',
storeId: 'teststore',
model: 'my.model.testModel',
proxy: {
type: 'rest',
url: 'http://someurl.ru',
reader: 'json',
batchActions: true
}

Can I export translations of place names from freebase.com

So I've looked at this use of the freebase API and I was really impressed with the translations of the name that it found. IE Rome, Roma, Rom, Rzym, Rooma,로마, 罗马市. This is because I have a database of some 5000+ location names and I would very much like all French, German or Korean translations for these English names.
The problem is I spent about two hours clicking around freebase, and could find no way to get a view of city/location names in a different language mapped to English. So I'd love it if someone who understands what freebase is and how it's organized could get me a link to that view which theoretically I could then export.
Also I just wanted to share this question because I'm totally impressed with freebase and think if people haven't looked at it they should.
The query
[{
limit: 100,
type: '/location/location',
name: [{
value: null,
lang: {
name: {
value: null,
lang: '/lang/en',
},
}
}],
}];
returns for every location and every language, the name of that location in that language. The results are organized by language. For example, here is a very small segment of the return value:
{
'lang': {
'name': {
'lang': '/lang/en',
'value': 'Russian'
}
},
'value': 'Сан-Франциско'
},
{
'lang': {
'name': {
'lang': '/lang/en',
'value': 'Swedish'
}
},
'value': 'San Francisco'
},
{
'lang': {
'name': {
'lang': '/lang/en',
'value': 'Portuguese'
}
},
'value': 'São Francisco (Califórnia)'
},
For a no-programming solution, copy-paste the following into an HTML file and open it with your browser:
<html><head>
<script type="text/javascript" src="http://mjtemplate.org/dist/mjt-0.6/mjt.js"></script>
</head>
<body onload="mjt.run()">
<div mjt.task="q">
mjt.freebase.MqlRead([{
limit: 10,
type: '/location/location',
name: [{
value:null,
lang:{
name:{
value:null,
lang:'/lang/en',
},
}
}],
}])
</div>
<table><tr mjt.for="topic in q.result"><td>
<table><tr mjt.for="(var rowi = 0; rowi < topic.name.length; rowi++)"
mjt.if="rowi < topic.name.length" style="padding-left:2em"><td>
<pre mjt.script="">
var name = topic.name[rowi];
</pre>
${(name.lang['q:name']||name.lang.name).value}:
</td><td>$name.value</td></tr></table></td></tr></table></body></html>
Of course, that will only include the first 10 results. Up the limit above if you want more. (By the way, not only is Freebase cool, so is this mjt templating language!)
The link you posted uses mjt, a javascript framework designed for Freebase.
The Query they use.
mjt.freebase.MqlRead([{
limit: 100,
id:qid,
/* allow fuzzy matches in the value for more results... */
/* 'q:name': {'value~=': qname, value:null, lang: '/lang/'+qlang}, */
'q:name': {value: qname, lang: '/lang/'+qlang},
type: '/common/topic',
name: [{
value:null,
lang:{
id:null,
name:{
value:null,
lang:'/lang/en',
optional:true
},
'q:name':{
value:null,
lang:'/lang/'+qlang,
optional:true
}
}
}],
article: [{id:null, limit:1}],
image: [{id:null, limit:1, optional:true}],
creator: null,
timestamp:null
}])
Where:
qlang - is your desired language to translate too.
qname - is is the location to query.
To get the link you want, you'll need the API, and you can convert the above query to a link that will return a JSON object containing the translated string.

Resources