| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- import * as Location from "expo-location";
- import axios from "axios";
- 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,
- });
- }
- return Promise.reject();
- })
- .then(({ coords }) => {
- console.log(coords);
- lat = coords.latitude;
- lng = coords.longitude;
- return axios.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.data.status === 0) {
- lat = res.data.locations[0].lat;
- lng = res.data.locations[0].lng;
- }
- return axios.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.data.status === 0) {
- return Promise.resolve({
- addressName: res.data.result.address,
- location: res.data.result.location,
- });
- }
- return Promise.reject();
- })
- .catch(e => {
- console.log(e);
- return Promise.resolve({
- addressName: "定位失败",
- location: {
- lat,
- lng,
- },
- });
- });
- }
- function getSearch(searchKey, boundary) {
- return axios
- .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,
- });
- }
- 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 };
|