javascript - 用标题反应原生地图fitToCoordinates?
问题描述
我正在使用 Expo 和 React Native Maps,我试图弄清楚如何有 2 个点,一个起点和一个目的地,然后定位地图,使目的地在顶部,原点在底部。我想出了如何计算航向并使用 setCamera 进行设置。但现在我希望地图适合原点和目的地点的地图,但问题是当我调用 fitToCoordinates 时,地图会重置为标题 0。有谁知道如何使用标题执行 fitToCoordinates 之类的操作?
这是代码:
import React, { useRef } from 'react';
import { Dimensions, StyleSheet, View } from 'react-native';
import { StatusBar } from 'expo-status-bar';
import MapView from 'react-native-maps';
// Converts from degrees to radians.
function toRadians(degrees) {
return (degrees * Math.PI) / 180;
}
// Converts from radians to degrees.
function toDegrees(radians) {
return (radians * 180) / Math.PI;
}
function getHeading(origin, destination) {
const originLat = toRadians(origin.latitude);
const originLng = toRadians(origin.longitude);
const destLat = toRadians(destination.latitude);
const destLng = toRadians(destination.longitude);
const y = Math.sin(destLng - originLng) * Math.cos(destLat);
const x =
Math.cos(originLat) * Math.sin(destLat) -
Math.sin(originLat) * Math.cos(destLat) * Math.cos(destLng - originLng);
const heading = toDegrees(Math.atan2(y, x));
return (heading + 360) % 360;
}
const origin = { latitude: 47.58991363446258, longitude: -122.24398815305646 };
const destination = {
latitude: 47.596003585463656,
longitude: -122.32888303102513,
};
const center = {
latitude: (origin.latitude + destination.latitude) / 2,
longitude: (origin.longitude + destination.longitude) / 2,
};
const latitudeDelta =
Math.abs(origin.latitude - destination.latitude) + 0.00025;
const longitudeDelta =
Math.abs(origin.longitude - destination.longitude) + 0.00025;
const heading = getHeading(origin, destination);
export default function App() {
const mapRef = useRef(null);
console.log('origin', origin);
console.log('destination', destination);
console.log('center', center);
console.log('latitudeDelta', latitudeDelta);
console.log('longitudeDelta', longitudeDelta);
console.log('heading', heading);
return (
<View style={styles.container}>
<MapView
style={styles.mapStyle}
ref={mapRef}
initialRegion={{
latitude: center.latitude,
longitude: center.longitude,
latitudeDelta,
longitudeDelta,
}}
onMapReady={() => {
console.log('mapReady...');
// This orientates the map with the origin on bottom and destination on top
// You can see that when the map first loads,
// but the map isn't fit to the points
mapRef.current.setCamera({ center, heading });
// 5 seconds after the map loads,
// this runs and does fit the map to the points,
// but it sets the heading back to 0
setTimeout(() => {
console.log('fitting to coordinates');
mapRef.current.fitToCoordinates([origin, destination], {
animated: false,
});
}, 5000);
}}
scrollEnabled={true}
/>
<StatusBar style='auto' />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
mapStyle: {
width: Dimensions.get('window').width,
height: Dimensions.get('window').height,
},
});
解决方案
推荐阅读
- swift4 - 试图在 swift 4 中使用可编码协议在 JSON Api 中显示所有驱动程序名称
- css - Bootstrap 轮播显示在第一个衰落循环中有延迟的图像
- string - 使用条件缩短 Haskell 中的字符串
- excel - 访问集合导致下标超出范围错误?
- python - Python在二维字典中追加值
- php - SQL:如何在 SQL 中过滤 (WHERE) 连接 (ON) 表?
- c# - 如何在 OWIN 中间件中捕获 SecurityTokenExpiredException?
- python - Pytest BDD - 一个具有多个场景的功能文件
- c# - 在以“管道”开头的字符串中查找连续行
- node.js - 节点 Cheerio 返回 null