import React, { useEffect, useState } from ‘react’;
import { Text, View } from ‘react-native’;
import axios from ‘axios’;
import * as Location from ‘expo-location’;
const Map1 = () => {
const [location, setLocation] = useState(null);
const [errorMessage, setErrorMessage] = useState(null);
const [counselingCenters, setCounselingCenters] = useState([]);
useEffect(() => {
const fetchData = async () => {
try {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== ‘granted’) {
setErrorMessage(‘Permission to access location was denied’);
return;
}
let userLocation = await Location.getCurrentPositionAsync({});
setLocation(userLocation.coords);
const apiUrl =`https://dapi.kakao.com/v2/local/search/keyword.json?query=상담심리센터&y=${userLocation.coords.latitude}&x=${userLocation.coords.longitude}&radius=2000`;
const response = await axios.get(apiUrl, {
headers: {
Authorization: '', // 발급받은 카카오 REST API 키를 여기에 넣어주세요
},
});
const centers = response.data.documents;
setCounselingCenters(centers);
} catch (error) {
console.error('Error fetching data:', error);
setErrorMessage('Failed to fetch counseling centers.');
}
};
fetchData();
}, []);
return (
{errorMessage ? (
{errorMessage}
) : (
주변 상담심리센터:
{counselingCenters.map((center, index) => (
{center.place_name}
))}
)}
);
};
export default Map1;
아무리 정확한 api키를 넣어도 Error fetching data:, [AxiosError: Request failed with status code 401] 오류가뜹니다 해결법좀 알려주세요 ㅠㅠ