确保您已经启用了定位服务,并将 Expo 位置设置为“始终允许”或“允许时在使用应用程序期间”。
检查您的Android设备是否支持位置服务。如果您的设备没有内置 GPS 或网络连接,则可能无法访问位置服务。您可以尝试使用模拟器或在其他设备上测试该应用程序。
您可以尝试使用Expo的Location.getCurrentPositionAsync()函数来获取设备的当前位置。以下代码演示了如何使用该函数:
import * as Location from 'expo-location';
async function getLocationAsync() { let { status } = await Location.requestForegroundPermissionsAsync(); if (status !== 'granted') { console.log('Permission to access location was denied'); return; }
let location = await Location.getCurrentPositionAsync({}); console.log(location); }
import MapView from 'react-native-maps'; import * as Location from 'expo-location';
class MyMapView extends React.Component { state = { region: null, };
componentDidMount() { this._getLocationAsync(); }
render() { if (!this.state.region) { return null; }
return (
);
}
_getLocationAsync = async () => { let { status } = await Location.requestForegroundPermissionsAsync(); if (status !== 'granted') { console.log('Permission to access location was denied'); return; }
let location = await Location.getCurrentPositionAsync({});
let region = {
latitude: location.coords.latitude,
longitude: location.coords.longitude,
latitudeDelta: 0.01,
longitudeDelta: 0.01,
};
this.setState({ region });
}; }