Jak vycentrovat mapu ukázat aktuální polohy uživatele, když se obrazovka mapa je otevřel? Po expo dokumentaci, mělo by být dosaženo s Expo Umístění API? Nicméně, v dokumentaci je nejasný. Vzal jsem část kódu z expo Umístění dokumentace a realizována v mé Mapě Obrazovce. Tak, jak by měla integrovat to v MapView k provedení getCurrentPositionAsync metoda a centrum na mapě, tedy, když se obrazovka mapa je otevřel?
import React, { useContext, useState, useEffect } from "react";
import MapView from "react-native-maps";
import styled from "styled-components";
import { Searchbar } from "react-native-paper";
import { View } from "react-native";
import * as Location from 'expo-location';
const Map = styled(MapView)`
height: 100%;
width: 100%;
`;
const SearchBarContainer= styled(View)`
padding: ${(props) => props.theme.space[3]};
position: absolute;
z-index: 999;
top: 20px;
width: 100%;
`;
export const MapScreen = ({navigation}) => {
const [location, setLocation] = useState(null);
const [errorMsg, setErrorMsg] = useState(null);
useEffect(() => {
(async () => {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to access location was denied');
return;
}
let location = await Location.getCurrentPositionAsync({});
setLocation(location);
})();
}, []);
return (
<>
<SearchBarContainer>
<Searchbar placeholder="location"/>
</SearchBarContainer>
<Map showsUserLocation={true}>
</Map>
</>
);};