В мобильное приложение я добавляю GeoLocation для результатов поиска, который связан с API веб-сервера, но он всегда возвращается «недоступно».
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-интерфейс)
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(); }
И я определил разрешение в config.xml
<gap:plugin name="cordova-plugin-geolocation" />
Любой совет, пожалуйста?
Пример EDIT @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(); }