我要实现一个定位的功能~~
在postman上面可以测试成功
但是在项目中不行
控制台打印的结果为:
求各位大佬们帮忙看看~
fetch请求返回的是一个Promise,这个Promise包装了一些请求的属性,如果需要仅仅获取接口返回的数据,这样格式化一下json:
fetch(url).then(data => data.json()).then(data => {
//这里就是你需要的JSON了
console.log(data);
})
fetch(url).then(data => data.json()).then(data => {
//这里就是你需要的JSON了
console.log(data);
})
componentDidMount() {
this._getCityLocation()
.then((data) => {
console.log("位置?", "" + JSON.stringify(data));
})
.catch((error) => {
console.log("失败" + JSON.stringify(error));
})
}
//获取经纬度的方法
_getLatitudeAndLongitude(){
return new Promise((resolve,reject) => {
Geolocation.getCurrentPosition(
location => {
resolve([location.coords.latitude,location.coords.longitude]);
},
error => {
reject(error);
}
);
})
}
//网络请求
_getNetData(url) {
return new Promise((resolve,reject) => {
fetch(url).then((response) => response.json())
.then((responseData) => {
resolve(responseData);
})
.catch((error) => {
reject(error)
})
.done()
});
}
//获取当前城市
_getCityLocation() {
return new Promise((resolve,reject) => {
this._getLatitudeAndLongitude()
.then((locationArr) => {
let latitude = locationArr[0];
let longitude = locationArr[1];
const BaiduMap_URL = "http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&output=json&pois=1&" +
"ak=3ZqDvnxYpoFz2oUQmCFMu9XpUm3QKuXv&mcode=B8:CA:55:20:E5:A1:8C:94:9B:53:E1:81:8F:92:8B:D4:59:5C:5B:D1;com.traveltips&" +
"location=";
this._getNetData(BaiduMap_URL + latitude + "," + longitude)
.then((data) => {
if(data.status == 0){
resolve(data);
} else {
reject(data.code);
}
})
}).catch((data) => {
reject(data.code);
})
});
}