import * as Location from 'expo-location'; import * as Permissions from 'expo-permissions'; import * as TaskManager from 'expo-task-manager'; import request from './RequestUtils'; import { alert } from './TotastUtils'; const key = 'c4faf80125b298f93bbc1477db10e69c'; const tengxunKey = 'GLFBZ-ZR2W6-76XSA-MF7CQ-GDJ6Z-6FB5K'; let lat = '31.981746'; let lng = '118.734661'; async function getLocation() { return Location.requestPermissionsAsync() .then((res) => { if (res.status === 'granted') { return Location.getCurrentPositionAsync({ enableHighAccuracy: true, timeout: 5000, }); } else { return Promise.reject(); } }) .then(({ coords }) => { lat = coords.latitude; lng = coords.longitude; return request.get( `https://apis.map.qq.com/ws/coord/v1/translate?locations=${lat},${lng}&type=1&key=${tengxunKey}&get_poi=1`, { prefix: '', mode: 'no-cors', } ); }) .then((res) => { if (res.status === 0) { lat = res.locations[0].lat; lng = res.locations[0].lng; } return request.get( `https://apis.map.qq.com/ws/geocoder/v1/?location=${lat},${lng}&key=${tengxunKey}&get_poi=1`, { prefix: '', mode: 'no-cors', } ); }) .then((res) => { if (res.status === 0) { return Promise.resolve({ addressName: res.result.address, location: res.result.location, }); } else { return Promise.reject(); } }) .catch((e) => { return request .get(`https://apis.map.qq.com/ws/location/v1/ip?key=${tengxunKey}`) .then((res) => { return Promise.resolve({ addressName: '定位失败', location: res.result.location, }); }) .catch((e) => { return Promise.resolve({ addressName: '定位失败', location: { lat, lng, }, }); }); }); } function getSearch(searchKey, boundary) { return request .get( `https://apis.map.qq.com/ws/place/v1/search?boundary=${boundary}&keyword=${searchKey}&page_size=20&page_index=1&orderby=_distance&key=${tengxunKey}`, { prefix: '', mode: 'no-cors', } ) .then((res) => { if (res.status === 0) { return Promise.resolve({ pois: res.data, }); } else { return Promise.reject(); } }) .catch(() => { return Promise.resolve({ pois: [], }); }); } function mapMarks(params) { return `https://restapi.amap.com/v3/staticmap?zoom=15&size=500*500&paths=10,0x0000ff,1,,:116.31604,39.96491;116.320816,39.966606;116.321785,39.966827;116.32361,39.966957&key=${key}`; } export { getLocation, getSearch };