Swift4:我想从我的位置重新排列 API 获得的 JSON
Swift4: I want to rearrange the JSON acquired by API in near order from my location
感谢您观看我的问题。
我的iOS申请流程。当您按下类别按钮时,将返回该类别中商店的数据JSON。纬度和经度包含在商店的数据中。根据这个纬度和经度,我想重新排列 JSON 的内容,以便您可以从我的位置开始按顺序显示商店。
我正在搜索示例代码,但找不到。
请告诉我。
谢谢。
JSON数据
{
"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"
}
]
}
}
考虑到您有一个 struct
映射到以 lat 和 lng 作为其属性的商店对象,如下所示
struct Store {
// other attributes
var lat: CLLocationDegrees
var lng: CLLocationDegrees
}
而且您在名为 currentLocation 的属性中有可用的当前位置。
现在在你的商店数组中,要根据与当前位置的距离对对象进行排序,你可以使用下面的代码
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)
}
现在您的商店数组按升序排列与当前位置的距离为 w.r.t。
感谢您观看我的问题。
我的iOS申请流程。当您按下类别按钮时,将返回该类别中商店的数据JSON。纬度和经度包含在商店的数据中。根据这个纬度和经度,我想重新排列 JSON 的内容,以便您可以从我的位置开始按顺序显示商店。
我正在搜索示例代码,但找不到。
请告诉我。
谢谢。
JSON数据
{
"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"
}
]
}
}
考虑到您有一个 struct
映射到以 lat 和 lng 作为其属性的商店对象,如下所示
struct Store {
// other attributes
var lat: CLLocationDegrees
var lng: CLLocationDegrees
}
而且您在名为 currentLocation 的属性中有可用的当前位置。
现在在你的商店数组中,要根据与当前位置的距离对对象进行排序,你可以使用下面的代码
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)
}
现在您的商店数组按升序排列与当前位置的距离为 w.r.t。