谷歌地图在php中获取坐标的问题

发布于 2024-10-14 05:39:15 字数 2280 浏览 2 评论 0原文

这是在 php 上,我在数组上有以下变量,

Array ( [0] => { "name": "BRAVO Mario1050 [1] => Capital Federal [2] => Argentina" [3] => "Status": { "code": 200 [4] => "request": "geocode" } [5] => "Placemark": [ { "id": "p1" [6] => "address": "Buenos Aires [7] => Capital Federal [8] => Argentina" [9] => "AddressDetails": { "Accuracy" : 4 [10] => "Country" : { "AdministrativeArea" : { "AdministrativeAreaName" : "Capital Federal" [11] => "Locality" : { "LocalityName" : "Ciudad Autónoma de Buenos Aires" } } [12] => "CountryName" : "Argentina" [13] => "CountryNameCode" : "AR" } } [14] => "ExtendedData": { "LatLonBox": { "north": -34.5349161 [15] => "south": -34.6818539 [16] => "east": -58.2451019 [17] => "west": -58.5012207 } } [18] => "Point": { "coordinates": [ -58.3731613 [19] => -34.6084175 [20] => 0 ] } } ] } ) 

我正在使用数组、爆炸和 str_replace 来获取 -58.3731613、-34.6084175 到两个变量中,有没有一种简单的方法可以做到这一点?

我有一个额外的问题,我所做的工作是有效的,但显然谷歌改变了一些东西,因为现在我得到了与 1 个月前不同的结果,问题是......有人知道为什么谷歌改变了一些东西吗?

感谢您

所做的一切以防万一旧代码曾经有效:

        $longitude = "";
        $latitude = "";
        $precision = "";
        //Three parts to the querystring: q is address, output is the format (
        $key = "googlekey";
        $address = urlencode(str_replace(',',' ',$calle).$altura.", ".$localidadList.", Argentina");
        $url = "http://maps.google.com/maps/geo?q=".$address."&output=csv&key=".$key;
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($ch);
        curl_close($ch);
        $latitude= str_replace('Point','',$data[20]);
        $latitude= str_replace('coordinates','',$latitude);
        $latitude= str_replace('"','',$latitude);
        $latitude= str_replace(':','',$latitude);
        $latitude= str_replace('{','',$latitude);
        $latitude= trim(str_replace('[','',$latitude));
        $longitude= trim(str_replace('}','',str_replace('"west": ','',$data[21])));

This is on php I have the following variable on an array

Array ( [0] => { "name": "BRAVO Mario1050 [1] => Capital Federal [2] => Argentina" [3] => "Status": { "code": 200 [4] => "request": "geocode" } [5] => "Placemark": [ { "id": "p1" [6] => "address": "Buenos Aires [7] => Capital Federal [8] => Argentina" [9] => "AddressDetails": { "Accuracy" : 4 [10] => "Country" : { "AdministrativeArea" : { "AdministrativeAreaName" : "Capital Federal" [11] => "Locality" : { "LocalityName" : "Ciudad Autónoma de Buenos Aires" } } [12] => "CountryName" : "Argentina" [13] => "CountryNameCode" : "AR" } } [14] => "ExtendedData": { "LatLonBox": { "north": -34.5349161 [15] => "south": -34.6818539 [16] => "east": -58.2451019 [17] => "west": -58.5012207 } } [18] => "Point": { "coordinates": [ -58.3731613 [19] => -34.6084175 [20] => 0 ] } } ] } ) 

I am using arrays, explodes and str_replace to obtain the -58.3731613, -34.6084175 into two variables, is there an easy way to do this?

I have an extra question to, What I did was working, but apparently google change something because now I have a different result that I had 1 month ago, the question is .... does anybody know why google changed something?

Thanks for everything

Just in case old code that used to work:

        $longitude = "";
        $latitude = "";
        $precision = "";
        //Three parts to the querystring: q is address, output is the format (
        $key = "googlekey";
        $address = urlencode(str_replace(',',' ',$calle).$altura.", ".$localidadList.", Argentina");
        $url = "http://maps.google.com/maps/geo?q=".$address."&output=csv&key=".$key;
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($ch);
        curl_close($ch);
        $latitude= str_replace('Point','',$data[20]);
        $latitude= str_replace('coordinates','',$latitude);
        $latitude= str_replace('"','',$latitude);
        $latitude= str_replace(':','',$latitude);
        $latitude= str_replace('{','',$latitude);
        $latitude= trim(str_replace('[','',$latitude));
        $longitude= trim(str_replace('}','',str_replace('"west": ','',$data[21])));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何其悲哀 2024-10-21 05:39:15

3行代码怎么样? ;-)

$b = file_get_contents("http://maps.google.com/maps/geo?q=". urlencode("1050+BRAVO Mario,+Capital Federal,+Argentina") ."&oe=utf8&key=abcdefg");
$b = json_decode($b, TRUE);
list($longitude,$latitude) = $b['Placemark'][0]['Point']['coordinates'];
echo "Longitude: " . $longitude . "<br />Latitude: " . $latitude;

请注意,这里我们假设只返回一个地标,如果您期望更多,则循环 $b['Placemark'] 变量来获取数据。

How about 3 lines of code? ;-)

$b = file_get_contents("http://maps.google.com/maps/geo?q=". urlencode("1050+BRAVO Mario,+Capital Federal,+Argentina") ."&oe=utf8&key=abcdefg");
$b = json_decode($b, TRUE);
list($longitude,$latitude) = $b['Placemark'][0]['Point']['coordinates'];
echo "Longitude: " . $longitude . "<br />Latitude: " . $latitude;

Please note that here, we are assuming we only have ONE Placemark returned, if you are expecting more, then loop the $b['Placemark'] variable to get your data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文