Search code examples
iosjsonswiftgeolocation

Swift4: I want to rearrange the JSON acquired by API in near order from my location


Thank you for watching my question.

My iOS Application flow. When you press the category button, the data of the store in that category will be returned JSON. Latitude and longitude are included in the data of the shop. Based on this latitude and longitude I would like to rearrange the contents of JSON so that you can display the shop in order from my position.

I'm searching for a sample code, but I could not find it.

please tell me.

thank you.

JSON DATA

{
"id": 1,
"name": "Barth day",
"created_at": "2018-08-09 10:00:58",
"updated_at": "2018-08-09 10:00:58",
"stores": [
    {
        "id": 1,
        "name": "Pink Cafe Tokyo",
        "location": "Tokyo",
        "price": "1000~3000",
        "open_time": "10:00-14:00",
        "closed_day": "月曜日",
        "created_at": "2018-08-09 10:03:52",
        "updated_at": "2018-08-09 10:03:52",
        "tellNumber": "09012345678",
        "lat": 35.662163,
        "lng": 139.744629,
        "pivot": {
            "store_id": 1,
            "category_id": 1
        },
        "tags": [
            {
                "id": 1,
                "name": "Sweets",
                "created_at": "2018-08-09 10:00:58",
                "updated_at": "2018-08-09 10:00:58",
                "pivot": {
                    "tag_id": 1,
                    "store_id": 1
                }
            },
            {
                "id": 13,
                "name": "Pink Cafe",
                "created_at": "2018-08-09 10:00:58",
                "updated_at": "2018-08-09 10:00:58",
                "pivot": {
                    "tag_id": 1,
                    "store_id": 13
                }
            },
            {
                "id": 14,
                "name": "Concept Cafe",
                "created_at": "2018-08-09 10:00:58",
                "updated_at": "2018-08-09 10:00:58",
                "pivot": {
                    "tag_id": 1,
                    "store_id": 14
                }
            }
        ],
        "photos": [
            {
                "id": 1,
                "store_id": 1,
                "path": "photos/Pink%20Cafe%20Tokyo_0.jpeg",
                "created_at": "2018-08-09 10:03:52",
                "updated_at": "2018-08-09 10:03:52"
            }
        ]
    }
    }

Solution

  • Considering you have a struct that maps to the store Object with lat and lng as its attributes like below

    struct Store {
     // other attributes
      var lat: CLLocationDegrees
      var lng: CLLocationDegrees
    }
    

    And also that you have current location available in a attribute named currentLocation.

    Now in you array of stores, to sort the objects based on the distance from current location, you can use the code below

    arrOfStores.sort { (obj1, obj2) -> Bool in
                let a = CLLocation(latitude: obj1.lat, longitude: obj1.lng)
                let b = CLLocation(latitude: obj2.lat, longitude: obj2.lng)
                return currentLocation.distance(from: a) < currentLocation.distance(from: b)
    
            }
    

    Now your array of Stores is in ascending order w.r.t distance from current location.