Javascript 世博会地点。reverseGeocodeAsync发布
获取用户坐标并了解坐标所指地址的位置: 但我不明白这是我的代码的一部分:Javascript 世博会地点。reverseGeocodeAsync发布,javascript,reactjs,react-native,Javascript,Reactjs,React Native,获取用户坐标并了解坐标所指地址的位置: 但我不明白这是我的代码的一部分: const AdressWithCoordonate = async (coordinate) => { try { let result = await Location.reverseGeocodeAsync(coordinate); return result[0]; } catch (e) { console.log(e); } }; cons
const AdressWithCoordonate = async (coordinate) => {
try {
let result = await Location.reverseGeocodeAsync(coordinate);
return result[0];
} catch (e) {
console.log(e);
}
};
const mapHandlePress = (coordinates) => {
let arrayMarkers = [...markers];
arrayMarkers.push({
id: arrayMarkers.length + 1,
coordinate: coordinates,
address: AdressWithCoordonate(coordinates),
title: "test",
description: "test2",
});
console.log(arrayMarkers);
setMarkers(arrayMarkers);
console.log(markers);
};
当我看到address prop中的值时,我会看到如下内容:
Object {
"address": Promise {
"_40": 0,
"_55": Object {
"city": "San Francisco",
"country": "United States",
"isoCountryCode": "US",
"name": "980 Green St",
"postalCode": "94133",
"region": "CA",
"street": "Green St",
},
"_65": 1,
"_72": null,
},
const mapHandlePress = (coordinates) => {
AdressWithCoordonate(coordinates).then((res) => {
let arrayMarkers = [...markers];
arrayMarkers.push({
id: arrayMarkers.length + 1,
coordinate: coordinates,
address: res,
title: 'test',
description: 'test2',
});
console.log(arrayMarkers);
setMarkers(arrayMarkers);
console.log(markers);
});
};
(我没有忘记询问位置权限)
我不知道怎么解决这个问题,请帮帮我。谢谢试试这样的方法:
Object {
"address": Promise {
"_40": 0,
"_55": Object {
"city": "San Francisco",
"country": "United States",
"isoCountryCode": "US",
"name": "980 Green St",
"postalCode": "94133",
"region": "CA",
"street": "Green St",
},
"_65": 1,
"_72": null,
},
const mapHandlePress = (coordinates) => {
AdressWithCoordonate(coordinates).then((res) => {
let arrayMarkers = [...markers];
arrayMarkers.push({
id: arrayMarkers.length + 1,
coordinate: coordinates,
address: res,
title: 'test',
description: 'test2',
});
console.log(arrayMarkers);
setMarkers(arrayMarkers);
console.log(markers);
});
};
通过这种方式,实际上可以传递异步操作的结果,而不是整个Promise对象