Phonegap Geolocation connected to Server API

泄露秘密 提交于 2019-12-12 03:34:08

问题


Into the mobile app I'm adding GeoLocation for search results which is connected to webserver API however it's always returning "not available".

Javascript

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 side)

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";
        if ($res=file_get_contents($file)){
            $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();
}

And I've defined the permission in config.xml

<gap:plugin name="cordova-plugin-geolocation" />

Any advice please?

EDIT example of @Banik

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();
}

来源:https://stackoverflow.com/questions/35591717/phonegap-geolocation-connected-to-server-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!