试图获取非对象地理位置google API的属性

时间:2017-10-24 11:46:30

标签: php google-maps google-maps-api-3 latitude-longitude google-geolocation

我正在通过地理定位API从谷歌地图获取经度和纬度。我已经尝试了很多次,但总是显示这个错误:

  

尝试在第16行的C:\ wamp64 \ www \ index3.php中获取非对象的属性

问题在于我无法从$url获取信息,因此无法获得经纬度。

我改变了我的代码,现在却是,但仍然没有工作。

<php

$address = urlencode('BTM 2nd Stage,Bengaluru,Karnataka,560076');
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=$address&key=*************************************";


$json = file_get_contents($url); // I tried with the @ symbol and without it

$data=json_decode($json);

echo ($data->results[0]->geometry->location->lat);

?>

我也用过这个:

<php
$address = 'BTM 2nd Stage,Bengaluru,Karnataka,560076';
$encode = urlencode($address);
$key = "insert key here";
$url = "https://maps.googleapis.com/maps/api/geocode/json?address={$encode}&key={$key}";

$json = file_get_contents($url); // I tried with the @ symbol and without it
$data = json_decode($json);

echo "Latitud".($data->results[0]->geometry->location->lat);

?>

没有@符号我有这个错误行

  

警告:出于安全原因, [...] <... b> 8 <已禁用file_get_contents() BR />

6 个答案:

答案 0 :(得分:0)

更改 http 在您的请求网址中 https

答案 1 :(得分:0)

作为Abrucius,您使用HTTPS或谷歌将拒绝该请求。您还必须对地址进行编码,否则您将收到错误的请求&#34;错误。

<?php
$address = 'BTM 2nd Stage,Bengaluru,Karnataka,560076';
$encode = urlencode($address);
$key = "insert key here";
$url = "https://maps.googleapis.com/maps/api/geocode/json?address={$encode}&key={$key}";

$json = file_get_contents($url);
$data = json_decode($json);

echo ($data->results[0]->geometry->location->lat);    

输出:12.9082396

答案 2 :(得分:0)

替换以下代码

    $address = 'BTM 2nd Stage,Bengaluru,Karnataka,560076';
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=$address&key=AIzaSyDjWI3J9CNlANoB5PXNKq3nz1ZbYeOrNGE";

 $address = urlencode('BTM 2nd Stage,Bengaluru,Karnataka,560076');
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=$address&key=AIzaSyDjWI3J9CNlANoB5PXNKq3nz1ZbYeOrNGE";

答案 3 :(得分:0)

您在网址中提供了http,它应该是https。尝试下面的代码,可以帮助您。您必须使用urlencode(),因为api不允许地址中的空格,因此您必须进行编码。

$address = 'BTM 2nd Stage,Bengaluru,Karnataka,560076';
$address = urlencode($address);

$url = "https://maps.google.com/maps/api/geocode/json?address={$address}&key=***************************";

$resp_json = file_get_contents($url);

$resp = json_decode($resp_json, true);
echo "<pre>";print_r($resp["results"][0]['geometry']['location']['lat']);

尝试此代码肯定会起作用

答案 4 :(得分:0)

WillParky93的回答似乎对我有用。也许尝试按照下一个链接允许https包装:

How to get file_get_contents() to work with HTTPS?

...或发布您收到的错误,以便我们为您提供帮助

答案 5 :(得分:0)

我刚刚解决了这个问题,因为代理配置,我收到了这个错误。我正在使用代理配置测试网络。因此,代理不允许我连接Google数据库。然后我尝试连接不同的网络,它完美地工作。谢谢你们的帮助!