CoffeeGuide icon indicating copy to clipboard operation
CoffeeGuide copied to clipboard

Get categories icons

Open taktikal17 opened this issue 9 years ago • 12 comments

Hey!

First thank you for this tutorial! I am trying to add more features on it, and I would like to get the categories icons from foursquare.

https://developer.foursquare.com/docs/responses/category https://developer.foursquare.com/categorytree

I have an issue to implement properly CoffeeAPI and Venue. I don't know how to manage the prefix and the suffix elements. Do you know how to do it? Thanks a lot!

Valentin

taktikal17 avatar Jan 19 '16 18:01 taktikal17

Hi Valentin,

Thanks for getting in touch. Could you explain me what you're trying to do? I'm not sure I quite understand. Are you trying to show multiple categories, and get the category images to show up in your app?

You can get the category image URLs like this:

Pieces needed to construct category icons at various sizes. Combine prefix with a size (32, 44, 64, and 88 are available) and suffix, e.g. https://foursquare.com/img/categories/food/default_64.png. To get an image with a gray background, use bg_ before the size, e.g. https://foursquare.com/img/categories_v2/food/icecream_bg_32.png.

reinder42 avatar Jan 19 '16 19:01 reinder42

Thank you for answering. Actually, I changed the categoryID for the shop one ( not the coffee one). And in the global shop category, there are many other categories. And to mark the difference between each categories, I would like to get the icon which match with each categories.

Basically, I would like to keep your CoffeeAPI implementation, but I don't know how to do it.

 if let icon = venue["icon"] as? NSURL
                                        {
                                            prefix = icon["prefix"] as? String,
                                            suffix = icon["suffix"] as? String {
                                           let url = prefix + 32x32 + suffix

                                  }
                                venueObject.icon = icon
                                  }

I tried this, but without success.

Thank you.

taktikal17 avatar Jan 19 '16 19:01 taktikal17

OK, got it. You first need to check the syntax of the snippet you posted. Also, use Markdown styling when pasting code on Github.

Try this:

if let icon = venue["icon"] as? NSURL
{
    if let prefix = icon["prefix"] as? String,
        suffix = icon["suffix"] as? String 
    {
        let url = "\(prefix)_32x32_\(suffix)"
        let pos = CLLocationCoordinate2DMake(lat, long)
        venues.append(FourSquarePlace(coordinates: pos, title: name, address: address, imageURL:url, photo: nil))
    }
    venueObject.icon = icon
}

Did you see I changed the let url line? You can't add strings to another, you need to use something called "string interpolation" to add strings to each other.

reinder42 avatar Jan 20 '16 10:01 reinder42

Thank you! I worked on it today.

Actually, when I run the project with the following modification, I get an error: ####Error Domain=io.realm Code=0 "Migration is required due to the following errors: ####- Property 'nameCategories' has been added to latest object model."

I am little bit perplex, it looks like that we can't add more information for the query to Foursquare. Maybe it blocks because we execute the query regarding to the categoryID..

 if let categories = venue["categories"] as? [String: AnyObject]
                                          {
                                          if let name = categories["name"] as? [String]
                                              {
                                                      venueObject.nameCategories = name.joinWithSeparator(" ")
                                                }
                                           }

By the way, thanks for the tips, it is much better with Markdown.

Have a good one.

taktikal17 avatar Jan 20 '16 16:01 taktikal17

No such module 'RealmSwift'

mvalbuquerque avatar Jan 21 '16 22:01 mvalbuquerque

Hi @mvalbuquerque,

Did you install and/or update the pods with pod install or pod update? Also, did you open the .xcodeproj file or the .xcworkspace? You have to work with the workspace. Note that this project is part of a code guide on AppCoda: http://www.appcoda.com/foursquare-realm-swift/.

Next time, please open a new issue on GitHub if the error you're getting is unrelated to an issue already present. Thanks!

reinder42 avatar Jan 22 '16 10:01 reinder42

Hi ! @reinderdevries tankns for answering.

The update pod solved the problem.

mvalbuquerque avatar Jan 22 '16 22:01 mvalbuquerque

