Javascript 连接到服务器API的Phonegap地理位置
在移动应用程序中,我为搜索结果添加了地理位置,该搜索结果连接到Web服务器API,但它总是返回“不可用” JavascriptJavascript 连接到服务器API的Phonegap地理位置,javascript,php,cordova,phonegap-plugins,phonegap-build,Javascript,Php,Cordova,Phonegap Plugins,Phonegap Build,在移动应用程序中,我为搜索结果添加了地理位置,该搜索结果连接到Web服务器API,但它总是返回“不可用” Javascript function getCurrentLocation() { navigator.geolocation.getCurrentPosition(geolocationSuccess,geolocationError, { enableHighAccuracy: true } ); } function geolocationSuccess(
function getCurrentLocation()
{
navigator.geolocation.getCurrentPosition(geolocationSuccess,geolocationError,
{ enableHighAccuracy: true } );
}
function geolocationSuccess(position)
{
var params="lat="+position.coords.latitude;
params+="&lng="+position.coords.longitude;
callAjax("reverseGeoCoding",params);
}
function geolocationError(error)
{
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
PHP(API端)
我已经在config.xml中定义了权限
<gap:plugin name="cordova-plugin-geolocation" />
您是否仅使用PHPAPI来混响地理代码?没有其他错误吗?@banik是的,确实只是颠倒了地理代码没有其他错误,我想我的JS代码很好地返回了我不确定为什么我会出现这个错误实际上你必须调试它得到的url并将该url粘贴到浏览器以检查值,否则,我们无法进行调试,或者您可以使用jquery ajax函数获取类似$.ajax的值({url:,success:function(result){alert(result[0].格式化的_address);},});还有另一种使用google map api获取地址的方法,如果您愿意,我可以将这些方法作为答案发布,也不需要php脚本。@Banik感谢您的回复是的,请将其作为答案发布,并在我结束时进行故障排除。谢谢!正如您所建议的,我尝试了直接浏览器访问,我得到了{您已经超过了此API的每日请求配额。我们建议您在Google开发者控制台注册密钥:“}听起来有问题
<gap:plugin name="cordova-plugin-geolocation" />
public function actionReverseGeoCoding()
{
if (isset($this->data['lat']) && !empty($this->data['lng'])){
$latlng=$this->data['lat'].",".$this->data['lng'];
$file="https://maps.googleapis.com/maps/api/geocode/json?latlng=".$latlng."&sensor=true";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $file);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
if ($res==$data){
$res=json_decode($res,true);
if (AddonMobileApp::isArray($res)){
$this->code=1; $this->msg="OK";
$this->details=$res['results'][0]['formatted_address'];
} else $this->msg=$this->t("not available");
} else $this->msg=$this->t("not available");
} else $this->msg=$this->t("missing coordinates");
$this->output();
}