In my react native application, I am showing services that are offered by my office and the location of those services in a flatlist. I am also showing the miles of the service from the persons current address in that same flatlist. I want those services to be sorted by miles. Below is what my screen data looks like:
Services are available in the following locations:
123 Test Drive
(313) 231-4560
Miles: 7
__________________________
234 Test Drive
(450) 222-9999
Miles: 3
____________________________
121 Addr Drive
(560) 455-1211
Miles: 1
Is their any way I can display these miles by sorting them from least to the highest so I want to display the service that has 1 Miles at the top and then 3 miles after it and then 7 miles after that. Below is the sample:
121 Addr Drive
(560) 455-1211
Miles: 1
____________________________
234 Test Drive
(450) 222-9999
Miles: 3
____________________________
123 Test Drive
(313) 231-4560
Miles: 7
the only issue is that I am calculating these miles. The miles are not ing from my JSON file directly. I have Longitude and Latitude in my JSON file and I am getting the miles by using geodist to get the exact miles.
var geodist = require("geodist");
var dist = geodist(
{ lat: item.cLat, lon: item.cLong },
{ lat: item.LatL, lon: item.Long2 },
"mi"
);
Below is my code for displaying the services address, Miles.
render() {
return (
<View>
<View>
<Text style={styles.title}>
{this.props.navigation.state.params.item.ser}
</Text>
<Text style={styles.SerContent}>
Service is available in the following locations:
</Text>
</View>
<View>
<FlatList
data={newList}
ItemSeparatorComponent={this.FlatListItemSeparator}
renderItem={this._renderItem}
keyExtractor={(item, index) => index}
/>
</View>
</View>
);
}
Below is the code for {_renderItem}
_renderItem = ({ item }) => {
var geodist = require("geodist");
var dist = geodist(
{ lat: item.cLat, lon: item.cLong },
{ lat: item.LatL, lon: item.Long2 },
"mi"
);
return (
<View>
<Text style={styles.Address1}>{item.addr} </Text>
<Text style={styles.Address1}>{item.phone}</Text>
<Text style={styles.AddressSpace}>Miles:{dist}</Text>
</View>
);
}
Below is my JSON file:
[
{
id: "1",
fk: 1,
addr: "123 test drive",
phone: "(313) 231-4560",
LatL: "33.9387",
Long2: "-117.284",
cLat: "33.931",
cLong: "-117.40"
},
{
id: "2",
fk: 1,
addr: "234 Test Drive",
phone: "(450) 222-9999",
LatL: "33.977",
Long2: "-117.37",
cLat: "33.93",
cLong: "-117.409"
},
{
id: "3",
fk: 1,
addr: "121 Addr drive",
phone: "560) 455-1211",
LatL: "33.76",
Long2: "-116.97",
cLat: "33.9319",
cLong: "-117.409"
}
];
Any help will be greatly appreciated.
In my react native application, I am showing services that are offered by my office and the location of those services in a flatlist. I am also showing the miles of the service from the persons current address in that same flatlist. I want those services to be sorted by miles. Below is what my screen data looks like:
Services are available in the following locations:
123 Test Drive
(313) 231-4560
Miles: 7
__________________________
234 Test Drive
(450) 222-9999
Miles: 3
____________________________
121 Addr Drive
(560) 455-1211
Miles: 1
Is their any way I can display these miles by sorting them from least to the highest so I want to display the service that has 1 Miles at the top and then 3 miles after it and then 7 miles after that. Below is the sample:
121 Addr Drive
(560) 455-1211
Miles: 1
____________________________
234 Test Drive
(450) 222-9999
Miles: 3
____________________________
123 Test Drive
(313) 231-4560
Miles: 7
the only issue is that I am calculating these miles. The miles are not ing from my JSON file directly. I have Longitude and Latitude in my JSON file and I am getting the miles by using geodist to get the exact miles.
var geodist = require("geodist");
var dist = geodist(
{ lat: item.cLat, lon: item.cLong },
{ lat: item.LatL, lon: item.Long2 },
"mi"
);
Below is my code for displaying the services address, Miles.
render() {
return (
<View>
<View>
<Text style={styles.title}>
{this.props.navigation.state.params.item.ser}
</Text>
<Text style={styles.SerContent}>
Service is available in the following locations:
</Text>
</View>
<View>
<FlatList
data={newList}
ItemSeparatorComponent={this.FlatListItemSeparator}
renderItem={this._renderItem}
keyExtractor={(item, index) => index}
/>
</View>
</View>
);
}
Below is the code for {_renderItem}
_renderItem = ({ item }) => {
var geodist = require("geodist");
var dist = geodist(
{ lat: item.cLat, lon: item.cLong },
{ lat: item.LatL, lon: item.Long2 },
"mi"
);
return (
<View>
<Text style={styles.Address1}>{item.addr} </Text>
<Text style={styles.Address1}>{item.phone}</Text>
<Text style={styles.AddressSpace}>Miles:{dist}</Text>
</View>
);
}
Below is my JSON file:
[
{
id: "1",
fk: 1,
addr: "123 test drive",
phone: "(313) 231-4560",
LatL: "33.9387",
Long2: "-117.284",
cLat: "33.931",
cLong: "-117.40"
},
{
id: "2",
fk: 1,
addr: "234 Test Drive",
phone: "(450) 222-9999",
LatL: "33.977",
Long2: "-117.37",
cLat: "33.93",
cLong: "-117.409"
},
{
id: "3",
fk: 1,
addr: "121 Addr drive",
phone: "560) 455-1211",
LatL: "33.76",
Long2: "-116.97",
cLat: "33.9319",
cLong: "-117.409"
}
];
Any help will be greatly appreciated.
Share Improve this question edited Jul 9, 2018 at 16:35 Tholle 113k22 gold badges208 silver badges197 bronze badges asked Jul 9, 2018 at 16:13 AnjaliAnjali 2,70811 gold badges47 silver badges90 bronze badges 02 Answers
Reset to default 4You could sort
the array by distance
before you give it as data to the FlatList
It would probably be a good idea to put the distance in the state, so that you don't have to do the calculations in both the sorting and in the _renderItem
method.
<FlatList
data={newList.sort((a, b) => {
const aDist = geodist(
{ lat: a.cLat, lon: a.cLong },
{ lat: a.LatL, lon: a.Long2 },
"mi"
);
const bDist = geodist(
{ lat: b.cLat, lon: b.cLong },
{ lat: b.LatL, lon: b.Long2 },
"mi"
);
return aDist - bDist;
})}
ItemSeparatorComponent={this.FlatListItemSeparator}
renderItem={this._renderItem}
keyExtractor={(item, index) => index}
/>
Using the Tholle method, i used the Geolib Liberary. And using the method getDistance to pare distances this is the result.
<FlatList data={newList.sort((a, b) => {
const aDist = getDistance({ //get distance between plaza and current location
latitude: a.latitud,
longitude: a.longitud
}, {
latitude: latitude,
longitude: longitude,
})
const bDist = getDistance({ //get distance between plaza and current location
latitude: b.latitud,
longitude: b.longitud
}, {
latitude: latitude,
longitude: longitude,
})
return aDist - bDist; })} ItemSeparatorComponent={this.FlatListItemSeparator} renderItem={this._renderItem}keyExtractor={(item, index) => index} />
Where latitude and longitud are the user's lcoation and the object data has already a latitude and longitude. Dont forget to import:
import {getDistance} from 'geolib';
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745553970a4632706.html
评论列表(0条)