Hi @reinderdevries , Do you know how to use the multi parameters function to get the venues ? ( I don't know how to properly implement function ) I would like to remplace your function in CoffeeAPI.swift:

     let searchTask = session.venues.search(parameters) {
     ...
      searchTask.start()
      }

by to take into account two categoryId:

    let multiTask = session.multi.get(tasks: [parameters, parameters1], completionHandler:                    ResponseClosure) {
       ...
      multiTask.start()
      }

with parameters:

    var parameters = location.parameters()
    parameters += [Parameter.categoryId: "4bf58dd8d48988d103951735"]
    parameters += [Parameter.radius: "2000"]
    parameters += [Parameter.limit: "50"]

and :

  var parameters1 = location.parameters()
    parameters1 += [Parameter.categoryId: "52f2ab2ebcbc57f1066b8b23"]
    parameters1 += [Parameter.radius: "2000"]
    parameters1 += [Parameter.limit: "50"]

Thank you a lot!!

taktikal17 avatar Feb 26 '16 00:02 taktikal17

Use it like this:

let task1 = self.quadratSession.users.get()
let task2 = self.quadratSession.users.friends(userId: "self", parameters: nil)

let multiTask = self.quadratSession.multi.get([task1, task2]){
    (responses) -> Void in
    println(responses)
}
multiTask.start()

First, you create two tasks and store them in two constants. Don't .start() the tasks. Then, create a third task (multiTask) in which you put the two tasks, attach a completion handler and start the multiTask.

In the code you mentioned, you're putting the parameters into the multi task. Put the parameters each in their separate tasks, and then put those tasks into the multitask.

reinder42 avatar Feb 26 '16 09:02 reinder42

@reinderdevries , Thank you for your reply!

I tried to follow your advice:

getCoffeeShopsWithLocation(location:CLLocation) { if let session = self.session { var parameters = location.parameters() parameters += [Parameter.categoryId: "4bf58dd8d48988d103951735"] parameters += [Parameter.radius: "1200"] parameters += [Parameter.limit: "50"]

        var parameters1 = location.parameters()
        parameters1 += [Parameter.categoryId: "4bf58dd8d48988d10c951735"

] parameters1 += [Parameter.radius: "1200"] parameters1 += [Parameter.limit: "50"]

        let task1 = session.venues.search(parameters)
        let task2 = session.venues.search(parameters1)

        let searchTask = session.multi.get([task1, task2]){
            (result) -> Void in
            print ("ok:", result.results)
            print ("ok:", result.response)
            if let response = result.response
            //response
            {
                if let venues = response["venues"] as? [[String:

AnyObject]] { autoreleasepool { let realm = try! Realm() realm.beginWrite() for venue:[String: AnyObject] in venues { let venueObject:Venue = Venue() if let id = venue["id"] as? String { venueObject.id = id } if let name = venue["name"] as? String { venueObject.name = name } if let location = venue["location"] as? [String: AnyObject] { if let longitude = location["lng"] as? Float { venueObject.longitude = longitude } if let latitude = location["lat"] as? Float { venueObject.latitude = latitude } if let formattedAddress = location[ "formattedAddress"] as? [String] { venueObject.address = formattedAddress.joinWithSeparator(" ") } } realm.add(venueObject, update: true) } do { try realm.commitWrite() print("Committing write...") } catch (let e) { print("Y U NO REALM ? (e)") } } NSNotificationCenter.defaultCenter(). postNotificationName(API.notifications.venuesUpdated, object: nil, userInfo: nil) } } } searchTask.start() } }

When I print (result.results), I got all the shops list, I mean all the JSON response.(And I saw the business icons are available actually) But when I print (result.response), I got a nil..

And on the app nothing is displayed. Do you have an idea on how to fix it?

Thank you a lot!

taktikal17 avatar Feb 26 '16 17:02 taktikal17

Hi, Does anybody know the usage licence associated to the venue category icons from foursquare ? (Can't find this info on their site and no reply from their support on it...) Thanks

jpduvet avatar May 09 '19 10:05 jpduvet

@jpduvet I don't know – asking Foursquare directly is the best option here I think. Perhaps it's covered in the license/agreement for using the API itself? The API data is/must be covered by an agreement too.

reinder42 avatar May 09 '19 11:05 reinder42