angular - 我如何通过插件获取 lat long 值,而不是在角度 5 中调用 ionic 中的 Web 服务?
问题描述
我试图通过在离子框架中按角度 5 的地理编码给出位置来找出纬度。在找到 lat long 后,我试图通过在服务中传递 lat long 来调用我的服务。
问题是我的服务在插件调用之前被调用,因此我得到了未定义的 lat long 的值。
//this is the plugin
selectSearchResult(item) {
var address =item.description;
this.geocoder.geocode({ 'address': address}, function(results, status) {
if (status == 'OK') {
this.formatted_address=results[0].formatted_address;
console.log(this.formatted_address);
this.latitude = results[0].geometry.location.lat();
this.longitude = results[0].geometry.location.lng();
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
////this is my service
this.fetch.GetLocation(this.latitude,this.longitude).subscribe(
result => {
console.log(result);
},
err =>{
console.error("Error : "+err);
} ,
() => {
console.log('getData completed');
}
);
}
我如何在我的网络服务之前调用插件?
解决方案
对于您的代码,可以尝试如下:
selectSearchResult = (item) => {
var address =item.description;
this.geocoder.geocode({ 'address': address}, function(results, status) {
if (status == 'OK') {
this.formatted_address=results[0].formatted_address;
console.log(this.formatted_address);
this.latitude = results[0].geometry.location.lat();
this.longitude = results[0].geometry.location.lng();
//web service function call
this.serviceFun();
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
serviceFun = () => {
this.fetch.GetLocation(this.latitude,this.longitude).subscribe(
result => {
console.log(result);
},
err =>{
console.error("Error : "+err);
} ,
() => {
console.log('getData completed');
}
);
}
推荐阅读
- spring-boot - Camunda,不会部署到 Tomcat
- javascript - 什么原因无法加载默认凭据错误?
- maximo-anywhere - 用于自定义 maximo Anywhere 应用程序的 Apple 帐户
- node.js - 我可以让我的 NodeJS 服务器等待函数调用吗?我的程序完成得太快了
- reactjs - useEffect 没有正确删除事件
- javascript - 如何在某些情况下防止自定义指令
- flutter - Flutter Quick Actions 更改选定的底部导航栏项目
- excel - Excel VBA 指的是使用图标/表情符号作为名称一部分的工作表
- django - 注册代码中出现意外的 MultiValueDictKeyError
- ruby - 如何设置正确的 Ruby 版本以在 Aptana 3 中与我的 Rails 5 项目一起使用?